@@ -184,22 +184,17 @@ impl<'a> Method<'a> {
184
184
185
185
writeln ! ( out) ?;
186
186
for reason in & emit_reject_reasons {
187
- writeln ! ( out, "{}// Not emitting: {}" , indent , reason ) ?;
187
+ writeln ! ( out, "{indent }// Not emitting: {reason}" ) ?;
188
188
}
189
189
if let Some ( url) = KnownDocsUrl :: from_method ( context, self ) {
190
- writeln ! ( out, "{}/// {}" , indent , url ) ?;
190
+ writeln ! ( out, "{indent }/// {url}" ) ?;
191
191
} else {
192
- writeln ! ( out, "{}/// {}" , indent , self . java. name( ) ) ?;
192
+ writeln ! ( out, "{indent }/// {}" , self . java. name( ) ) ?;
193
193
}
194
194
writeln ! (
195
195
out,
196
- "{}{}{}fn {}<'env>({}) -> ::std::result::Result<{}, ::java_spaghetti::Local<'env, {}>> {{" ,
197
- indent,
198
- attributes,
199
- access,
200
- method_name,
201
- params_decl,
202
- ret_decl,
196
+ "{indent}{attributes}{access}fn {method_name}<'env>({params_decl}) -> \
197
+ ::std::result::Result<{ret_decl}, ::java_spaghetti::Local<'env, {}>> {{",
203
198
context. throwable_rust_path( mod_)
204
199
) ?;
205
200
writeln ! (
@@ -211,43 +206,54 @@ impl<'a> Method<'a> {
211
206
self . java. name( ) ,
212
207
MethodSigWriter ( self . java. descriptor( ) )
213
208
) ?;
214
- writeln ! ( out, "{} unsafe {{" , indent) ?;
215
- writeln ! ( out, "{} let __jni_args = [{}];" , indent, params_array) ?;
209
+
210
+ writeln ! (
211
+ out,
212
+ "{indent} static __METHOD: ::std::sync::OnceLock<usize> = ::std::sync::OnceLock::new();"
213
+ ) ?;
214
+ writeln ! ( out, "{indent} unsafe {{" ) ?;
215
+ writeln ! ( out, "{indent} let __jni_args = [{params_array}];" ) ?;
216
216
if !self . java . is_constructor ( ) && !self . java . is_static ( ) {
217
- writeln ! ( out, "{} let __jni_env = self.env();" , indent ) ?;
217
+ writeln ! ( out, "{indent } let __jni_env = self.env();" ) ?;
218
218
}
219
-
220
219
writeln ! (
221
220
out,
222
- "{} let (__jni_class, __jni_method) = __jni_env.require_class_{}method({}, {}, {});" ,
223
- indent,
221
+ "{indent} let __jni_class = Self::__class_global_ref(__jni_env);"
222
+ ) ?;
223
+ writeln ! (
224
+ out,
225
+ "{indent} \
226
+ let __jni_method = *__METHOD.get_or_init(|| \
227
+ __jni_env.require_{}method(__jni_class, {}, {}).addr()\
228
+ ) as ::java_spaghetti::sys::jmethodID;",
224
229
if self . java. is_static( ) { "static_" } else { "" } ,
225
- StrEmitter ( self . class. path( ) . as_str( ) ) ,
226
230
StrEmitter ( self . java. name( ) ) ,
227
231
StrEmitter ( MethodSigWriter ( self . java. descriptor( ) ) )
228
232
) ?;
229
233
230
234
if self . java . is_constructor ( ) {
231
235
writeln ! (
232
236
out,
233
- "{} __jni_env.new_object_a(__jni_class, __jni_method, __jni_args.as_ptr())" ,
234
- indent
237
+ "{indent } \
238
+ __jni_env.new_object_a(__jni_class, __jni_method, __jni_args.as_ptr())" ,
235
239
) ?;
236
240
} else if self . java . is_static ( ) {
237
241
writeln ! (
238
242
out,
239
- "{} __jni_env.call_static_{}_method_a(__jni_class, __jni_method, __jni_args.as_ptr())" ,
240
- indent, ret_method_fragment
243
+ "{indent} \
244
+ __jni_env.call_static_{}_method_a(__jni_class, __jni_method, __jni_args.as_ptr())",
245
+ ret_method_fragment
241
246
) ?;
242
247
} else {
243
248
writeln ! (
244
249
out,
245
- "{} __jni_env.call_{}_method_a(self.as_raw(), __jni_method, __jni_args.as_ptr())" ,
246
- indent, ret_method_fragment
250
+ "{indent} \
251
+ __jni_env.call_{}_method_a(self.as_raw(), __jni_method, __jni_args.as_ptr())",
252
+ ret_method_fragment
247
253
) ?;
248
254
}
249
- writeln ! ( out, "{} }}" , indent ) ?;
250
- writeln ! ( out, "{}}}" , indent ) ?;
255
+ writeln ! ( out, "{indent } }}" ) ?;
256
+ writeln ! ( out, "{indent }}}" ) ?;
251
257
Ok ( ( ) )
252
258
}
253
259
}
0 commit comments