|
64 | 64 | import com.oracle.svm.core.SubstrateOptions;
|
65 | 65 | import com.oracle.svm.core.option.HostedOptionKey;
|
66 | 66 | import com.oracle.svm.core.option.SubstrateOptionsParser;
|
| 67 | +import com.oracle.svm.core.util.BasedOnJDKFile; |
67 | 68 | import com.oracle.svm.core.util.ExitStatus;
|
68 | 69 | import com.oracle.svm.core.util.InterruptImageBuilding;
|
69 | 70 | import com.oracle.svm.core.util.UserError;
|
@@ -495,19 +496,9 @@ private int buildImage(ImageClassLoader classLoader) {
|
495 | 496 | * If no C-level main method was found, look for a Java-level main method
|
496 | 497 | * and use our wrapper to invoke it.
|
497 | 498 | */
|
498 |
| - if ("main".equals(mainEntryPointName) && JavaMainWrapper.instanceMainMethodSupported()) { |
499 |
| - // Instance main method only supported for "main" method name |
| 499 | + if ("main".equals(mainEntryPointName)) { |
500 | 500 | try {
|
501 |
| - /* |
502 |
| - * JDK-8306112: Implementation of JEP 445: Unnamed Classes and |
503 |
| - * Instance Main Methods (Preview) |
504 |
| - * |
505 |
| - * MainMethodFinder will perform all the necessary checks |
506 |
| - */ |
507 |
| - String mainMethodFinderClassName = "jdk.internal.misc.MethodFinder"; |
508 |
| - Class<?> mainMethodFinder = ReflectionUtil.lookupClass(false, mainMethodFinderClassName); |
509 |
| - Method findMainMethod = ReflectionUtil.lookupMethod(mainMethodFinder, "findMainMethod", Class.class); |
510 |
| - javaMainMethod = (Method) findMainMethod.invoke(null, mainClass); |
| 501 | + javaMainMethod = findDefaultJavaMainMethod(mainClass); |
511 | 502 | } catch (InvocationTargetException ex) {
|
512 | 503 | assert ex.getTargetException() instanceof NoSuchMethodException;
|
513 | 504 | throw UserError.abort(ex.getCause(),
|
@@ -618,6 +609,27 @@ private int buildImage(ImageClassLoader classLoader) {
|
618 | 609 | return ExitStatus.OK.getValue();
|
619 | 610 | }
|
620 | 611 |
|
| 612 | + /* |
| 613 | + * Finds the main method using the {@code jdk.internal.misc.MethodFinder}. |
| 614 | + * |
| 615 | + * The {@code MethodFinder} was introduced by JDK-8344706 (Implement JEP 512: Compact Source |
| 616 | + * Files and Instance Main Methods) and will perform all the necessary checks. |
| 617 | + */ |
| 618 | + @BasedOnJDKFile("https://github.com/openjdk/jdk/blob/jdk-25+24/src/java.base/share/classes/jdk/internal/misc/MethodFinder.java#L45-L106") |
| 619 | + private static Method findDefaultJavaMainMethod(Class<?> mainClass) throws IllegalAccessException, InvocationTargetException { |
| 620 | + Class<?> mainMethodFinder = ReflectionUtil.lookupClass(false, "jdk.internal.misc.MethodFinder"); |
| 621 | + Method findMainMethod = ReflectionUtil.lookupMethod(mainMethodFinder, "findMainMethod", Class.class); |
| 622 | + /* |
| 623 | + * We are using Method.invoke and throwing checked exceptions on purpose instead of |
| 624 | + * ReflectionUtil to issue a proper error message. |
| 625 | + */ |
| 626 | + Method invoke = (Method) findMainMethod.invoke(null, mainClass); |
| 627 | + /* |
| 628 | + * Use ReflectionUtil get a Method object with the right accessibility. |
| 629 | + */ |
| 630 | + return ReflectionUtil.lookupMethod(invoke.getDeclaringClass(), invoke.getName(), invoke.getParameterTypes()); |
| 631 | + } |
| 632 | + |
621 | 633 | private static void reportConflictingOptions(HostedOptionKey<Boolean> o1, HostedOptionKey<?> o2) {
|
622 | 634 | throw UserError.abort("Cannot pass both options: %s and %s", SubstrateOptionsParser.commandArgument(o1, "+"), SubstrateOptionsParser.commandArgument(o2, "+"));
|
623 | 635 | }
|
|
0 commit comments