toad_jni/java/lang/
object.rs1use std::ops::Deref;
2
3use java::ResultExt;
4use jni::objects::{GlobalRef, JObject, JValueGen};
5
6use crate::java;
7
8pub struct Object(GlobalRef);
13
14impl Object {
15 pub fn is_instance_of<T>(&self, e: &mut java::Env) -> bool
17 where T: java::Type
18 {
19 T::is_type_of(e, self)
20 }
21
22 pub fn upcast_to<T>(self, e: &mut java::Env) -> T
24 where T: java::Object
25 {
26 T::upcast(e, self)
27 }
28
29 pub fn new_reference(&self, e: &mut java::Env) -> Self {
31 Self(e.new_global_ref(self.as_local()).unwrap_java(e))
32 }
33
34 pub fn to_string(&self, e: &mut java::Env) -> String {
36 static TO_STRING: java::Method<Object, fn() -> String> = java::Method::new("toString");
37 TO_STRING.invoke(e, self)
38 }
39
40 pub fn equals(&self, e: &mut java::Env, other: &Object) -> bool {
42 static EQUALS: java::Method<Object, fn(Object) -> bool> = java::Method::new("equals");
43 let other = other.new_reference(e);
44 EQUALS.invoke(e, self, other)
45 }
46
47 pub fn to_value<'a>(&self, e: &mut java::Env<'a>) -> JValueGen<JObject<'a>> {
49 JValueGen::Object(self.to_local(e))
50 }
51
52 pub fn to_local<'a>(&self, e: &mut java::Env<'a>) -> JObject<'a> {
54 e.new_local_ref(&self.0).unwrap_java(e)
55 }
56
57 pub fn into_global(self) -> GlobalRef {
59 self.0
60 }
61
62 pub fn from_value<'a, 'b>(e: &mut java::Env<'a>, jv: JValueGen<JObject<'b>>) -> Self
64 where 'a: 'b
65 {
66 Self::from_local(e, jv.l().unwrap())
67 }
68
69 pub fn from_value_ref<'a, 'b, 'c>(e: &mut java::Env<'a>, jv: JValueGen<&'c JObject<'b>>) -> Self
71 where 'a: 'b,
72 'c: 'b
73 {
74 Self::from_local(e, jv.l().unwrap())
75 }
76
77 pub fn from_local<'a, 'b, T>(e: &mut java::Env<'a>, t: T) -> Self
79 where 'a: 'b,
80 T: AsRef<JObject<'b>>
81 {
82 Self(e.new_global_ref(t.as_ref()).unwrap_java(e))
83 }
84
85 pub fn from_global(t: GlobalRef) -> Self {
87 Self(t)
88 }
89
90 pub fn as_value(&self) -> JValueGen<&JObject<'static>> {
92 JValueGen::Object(self.as_local())
93 }
94
95 pub fn as_local(&self) -> &JObject<'static> {
97 self.0.as_obj()
98 }
99
100 pub fn as_global(&self) -> &GlobalRef {
102 &self.0
103 }
104}
105
106impl java::Class for Object {
107 const PATH: &'static str = "java/lang/Object";
108}
109
110impl java::Object for Object {
111 fn upcast(_: &mut java::Env, jobj: java::lang::Object) -> Self {
112 jobj
113 }
114
115 fn downcast(self, _: &mut java::Env) -> java::lang::Object {
116 self
117 }
118
119 fn downcast_ref(&self, e: &mut java::Env) -> java::lang::Object {
120 Self::from_local(e, self.as_local())
121 }
122
123 fn upcast_value_ref<'e>(e: &mut java::Env<'e>, jv: jni::objects::JValue<'e, '_>) -> Self
124 where Self: Sized
125 {
126 Self::from_value_ref(e, jv)
127 }
128
129 fn upcast_value<'e>(e: &mut java::Env<'e>, jv: jni::objects::JValueOwned<'e>) -> Self
130 where Self: Sized
131 {
132 Self::from_value(e, jv)
133 }
134
135 fn downcast_value<'e>(self, e: &mut java::Env<'e>) -> jni::objects::JValueOwned<'e>
136 where Self: Sized
137 {
138 self.to_value(e)
139 }
140
141 fn yield_to_java(&self, e: &mut java::Env) -> jni::sys::jobject {
142 self.to_local(e).as_raw()
143 }
144}
145
146impl From<GlobalRef> for Object {
147 fn from(value: GlobalRef) -> Self {
148 Object::from_global(value)
149 }
150}
151
152impl From<Object> for GlobalRef {
153 fn from(value: Object) -> Self {
154 value.into_global()
155 }
156}
157
158impl AsRef<JObject<'static>> for Object {
159 fn as_ref(&self) -> &JObject<'static> {
160 self.as_local()
161 }
162}
163
164impl Deref for Object {
165 type Target = JObject<'static>;
166
167 fn deref(&self) -> &Self::Target {
168 self.as_ref()
169 }
170}