@@ -387,73 +387,92 @@ JLI_Launch(int argc, char ** argv, /* main argc, argv */
387387 } \
388388 } while (JNI_FALSE)
389389
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-
399390/*
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.
402394 */
403395int
404396invokeStaticMainWithArgs (JNIEnv * env , jclass mainClass , jobjectArray mainArgs ) {
405397 jmethodID mainID = (* env )-> GetStaticMethodID (env , mainClass , "main" ,
406398 "([Ljava/lang/String;)V" );
407- CHECK_EXCEPTION_NULL_FAIL (mainID );
399+ if (mainID == NULL ) {
400+ // static main(String[]) not found
401+ return 0 ;
402+ }
408403 (* env )-> CallStaticVoidMethod (env , mainClass , mainID , mainArgs );
409- return 1 ;
404+ return 1 ; // method was invoked
410405}
411406
412407/*
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.
415411 */
416412int
417413invokeInstanceMainWithArgs (JNIEnv * env , jclass mainClass , jobjectArray mainArgs ) {
418414 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+ }
420419 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+ }
422424 jmethodID mainID =
423425 (* 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+ }
425430 (* env )-> CallVoidMethod (env , mainObject , mainID , mainArgs );
426- return 1 ;
431+ return 1 ; // method was invoked
427432}
428433
429434/*
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.
432438 */
433439int
434440invokeStaticMainWithoutArgs (JNIEnv * env , jclass mainClass ) {
435441 jmethodID mainID = (* env )-> GetStaticMethodID (env , mainClass , "main" ,
436442 "()V" );
437- CHECK_EXCEPTION_NULL_FAIL (mainID );
443+ if (mainID == NULL ) {
444+ // static main() method couldn't be located
445+ return 0 ;
446+ }
438447 (* env )-> CallStaticVoidMethod (env , mainClass , mainID );
439- return 1 ;
448+ return 1 ; // method was invoked
440449}
441450
442451/*
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.
445455 */
446456int
447457invokeInstanceMainWithoutArgs (JNIEnv * env , jclass mainClass ) {
448458 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+ }
450463 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+ }
452468 jmethodID mainID = (* env )-> GetMethodID (env , mainClass , "main" ,
453469 "()V" );
454- CHECK_EXCEPTION_NULL_FAIL (mainID );
470+ if (mainID == NULL ) {
471+ // instance method main() not found
472+ return 0 ;
473+ }
455474 (* env )-> CallVoidMethod (env , mainObject , mainID );
456- return 1 ;
475+ return 1 ; // method was invoked
457476}
458477
459478int
@@ -639,15 +658,24 @@ JavaMain(void* _args)
639658 }
640659 }
641660 if (!ret ) {
661+ // An appropriate main method couldn't be located, check and report
662+ // any exception and LEAVE()
642663 CHECK_EXCEPTION_LEAVE (1 );
643664 }
644665
645666 /*
646667 * The launcher's exit code (in the absence of calls to
647668 * System.exit) will be non-zero if main threw an exception.
648669 */
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+ }
651679 LEAVE ();
652680}
653681
0 commit comments