From d41e018dcea5f12c61ab8c91e0838ab871de64e0 Mon Sep 17 00:00:00 2001 From: Thomas Segismont Date: Wed, 6 Nov 2024 11:48:42 +0100 Subject: [PATCH] Let subclasses invoke the protected VertxApplication constructor with a null hooks param. (#13) Otherwise, a subclass cannot extend VertxApplicationHooks and control advanced parameters like exitOnFailure. Signed-off-by: Thomas Segismont --- .../application/VertxApplication.java | 24 ++++++++++--------- .../application/VertxApplicationHooks.java | 3 +++ .../CustomApplicationLowMemoryTest.java | 2 +- 3 files changed, 17 insertions(+), 12 deletions(-) diff --git a/application/src/main/java/io/vertx/launcher/application/VertxApplication.java b/application/src/main/java/io/vertx/launcher/application/VertxApplication.java index a2256ff..204ed8c 100644 --- a/application/src/main/java/io/vertx/launcher/application/VertxApplication.java +++ b/application/src/main/java/io/vertx/launcher/application/VertxApplication.java @@ -50,15 +50,7 @@ public static void main(String[] args) { * @param args the program arguments */ public VertxApplication(String[] args) { - this.args = Objects.requireNonNull(args); - if (this instanceof VertxApplicationHooks) { - hooks = (VertxApplicationHooks) this; - } else { - hooks = new VertxApplicationHooks() { - }; - } - printUsageOnFailure = true; - exitOnFailure = true; + this(args, null); } /** @@ -73,15 +65,25 @@ public VertxApplication(String[] args, VertxApplicationHooks hooks) { /** * May be invoked by subclasses to customize behavior. + *

+ * When the {@code hooks} parameter is {@code null}, the application instance will be used if it implements {@link VertxApplicationHooks}. * * @param args the program arguments - * @param hooks an instance of {@link VertxApplicationHooks} to be invoked at different stages of the launch process + * @param hooks an instance of {@link VertxApplicationHooks} to be invoked at different stages of the launch process (maybe null) * @param printUsageOnFailure whether usage should be printed to {@link System#out} if the application failed to start * @param exitOnFailure whether the JVM should be terminated with a specific {@code exitCode} if the application failed to start */ protected VertxApplication(String[] args, VertxApplicationHooks hooks, boolean printUsageOnFailure, boolean exitOnFailure) { this.args = Objects.requireNonNull(args); - this.hooks = Objects.requireNonNull(hooks); + if (hooks == null) { + if (this instanceof VertxApplicationHooks) { + this.hooks = (VertxApplicationHooks) this; + } else { + this.hooks = VertxApplicationHooks.DEFAULT; + } + } else { + this.hooks = hooks; + } this.printUsageOnFailure = printUsageOnFailure; this.exitOnFailure = exitOnFailure; } diff --git a/application/src/main/java/io/vertx/launcher/application/VertxApplicationHooks.java b/application/src/main/java/io/vertx/launcher/application/VertxApplicationHooks.java index 75f1a15..a978b67 100644 --- a/application/src/main/java/io/vertx/launcher/application/VertxApplicationHooks.java +++ b/application/src/main/java/io/vertx/launcher/application/VertxApplicationHooks.java @@ -24,6 +24,9 @@ */ public interface VertxApplicationHooks { + VertxApplicationHooks DEFAULT = new VertxApplicationHooks() { + }; + /** * Invoked before starting Vert.x. *

diff --git a/application/src/test/java/io/vertx/launcher/application/CustomApplicationLowMemoryTest.java b/application/src/test/java/io/vertx/launcher/application/CustomApplicationLowMemoryTest.java index f9010ed..b05ac78 100644 --- a/application/src/test/java/io/vertx/launcher/application/CustomApplicationLowMemoryTest.java +++ b/application/src/test/java/io/vertx/launcher/application/CustomApplicationLowMemoryTest.java @@ -103,7 +103,7 @@ private boolean outputContainsMsgHook() { public static class MyVertxApplication extends VertxApplication implements VertxApplicationHooks { public MyVertxApplication(String[] args) { - super(args); + super(args, null, true, true); } public static void main(String[] args) {