Allow main(String[]) to be an instance method
This way commands can extend a base class, which provides a standard
main implementation, such as to launch a command line parser and then
invoke the real logic from some other method.
Optionally, main may now return a numeric value, which becomes the
JVM's exit code.
Signed-off-by: Shawn O. Pearce <sop@google.com>
diff --git a/pom.xml b/pom.xml
index 6827c3f..b368e1a 100644
--- a/pom.xml
+++ b/pom.xml
@@ -21,7 +21,7 @@
<groupId>gerrit</groupId>
<artifactId>executablewar</artifactId>
<packaging>jar</packaging>
- <version>1.0</version>
+ <version>1.1</version>
<name>executablewar</name>
<description>Support for running code directly from WAR files</description>
<url>http://android.git.kernel.org/?p=tools/executablewar.git</url>
diff --git a/src/main/java/ExecutableWarMain.java b/src/main/java/ExecutableWarMain.java
index 781787e..4d2e8fb 100644
--- a/src/main/java/ExecutableWarMain.java
+++ b/src/main/java/ExecutableWarMain.java
@@ -18,6 +18,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Method;
+import java.lang.reflect.Modifier;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
@@ -166,7 +167,17 @@
return;
}
- main.invoke(null, new Object[] {argv});
+ final Object res;
+ if ((main.getModifiers() & Modifier.STATIC) == Modifier.STATIC) {
+ res = main.invoke(null, new Object[] {argv});
+ } else {
+ res = main.invoke(clazz.newInstance(), new Object[] {argv});
+ }
+ if (res instanceof Number) {
+ System.exit(((Number) res).intValue());
+ } else {
+ System.exit(0);
+ }
}
private static Attributes myManifest() throws IOException,