Skip to main content

rustjava_runtime/classes/java/util/logging/
handler.rs

1use alloc::vec;
2
3use jvm::{ClassInstanceRef, Jvm, Result};
4use jvm_class_proto::{JavaFieldProto, JavaMethodProto};
5use jvm_types::{ClassAccessFlags, FieldAccessFlags, MethodAccessFlags};
6
7use crate::{
8    RuntimeClassProto, RuntimeContext,
9    classes::java::{
10        io::OutputStreamWriter,
11        lang::{Exception, String},
12    },
13};
14
15use super::{Filter, Formatter, Level, LogRecord};
16
17// public abstract class java.util.logging.Handler
18pub struct Handler;
19
20impl Handler {
21    pub fn as_proto() -> RuntimeClassProto {
22        RuntimeClassProto {
23            name: "java/util/logging/Handler",
24            parent_class: Some("java/lang/Object"),
25            interfaces: vec![],
26            methods: vec![
27                JavaMethodProto::new("<init>", "()V", Self::init, MethodAccessFlags::PROTECTED),
28                JavaMethodProto::new_abstract("close", "()V", MethodAccessFlags::PUBLIC | MethodAccessFlags::ABSTRACT),
29                JavaMethodProto::new_abstract("flush", "()V", MethodAccessFlags::PUBLIC | MethodAccessFlags::ABSTRACT),
30                JavaMethodProto::new_abstract(
31                    "publish",
32                    "(Ljava/util/logging/LogRecord;)V",
33                    MethodAccessFlags::PUBLIC | MethodAccessFlags::ABSTRACT,
34                ),
35                JavaMethodProto::new("getEncoding", "()Ljava/lang/String;", Self::get_encoding, MethodAccessFlags::PUBLIC),
36                JavaMethodProto::new("getFilter", "()Ljava/util/logging/Filter;", Self::get_filter, MethodAccessFlags::PUBLIC),
37                JavaMethodProto::new(
38                    "getFormatter",
39                    "()Ljava/util/logging/Formatter;",
40                    Self::get_formatter,
41                    MethodAccessFlags::PUBLIC,
42                ),
43                JavaMethodProto::new("getLevel", "()Ljava/util/logging/Level;", Self::get_level, MethodAccessFlags::PUBLIC),
44                JavaMethodProto::new(
45                    "isLoggable",
46                    "(Ljava/util/logging/LogRecord;)Z",
47                    Self::is_loggable,
48                    MethodAccessFlags::PUBLIC,
49                ),
50                JavaMethodProto::new(
51                    "reportError",
52                    "(Ljava/lang/String;Ljava/lang/Exception;I)V",
53                    Self::report_error,
54                    MethodAccessFlags::PROTECTED,
55                ),
56                JavaMethodProto::new("setEncoding", "(Ljava/lang/String;)V", Self::set_encoding, MethodAccessFlags::PUBLIC),
57                JavaMethodProto::new("setFilter", "(Ljava/util/logging/Filter;)V", Self::set_filter, MethodAccessFlags::PUBLIC),
58                JavaMethodProto::new(
59                    "setFormatter",
60                    "(Ljava/util/logging/Formatter;)V",
61                    Self::set_formatter,
62                    MethodAccessFlags::PUBLIC,
63                ),
64                JavaMethodProto::new("setLevel", "(Ljava/util/logging/Level;)V", Self::set_level, MethodAccessFlags::PUBLIC),
65            ],
66            fields: vec![
67                JavaFieldProto::new("level", "Ljava/util/logging/Level;", FieldAccessFlags::PRIVATE),
68                JavaFieldProto::new("filter", "Ljava/util/logging/Filter;", FieldAccessFlags::PRIVATE),
69                JavaFieldProto::new("formatter", "Ljava/util/logging/Formatter;", FieldAccessFlags::PRIVATE),
70                JavaFieldProto::new("encoding", "Ljava/lang/String;", FieldAccessFlags::PRIVATE),
71            ],
72            access_flags: ClassAccessFlags::PUBLIC | ClassAccessFlags::ABSTRACT,
73        }
74    }
75
76    async fn init(jvm: &Jvm, _: &mut RuntimeContext, mut this: ClassInstanceRef<Self>) -> Result<()> {
77        tracing::debug!("java.util.logging.Handler::<init>({this:?})");
78
79        let _: () = jvm.invoke_special(&this, "java/lang/Object", "<init>", "()V", ()).await?;
80        let level: ClassInstanceRef<Level> = jvm
81            .get_static_field("java/util/logging/Level", "ALL", "Ljava/util/logging/Level;")
82            .await?;
83        jvm.put_field(&mut this, "level", "Ljava/util/logging/Level;", level).await
84    }
85
86    async fn get_encoding(jvm: &Jvm, _: &mut RuntimeContext, this: ClassInstanceRef<Self>) -> Result<ClassInstanceRef<String>> {
87        tracing::debug!("java.util.logging.Handler::getEncoding({this:?})");
88
89        jvm.get_field(&this, "encoding", "Ljava/lang/String;").await
90    }
91
92    async fn get_filter(jvm: &Jvm, _: &mut RuntimeContext, this: ClassInstanceRef<Self>) -> Result<ClassInstanceRef<Filter>> {
93        tracing::debug!("java.util.logging.Handler::getFilter({this:?})");
94
95        jvm.get_field(&this, "filter", "Ljava/util/logging/Filter;").await
96    }
97
98    async fn get_formatter(jvm: &Jvm, _: &mut RuntimeContext, this: ClassInstanceRef<Self>) -> Result<ClassInstanceRef<Formatter>> {
99        tracing::debug!("java.util.logging.Handler::getFormatter({this:?})");
100
101        jvm.get_field(&this, "formatter", "Ljava/util/logging/Formatter;").await
102    }
103
104    async fn get_level(jvm: &Jvm, _: &mut RuntimeContext, this: ClassInstanceRef<Self>) -> Result<ClassInstanceRef<Level>> {
105        tracing::debug!("java.util.logging.Handler::getLevel({this:?})");
106
107        jvm.get_field(&this, "level", "Ljava/util/logging/Level;").await
108    }
109
110    async fn is_loggable(jvm: &Jvm, _: &mut RuntimeContext, this: ClassInstanceRef<Self>, record: ClassInstanceRef<LogRecord>) -> Result<bool> {
111        tracing::debug!("java.util.logging.Handler::isLoggable({this:?}, {record:?})");
112
113        if record.is_null() {
114            return Ok(false);
115        }
116
117        let record_level: ClassInstanceRef<Level> = jvm
118            .invoke_virtual(&record, "java/util/logging/LogRecord", "getLevel", "()Ljava/util/logging/Level;", ())
119            .await?;
120        let handler_level: ClassInstanceRef<Level> = jvm.get_field(&this, "level", "Ljava/util/logging/Level;").await?;
121        let record_value: i32 = jvm
122            .invoke_virtual(&record_level, "java/util/logging/Level", "intValue", "()I", ())
123            .await?;
124        let handler_value: i32 = jvm
125            .invoke_virtual(&handler_level, "java/util/logging/Level", "intValue", "()I", ())
126            .await?;
127        if handler_value == i32::MAX || record_value < handler_value {
128            return Ok(false);
129        }
130
131        let filter: ClassInstanceRef<Filter> = jvm.get_field(&this, "filter", "Ljava/util/logging/Filter;").await?;
132        if filter.is_null() {
133            return Ok(true);
134        }
135
136        jvm.invoke_virtual(
137            &filter,
138            &filter.class_definition().name(),
139            "isLoggable",
140            "(Ljava/util/logging/LogRecord;)Z",
141            (record,),
142        )
143        .await
144    }
145
146    async fn report_error(
147        _: &Jvm,
148        _: &mut RuntimeContext,
149        this: ClassInstanceRef<Self>,
150        _: ClassInstanceRef<String>,
151        _: ClassInstanceRef<Exception>,
152        _: i32,
153    ) -> Result<()> {
154        tracing::debug!("java.util.logging.Handler::reportError({this:?})");
155
156        Ok(())
157    }
158
159    async fn set_encoding(jvm: &Jvm, _: &mut RuntimeContext, mut this: ClassInstanceRef<Self>, encoding: ClassInstanceRef<String>) -> Result<()> {
160        tracing::debug!("java.util.logging.Handler::setEncoding({this:?}, {encoding:?})");
161
162        if !encoding.is_null() {
163            OutputStreamWriter::validate_encoding(jvm, &encoding).await?;
164        }
165        jvm.put_field(&mut this, "encoding", "Ljava/lang/String;", encoding).await
166    }
167
168    async fn set_filter(jvm: &Jvm, _: &mut RuntimeContext, mut this: ClassInstanceRef<Self>, filter: ClassInstanceRef<Filter>) -> Result<()> {
169        tracing::debug!("java.util.logging.Handler::setFilter({this:?}, {filter:?})");
170
171        jvm.put_field(&mut this, "filter", "Ljava/util/logging/Filter;", filter).await
172    }
173
174    async fn set_formatter(
175        jvm: &Jvm,
176        _: &mut RuntimeContext,
177        mut this: ClassInstanceRef<Self>,
178        formatter: ClassInstanceRef<Formatter>,
179    ) -> Result<()> {
180        tracing::debug!("java.util.logging.Handler::setFormatter({this:?}, {formatter:?})");
181
182        if formatter.is_null() {
183            return Err(jvm.exception("java/lang/NullPointerException", "formatter").await);
184        }
185        jvm.put_field(&mut this, "formatter", "Ljava/util/logging/Formatter;", formatter).await
186    }
187
188    async fn set_level(jvm: &Jvm, _: &mut RuntimeContext, mut this: ClassInstanceRef<Self>, level: ClassInstanceRef<Level>) -> Result<()> {
189        tracing::debug!("java.util.logging.Handler::setLevel({this:?}, {level:?})");
190
191        if level.is_null() {
192            return Err(jvm.exception("java/lang/NullPointerException", "level").await);
193        }
194        jvm.put_field(&mut this, "level", "Ljava/util/logging/Level;", level).await
195    }
196}