Skip to content

Commit

Permalink
Let subclasses invoke the protected VertxApplication constructor with…
Browse files Browse the repository at this point in the history
… a null hooks param. (#13)

Otherwise, a subclass cannot extend VertxApplicationHooks and control advanced parameters like exitOnFailure.

Signed-off-by: Thomas Segismont <[email protected]>
  • Loading branch information
tsegismont authored Nov 6, 2024
1 parent b89999e commit d41e018
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand All @@ -73,15 +65,25 @@ public VertxApplication(String[] args, VertxApplicationHooks hooks) {

/**
* May be invoked by subclasses to customize behavior.
* <p>
* 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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
*/
public interface VertxApplicationHooks {

VertxApplicationHooks DEFAULT = new VertxApplicationHooks() {
};

/**
* Invoked before starting Vert.x.
* <p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit d41e018

Please sign in to comment.