forked from quarkusio/quarkus
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support Java 21 main launch protocol in prod and dev mode
Relates to: quarkusio#36154
- Loading branch information
Showing
3 changed files
with
83 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
...rojects/bootstrap/runner/src/main/java/io/quarkus/bootstrap/runner/MainMethodInvoker.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package io.quarkus.bootstrap.runner; | ||
|
||
import java.lang.reflect.InvocationTargetException; | ||
import java.lang.reflect.Method; | ||
import java.lang.reflect.Modifier; | ||
|
||
public final class MainMethodInvoker { | ||
|
||
// follow the rules outlines in https://openjdk.org/jeps/445 section 'Selecting a main method' | ||
public static void invoke(Class<?> mainClass, Object args) | ||
throws InvocationTargetException, IllegalAccessException, NoSuchMethodException, InstantiationException { | ||
|
||
// public static void main(String[] args) | ||
Method mainWithArgs = null; | ||
try { | ||
mainWithArgs = mainClass.getDeclaredMethod("main", String[].class); | ||
int modifiers = mainWithArgs.getModifiers(); | ||
if (Modifier.isStatic(modifiers) && !Modifier.isPrivate(modifiers)) { | ||
if (!Modifier.isPublic(modifiers)) { | ||
mainWithArgs.setAccessible(true); | ||
} | ||
mainWithArgs.invoke(null, args); | ||
return; | ||
} | ||
} catch (NoSuchMethodException ignored) { | ||
|
||
} | ||
|
||
// public static void main() | ||
Method mainWithoutArgs = null; | ||
try { | ||
mainWithoutArgs = mainClass.getDeclaredMethod("main"); | ||
int modifiers = mainWithoutArgs.getModifiers(); | ||
if (Modifier.isStatic(modifiers) && !Modifier.isPrivate(modifiers)) { | ||
if (!Modifier.isPublic(modifiers)) { | ||
mainWithoutArgs.setAccessible(true); | ||
} | ||
mainWithoutArgs.invoke(null); | ||
return; | ||
} | ||
} catch (NoSuchMethodException ignored) { | ||
|
||
} | ||
|
||
var instance = mainClass.getConstructor().newInstance(); | ||
|
||
// public void main(String[] args) | ||
if (mainWithArgs != null) { | ||
int modifiers = mainWithArgs.getModifiers(); | ||
if (!Modifier.isPrivate(modifiers)) { | ||
if (!Modifier.isPublic(modifiers)) { | ||
mainWithArgs.setAccessible(true); | ||
} | ||
mainWithArgs.invoke(instance, args); | ||
return; | ||
} | ||
} | ||
|
||
// public void main() | ||
if (mainWithoutArgs != null) { | ||
int modifiers = mainWithoutArgs.getModifiers(); | ||
if (!Modifier.isPrivate(modifiers)) { | ||
if (!Modifier.isPublic(modifiers)) { | ||
mainWithoutArgs.setAccessible(true); | ||
} | ||
mainWithoutArgs.invoke(instance); | ||
return; | ||
} | ||
} | ||
|
||
throw new NoSuchMethodException("Unable to find main method"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters