12
12
// See the License for the specific language governing permissions and
13
13
// limitations under the License.
14
14
15
- use crate :: api :: { CLASS_BOOLEAN , CLASS_BYTE , CLASS_CHARACTER , CLASS_DOUBLE , CLASS_FLOAT , CLASS_INTEGER , CLASS_LIST , CLASS_LONG , CLASS_SHORT , CLASS_STRING , Jvm , Null } ;
15
+ use std :: any :: Any ;
16
16
use std:: convert:: TryFrom ;
17
+ use std:: ptr;
18
+
19
+ use jni_sys:: { JNIEnv , jobject} ;
17
20
use serde:: Serialize ;
18
21
use serde_json;
19
- use std:: any:: Any ;
20
- use jni_sys:: { JNIEnv , jobject} ;
21
- use std:: ptr;
22
+
22
23
use crate :: { cache, errors, jni_utils, utils} ;
24
+ use crate :: api:: { JavaClass , Jvm , Null } ;
23
25
use crate :: api:: instance:: Instance ;
24
26
25
27
/// Struct that carries an argument that is used for method invocations in Java.
@@ -177,19 +179,19 @@ impl InvocationArg {
177
179
178
180
/// Creates an InvocationArg that contains null
179
181
pub fn create_null ( null : Null ) -> errors:: Result < InvocationArg > {
180
- let class_name = match null {
181
- Null :: String => CLASS_STRING ,
182
- Null :: Boolean => CLASS_BOOLEAN ,
183
- Null :: Byte => CLASS_BYTE ,
184
- Null :: Character => CLASS_CHARACTER ,
185
- Null :: Short => CLASS_SHORT ,
186
- Null :: Integer => CLASS_INTEGER ,
187
- Null :: Long => CLASS_LONG ,
188
- Null :: Float => CLASS_FLOAT ,
189
- Null :: Double => CLASS_DOUBLE ,
190
- Null :: List => CLASS_LIST ,
191
- Null :: Of ( class_name) => class_name,
192
- } ;
182
+ let class_name: & str = match null {
183
+ Null :: String => JavaClass :: String ,
184
+ Null :: Boolean => JavaClass :: Boolean ,
185
+ Null :: Byte => JavaClass :: Byte ,
186
+ Null :: Character => JavaClass :: Character ,
187
+ Null :: Short => JavaClass :: Short ,
188
+ Null :: Integer => JavaClass :: Integer ,
189
+ Null :: Long => JavaClass :: Long ,
190
+ Null :: Float => JavaClass :: Float ,
191
+ Null :: Double => JavaClass :: Double ,
192
+ Null :: List => JavaClass :: List ,
193
+ Null :: Of ( class_name) => JavaClass :: Of ( class_name) ,
194
+ } . into ( ) ;
193
195
Ok ( InvocationArg :: RustBasic {
194
196
instance : Instance :: new ( ptr:: null_mut ( ) , class_name) ?,
195
197
class_name : class_name. to_string ( ) ,
@@ -220,7 +222,7 @@ impl<'a> TryFrom<Null<'a>> for InvocationArg {
220
222
impl TryFrom < String > for InvocationArg {
221
223
type Error = errors:: J4RsError ;
222
224
fn try_from ( arg : String ) -> errors:: Result < InvocationArg > {
223
- InvocationArg :: new_2 ( & arg, CLASS_STRING , cache:: get_thread_local_env ( ) ?)
225
+ InvocationArg :: new_2 ( & arg, JavaClass :: String . into ( ) , cache:: get_thread_local_env ( ) ?)
224
226
}
225
227
}
226
228
@@ -236,7 +238,7 @@ impl<'a> TryFrom<&'a [String]> for InvocationArg {
236
238
impl < ' a > TryFrom < & ' a str > for InvocationArg {
237
239
type Error = errors:: J4RsError ;
238
240
fn try_from ( arg : & ' a str ) -> errors:: Result < InvocationArg > {
239
- InvocationArg :: new_2 ( & arg. to_string ( ) , CLASS_STRING , cache:: get_thread_local_env ( ) ?)
241
+ InvocationArg :: new_2 ( & arg. to_string ( ) , JavaClass :: String . into ( ) , cache:: get_thread_local_env ( ) ?)
240
242
}
241
243
}
242
244
@@ -252,7 +254,7 @@ impl<'a> TryFrom<&'a [&'a str]> for InvocationArg {
252
254
impl TryFrom < bool > for InvocationArg {
253
255
type Error = errors:: J4RsError ;
254
256
fn try_from ( arg : bool ) -> errors:: Result < InvocationArg > {
255
- InvocationArg :: new_2 ( & arg, CLASS_BOOLEAN , cache:: get_thread_local_env ( ) ?)
257
+ InvocationArg :: new_2 ( & arg, JavaClass :: Boolean . into ( ) , cache:: get_thread_local_env ( ) ?)
256
258
}
257
259
}
258
260
@@ -268,7 +270,7 @@ impl<'a> TryFrom<&'a [bool]> for InvocationArg {
268
270
impl TryFrom < i8 > for InvocationArg {
269
271
type Error = errors:: J4RsError ;
270
272
fn try_from ( arg : i8 ) -> errors:: Result < InvocationArg > {
271
- InvocationArg :: new_2 ( & arg, CLASS_BYTE , cache:: get_thread_local_env ( ) ?)
273
+ InvocationArg :: new_2 ( & arg, JavaClass :: Byte . into ( ) , cache:: get_thread_local_env ( ) ?)
272
274
}
273
275
}
274
276
@@ -284,7 +286,7 @@ impl<'a> TryFrom<&'a [i8]> for InvocationArg {
284
286
impl TryFrom < char > for InvocationArg {
285
287
type Error = errors:: J4RsError ;
286
288
fn try_from ( arg : char ) -> errors:: Result < InvocationArg > {
287
- InvocationArg :: new_2 ( & arg, CLASS_CHARACTER , cache:: get_thread_local_env ( ) ?)
289
+ InvocationArg :: new_2 ( & arg, JavaClass :: Character . into ( ) , cache:: get_thread_local_env ( ) ?)
288
290
}
289
291
}
290
292
@@ -300,7 +302,7 @@ impl<'a> TryFrom<&'a [char]> for InvocationArg {
300
302
impl TryFrom < i16 > for InvocationArg {
301
303
type Error = errors:: J4RsError ;
302
304
fn try_from ( arg : i16 ) -> errors:: Result < InvocationArg > {
303
- InvocationArg :: new_2 ( & arg, CLASS_SHORT , cache:: get_thread_local_env ( ) ?)
305
+ InvocationArg :: new_2 ( & arg, JavaClass :: Short . into ( ) , cache:: get_thread_local_env ( ) ?)
304
306
}
305
307
}
306
308
@@ -316,7 +318,7 @@ impl<'a> TryFrom<&'a [i16]> for InvocationArg {
316
318
impl TryFrom < i32 > for InvocationArg {
317
319
type Error = errors:: J4RsError ;
318
320
fn try_from ( arg : i32 ) -> errors:: Result < InvocationArg > {
319
- InvocationArg :: new_2 ( & arg, CLASS_INTEGER , cache:: get_thread_local_env ( ) ?)
321
+ InvocationArg :: new_2 ( & arg, JavaClass :: Integer . into ( ) , cache:: get_thread_local_env ( ) ?)
320
322
}
321
323
}
322
324
@@ -332,7 +334,7 @@ impl<'a> TryFrom<&'a [i32]> for InvocationArg {
332
334
impl TryFrom < i64 > for InvocationArg {
333
335
type Error = errors:: J4RsError ;
334
336
fn try_from ( arg : i64 ) -> errors:: Result < InvocationArg > {
335
- InvocationArg :: new_2 ( & arg, CLASS_LONG , cache:: get_thread_local_env ( ) ?)
337
+ InvocationArg :: new_2 ( & arg, JavaClass :: Long . into ( ) , cache:: get_thread_local_env ( ) ?)
336
338
}
337
339
}
338
340
@@ -348,7 +350,7 @@ impl<'a> TryFrom<&'a [i64]> for InvocationArg {
348
350
impl TryFrom < f32 > for InvocationArg {
349
351
type Error = errors:: J4RsError ;
350
352
fn try_from ( arg : f32 ) -> errors:: Result < InvocationArg > {
351
- InvocationArg :: new_2 ( & arg, CLASS_FLOAT , cache:: get_thread_local_env ( ) ?)
353
+ InvocationArg :: new_2 ( & arg, JavaClass :: Float . into ( ) , cache:: get_thread_local_env ( ) ?)
352
354
}
353
355
}
354
356
@@ -364,7 +366,7 @@ impl<'a> TryFrom<&'a [f32]> for InvocationArg {
364
366
impl TryFrom < f64 > for InvocationArg {
365
367
type Error = errors:: J4RsError ;
366
368
fn try_from ( arg : f64 ) -> errors:: Result < InvocationArg > {
367
- InvocationArg :: new_2 ( & arg, CLASS_DOUBLE , cache:: get_thread_local_env ( ) ?)
369
+ InvocationArg :: new_2 ( & arg, JavaClass :: Double . into ( ) , cache:: get_thread_local_env ( ) ?)
368
370
}
369
371
}
370
372
@@ -377,91 +379,94 @@ impl<'a> TryFrom<&'a [f64]> for InvocationArg {
377
379
}
378
380
}
379
381
380
- impl < ' a , T : ' static > TryFrom < ( & ' a [ T ] , & ' a str ) > for InvocationArg where T : Serialize {
381
- type Error = errors:: J4RsError ;
382
- fn try_from ( vec : ( & ' a [ T ] , & ' a str ) ) -> errors:: Result < InvocationArg > {
383
- let ( vec, elements_class_name) = vec;
384
- let jni_env = cache:: get_thread_local_env ( ) ?;
385
- let args: errors:: Result < Vec < InvocationArg > > = vec. iter ( ) . map ( |elem| InvocationArg :: new_2 ( elem, elements_class_name, jni_env) ) . collect ( ) ;
386
- let res = Jvm :: do_create_java_list ( cache:: get_thread_local_env ( ) ?, cache:: J4RS_ARRAY , & args?) ;
387
- Ok ( InvocationArg :: from ( res?) )
388
- }
389
- }
390
-
391
382
impl TryFrom < ( ) > for InvocationArg {
392
383
type Error = errors:: J4RsError ;
393
384
fn try_from ( arg : ( ) ) -> errors:: Result < InvocationArg > {
394
- InvocationArg :: new_2 ( & arg, "void" , cache:: get_thread_local_env ( ) ?)
385
+ InvocationArg :: new_2 ( & arg, JavaClass :: Void . into ( ) , cache:: get_thread_local_env ( ) ?)
395
386
}
396
387
}
397
388
398
389
impl < ' a > TryFrom < & ' a String > for InvocationArg {
399
390
type Error = errors:: J4RsError ;
400
391
fn try_from ( arg : & ' a String ) -> errors:: Result < InvocationArg > {
401
- InvocationArg :: new_2 ( arg, CLASS_STRING , cache:: get_thread_local_env ( ) ?)
392
+ InvocationArg :: new_2 ( arg, JavaClass :: String . into ( ) , cache:: get_thread_local_env ( ) ?)
402
393
}
403
394
}
404
395
405
396
impl < ' a > TryFrom < & ' a bool > for InvocationArg {
406
397
type Error = errors:: J4RsError ;
407
398
fn try_from ( arg : & ' a bool ) -> errors:: Result < InvocationArg > {
408
- InvocationArg :: new_2 ( arg, CLASS_BOOLEAN , cache:: get_thread_local_env ( ) ?)
399
+ InvocationArg :: new_2 ( arg, JavaClass :: Boolean . into ( ) , cache:: get_thread_local_env ( ) ?)
409
400
}
410
401
}
411
402
412
403
impl < ' a > TryFrom < & ' a i8 > for InvocationArg {
413
404
type Error = errors:: J4RsError ;
414
405
fn try_from ( arg : & ' a i8 ) -> errors:: Result < InvocationArg > {
415
- InvocationArg :: new_2 ( arg, CLASS_BYTE , cache:: get_thread_local_env ( ) ?)
406
+ InvocationArg :: new_2 ( arg, JavaClass :: Byte . into ( ) , cache:: get_thread_local_env ( ) ?)
416
407
}
417
408
}
418
409
419
410
impl < ' a > TryFrom < & ' a char > for InvocationArg {
420
411
type Error = errors:: J4RsError ;
421
412
fn try_from ( arg : & ' a char ) -> errors:: Result < InvocationArg > {
422
- InvocationArg :: new_2 ( arg, CLASS_CHARACTER , cache:: get_thread_local_env ( ) ?)
413
+ InvocationArg :: new_2 ( arg, JavaClass :: Character . into ( ) , cache:: get_thread_local_env ( ) ?)
423
414
}
424
415
}
425
416
426
417
impl < ' a > TryFrom < & ' a i16 > for InvocationArg {
427
418
type Error = errors:: J4RsError ;
428
419
fn try_from ( arg : & ' a i16 ) -> errors:: Result < InvocationArg > {
429
- InvocationArg :: new_2 ( arg, CLASS_SHORT , cache:: get_thread_local_env ( ) ?)
420
+ InvocationArg :: new_2 ( arg, JavaClass :: Short . into ( ) , cache:: get_thread_local_env ( ) ?)
430
421
}
431
422
}
432
423
433
424
impl < ' a , ' b > TryFrom < & ' a i32 > for InvocationArg {
434
425
type Error = errors:: J4RsError ;
435
426
fn try_from ( arg : & ' a i32 ) -> errors:: Result < InvocationArg > {
436
- InvocationArg :: new_2 ( arg, CLASS_INTEGER , cache:: get_thread_local_env ( ) ?)
427
+ InvocationArg :: new_2 ( arg, JavaClass :: Integer . into ( ) , cache:: get_thread_local_env ( ) ?)
437
428
}
438
429
}
439
430
440
431
impl < ' a > TryFrom < & ' a i64 > for InvocationArg {
441
432
type Error = errors:: J4RsError ;
442
433
fn try_from ( arg : & ' a i64 ) -> errors:: Result < InvocationArg > {
443
- InvocationArg :: new_2 ( arg, CLASS_LONG , cache:: get_thread_local_env ( ) ?)
434
+ InvocationArg :: new_2 ( arg, JavaClass :: Long . into ( ) , cache:: get_thread_local_env ( ) ?)
444
435
}
445
436
}
446
437
447
438
impl < ' a > TryFrom < & ' a f32 > for InvocationArg {
448
439
type Error = errors:: J4RsError ;
449
440
fn try_from ( arg : & ' a f32 ) -> errors:: Result < InvocationArg > {
450
- InvocationArg :: new_2 ( arg, CLASS_FLOAT , cache:: get_thread_local_env ( ) ?)
441
+ InvocationArg :: new_2 ( arg, JavaClass :: Float . into ( ) , cache:: get_thread_local_env ( ) ?)
451
442
}
452
443
}
453
444
454
445
impl < ' a > TryFrom < & ' a f64 > for InvocationArg {
455
446
type Error = errors:: J4RsError ;
456
447
fn try_from ( arg : & ' a f64 ) -> errors:: Result < InvocationArg > {
457
- InvocationArg :: new_2 ( arg, CLASS_DOUBLE , cache:: get_thread_local_env ( ) ?)
448
+ InvocationArg :: new_2 ( arg, JavaClass :: Double . into ( ) , cache:: get_thread_local_env ( ) ?)
449
+ }
450
+ }
451
+
452
+ impl < ' a , T : ' static > TryFrom < ( & ' a [ T ] , & ' a str ) > for InvocationArg where T : Serialize {
453
+ type Error = errors:: J4RsError ;
454
+ fn try_from ( vec : ( & ' a [ T ] , & ' a str ) ) -> errors:: Result < InvocationArg > {
455
+ let ( vec, elements_class_name) = vec;
456
+ let jni_env = cache:: get_thread_local_env ( ) ?;
457
+ let args: errors:: Result < Vec < InvocationArg > > = vec. iter ( )
458
+ . map ( |elem| InvocationArg :: new_2 ( elem, JavaClass :: Of ( elements_class_name) . into ( ) , jni_env) ) . collect ( ) ;
459
+ let res = Jvm :: do_create_java_list ( cache:: get_thread_local_env ( ) ?, cache:: J4RS_ARRAY , & args?) ;
460
+ Ok ( InvocationArg :: from ( res?) )
458
461
}
459
462
}
460
463
461
464
#[ cfg( test) ]
462
465
mod inv_arg_unit_tests {
463
466
use serde:: Deserialize ;
467
+
464
468
use crate :: JvmBuilder ;
469
+
465
470
use super :: * ;
466
471
467
472
#[ test]
0 commit comments