@@ -387,73 +387,92 @@ JLI_Launch(int argc, char ** argv, /* main argc, argv */
387
387
} \
388
388
} while (JNI_FALSE)
389
389
390
- #define CHECK_EXCEPTION_NULL_FAIL (obj ) \
391
- do { \
392
- if ((*env)->ExceptionOccurred(env)) { \
393
- return 0; \
394
- } else if (obj == NULL) { \
395
- return 0; \
396
- } \
397
- } while (JNI_FALSE)
398
-
399
390
/*
400
- * Invoke a static main with arguments. Returns 1 (true) if successful otherwise
401
- * processes the pending exception from GetStaticMethodID and returns 0 (false).
391
+ * Invokes static main(String[]) method if found.
392
+ * Returns 0 with a pending exception if not found. Returns 1 if invoked, maybe
393
+ * a pending exception if the method threw.
402
394
*/
403
395
int
404
396
invokeStaticMainWithArgs (JNIEnv * env , jclass mainClass , jobjectArray mainArgs ) {
405
397
jmethodID mainID = (* env )-> GetStaticMethodID (env , mainClass , "main" ,
406
398
"([Ljava/lang/String;)V" );
407
- CHECK_EXCEPTION_NULL_FAIL (mainID );
399
+ if (mainID == NULL ) {
400
+ // static main(String[]) not found
401
+ return 0 ;
402
+ }
408
403
(* env )-> CallStaticVoidMethod (env , mainClass , mainID , mainArgs );
409
- return 1 ;
404
+ return 1 ; // method was invoked
410
405
}
411
406
412
407
/*
413
- * Invoke an instance main with arguments. Returns 1 (true) if successful otherwise
414
- * processes the pending exception from GetMethodID and returns 0 (false).
408
+ * Invokes instance main(String[]) method if found.
409
+ * Returns 0 with a pending exception if not found. Returns 1 if invoked, maybe
410
+ * a pending exception if the method threw.
415
411
*/
416
412
int
417
413
invokeInstanceMainWithArgs (JNIEnv * env , jclass mainClass , jobjectArray mainArgs ) {
418
414
jmethodID constructor = (* env )-> GetMethodID (env , mainClass , "<init>" , "()V" );
419
- CHECK_EXCEPTION_NULL_FAIL (constructor );
415
+ if (constructor == NULL ) {
416
+ // main class' no-arg constructor not found
417
+ return 0 ;
418
+ }
420
419
jobject mainObject = (* env )-> NewObject (env , mainClass , constructor );
421
- CHECK_EXCEPTION_NULL_FAIL (mainObject );
420
+ if (mainObject == NULL ) {
421
+ // main class instance couldn't be constructed
422
+ return 0 ;
423
+ }
422
424
jmethodID mainID =
423
425
(* env )-> GetMethodID (env , mainClass , "main" , "([Ljava/lang/String;)V" );
424
- CHECK_EXCEPTION_NULL_FAIL (mainID );
426
+ if (mainID == NULL ) {
427
+ // instance method main(String[]) method not found
428
+ return 0 ;
429
+ }
425
430
(* env )-> CallVoidMethod (env , mainObject , mainID , mainArgs );
426
- return 1 ;
431
+ return 1 ; // method was invoked
427
432
}
428
433
429
434
/*
430
- * Invoke a static main without arguments. Returns 1 (true) if successful otherwise
431
- * processes the pending exception from GetStaticMethodID and returns 0 (false).
435
+ * Invokes no-arg static main() method if found.
436
+ * Returns 0 with a pending exception if not found. Returns 1 if invoked, maybe
437
+ * a pending exception if the method threw.
432
438
*/
433
439
int
434
440
invokeStaticMainWithoutArgs (JNIEnv * env , jclass mainClass ) {
435
441
jmethodID mainID = (* env )-> GetStaticMethodID (env , mainClass , "main" ,
436
442
"()V" );
437
- CHECK_EXCEPTION_NULL_FAIL (mainID );
443
+ if (mainID == NULL ) {
444
+ // static main() method couldn't be located
445
+ return 0 ;
446
+ }
438
447
(* env )-> CallStaticVoidMethod (env , mainClass , mainID );
439
- return 1 ;
448
+ return 1 ; // method was invoked
440
449
}
441
450
442
451
/*
443
- * Invoke an instance main without arguments. Returns 1 (true) if successful otherwise
444
- * processes the pending exception from GetMethodID and returns 0 (false).
452
+ * Invokes no-arg instance main() method if found.
453
+ * Returns 0 with a pending exception if not found. Returns 1 if invoked, maybe
454
+ * a pending exception if the method threw.
445
455
*/
446
456
int
447
457
invokeInstanceMainWithoutArgs (JNIEnv * env , jclass mainClass ) {
448
458
jmethodID constructor = (* env )-> GetMethodID (env , mainClass , "<init>" , "()V" );
449
- CHECK_EXCEPTION_NULL_FAIL (constructor );
459
+ if (constructor == NULL ) {
460
+ // main class' no-arg constructor not found
461
+ return 0 ;
462
+ }
450
463
jobject mainObject = (* env )-> NewObject (env , mainClass , constructor );
451
- CHECK_EXCEPTION_NULL_FAIL (mainObject );
464
+ if (mainObject == NULL ) {
465
+ // couldn't create instance of main class
466
+ return 0 ;
467
+ }
452
468
jmethodID mainID = (* env )-> GetMethodID (env , mainClass , "main" ,
453
469
"()V" );
454
- CHECK_EXCEPTION_NULL_FAIL (mainID );
470
+ if (mainID == NULL ) {
471
+ // instance method main() not found
472
+ return 0 ;
473
+ }
455
474
(* env )-> CallVoidMethod (env , mainObject , mainID );
456
- return 1 ;
475
+ return 1 ; // method was invoked
457
476
}
458
477
459
478
int
@@ -639,15 +658,24 @@ JavaMain(void* _args)
639
658
}
640
659
}
641
660
if (!ret ) {
661
+ // An appropriate main method couldn't be located, check and report
662
+ // any exception and LEAVE()
642
663
CHECK_EXCEPTION_LEAVE (1 );
643
664
}
644
665
645
666
/*
646
667
* The launcher's exit code (in the absence of calls to
647
668
* System.exit) will be non-zero if main threw an exception.
648
669
*/
649
- ret = (* env )-> ExceptionOccurred (env ) == NULL ? 0 : 1 ;
650
-
670
+ if (ret && (* env )-> ExceptionOccurred (env ) == NULL ) {
671
+ // main method was invoked and no exception was thrown from it,
672
+ // return success.
673
+ ret = 0 ;
674
+ } else {
675
+ // Either the main method couldn't be located or an exception occurred
676
+ // in the invoked main method, return failure.
677
+ ret = 1 ;
678
+ }
651
679
LEAVE ();
652
680
}
653
681
0 commit comments