Skip to content

Commit

Permalink
Use MethodHandle to invoke the generated Application class
Browse files Browse the repository at this point in the history
  • Loading branch information
geoand committed Dec 20, 2024
1 parent 7a0a0f9 commit 38d776a
Showing 1 changed file with 20 additions and 8 deletions.
28 changes: 20 additions & 8 deletions core/runtime/src/main/java/io/quarkus/runtime/Quarkus.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

import java.io.Closeable;
import java.io.IOException;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.lang.reflect.UndeclaredThrowableException;
import java.util.Locale;
import java.util.function.BiConsumer;

Expand Down Expand Up @@ -67,18 +71,17 @@ public static void run(Class<? extends QuarkusApplication> quarkusApplication, B
//we already have an application, run it directly
Class<? extends Application> appClass = (Class<? extends Application>) Class.forName(Application.APP_CLASS_NAME,
false, Thread.currentThread().getContextClassLoader());
Application application = appClass.getDeclaredConstructor().newInstance();
MethodHandle constructor = MethodHandles.lookup().findConstructor(appClass, MethodType.methodType(void.class));
Application application = (Application) constructor.invoke();
ApplicationLifecycleManager.run(application, quarkusApplication, exitHandler, args);
return;
} catch (ClassNotFoundException e) {
//ignore, this happens when running in dev mode
} catch (Exception e) {
if (exitHandler != null) {
exitHandler.accept(1, e);
} else {
Logger.getLogger(Quarkus.class).error("Error running Quarkus", e);
ApplicationLifecycleManager.getDefaultExitCodeHandler().accept(1, e);
}
} catch (RuntimeException | Error e) {
handleReflectiveInvocationIssue(exitHandler, e);
return;
} catch (Throwable t) {
handleReflectiveInvocationIssue(exitHandler, new UndeclaredThrowableException(t));
return;
}

Expand All @@ -89,6 +92,15 @@ public static void run(Class<? extends QuarkusApplication> quarkusApplication, B

}

private static void handleReflectiveInvocationIssue(BiConsumer<Integer, Throwable> exitHandler, Throwable t) {
if (exitHandler != null) {
exitHandler.accept(1, t);
} else {
Logger.getLogger(Quarkus.class).error("Error running Quarkus", t);
ApplicationLifecycleManager.getDefaultExitCodeHandler().accept(1, t);
}
}

private static void launchFromIDE(Class<? extends QuarkusApplication> quarkusApplication, String... args) {
//some trickery, get the class that has invoked us, and use this to figure out the
//classes root
Expand Down

0 comments on commit 38d776a

Please sign in to comment.