Skip to content

Commit 5be2535

Browse files
cayhorstmannslachiewicz
authored andcommitted
JSR 512 support
1 parent 374710c commit 5be2535

File tree

12 files changed

+362
-9
lines changed

12 files changed

+362
-9
lines changed

src/main/java/org/codehaus/mojo/exec/ExecJavaMojo.java

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -357,18 +357,48 @@ private void doExec(final String bootClassName) throws Throwable {
357357
Class<?> bootClass = Thread.currentThread().getContextClassLoader().loadClass(bootClassName);
358358
MethodHandles.Lookup lookup = MethodHandles.lookup();
359359
try {
360-
doMain(lookup.findStatic(bootClass, "main", MethodType.methodType(void.class, String[].class)));
361-
} catch (final NoSuchMethodException nsme) {
362-
if (Runnable.class.isAssignableFrom(bootClass)) {
363-
doRun(bootClass);
364-
} else {
365-
throw nsme;
366-
}
360+
MethodHandle mainHandle = lookup.findStatic(bootClass, "main", MethodType.methodType(void.class, String[].class));
361+
mainHandle.invoke(arguments);
362+
return;
363+
} catch (final NoSuchMethodException | IllegalAccessException e) {
364+
// No static main(String[])
365+
}
366+
try {
367+
MethodHandle mainHandle = lookup.findVirtual(bootClass, "main", MethodType.methodType(void.class, String[].class));
368+
mainHandle.invoke(newInstance(bootClass), arguments);
369+
return;
370+
} catch (final NoSuchMethodException | IllegalAccessException e) {
371+
// No instance main(String[])
372+
}
373+
try {
374+
MethodHandle mainHandle = lookup.findStatic(bootClass, "main", MethodType.methodType(void.class));
375+
mainHandle.invoke();
376+
return;
377+
} catch (final NoSuchMethodException | IllegalAccessException e) {
378+
// No static main()
367379
}
380+
try {
381+
MethodHandle mainHandle = lookup.findVirtual(bootClass, "main", MethodType.methodType(void.class));
382+
mainHandle.invoke(newInstance(bootClass));
383+
return;
384+
} catch (final NoSuchMethodException | IllegalAccessException e) {
385+
// No instance main()
386+
}
387+
388+
if (Runnable.class.isAssignableFrom(bootClass)) {
389+
doRun(bootClass);
390+
return;
391+
}
392+
393+
throw new NoSuchMethodException("No suitable main method found for " + bootClass + ", and not Runnable");
368394
}
369395

370-
private void doMain(final MethodHandle mainHandle) throws Throwable {
371-
mainHandle.invoke(arguments);
396+
private Object newInstance(final Class<?> bootClass) throws InvocationTargetException, InstantiationException, IllegalAccessException, NoSuchMethodException {
397+
final Constructor<?> constructor = bootClass.getDeclaredConstructor();
398+
if ((constructor.getModifiers() & Modifier.PRIVATE) != 0) {
399+
throw new NoSuchMethodException("No public constructor found for " + bootClass);
400+
}
401+
return constructor.newInstance();
372402
}
373403

374404
private void doRun(final Class<?> bootClass)

src/test/java/org/codehaus/mojo/exec/ExecJavaMojoTest.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import org.codehaus.plexus.logging.console.ConsoleLogger;
3636
import org.eclipse.aether.DefaultRepositorySystemSession;
3737
import org.eclipse.aether.RepositorySystemSession;
38+
import org.junit.Test;
3839
import org.mockito.Mock;
3940
import org.mockito.MockitoAnnotations;
4041

@@ -91,6 +92,50 @@ public void testSimpleRun() throws Exception {
9192
assertEquals("Hello" + System.getProperty("line.separator"), output);
9293
}
9394

95+
public void testJSR512InstanceMainNoArgs() throws Exception {
96+
File pom = new File(getBasedir(), "src/test/projects/project22/pom.xml");
97+
98+
String output = execute(pom, "java");
99+
100+
assertEquals("Correct choice" + System.getProperty("line.separator"), output);
101+
}
102+
103+
public void testJSR512PrefersStringArrayArgs() throws Exception {
104+
File pom = new File(getBasedir(), "src/test/projects/project23/pom.xml");
105+
106+
String output = execute(pom, "java");
107+
108+
assertEquals("Correct choice arg1 arg2" + System.getProperty("line.separator"), output);
109+
}
110+
111+
public void testJSR512StaticMainNoArgs() throws Exception {
112+
File pom = new File(getBasedir(), "src/test/projects/project24/pom.xml");
113+
114+
String output = execute(pom, "java");
115+
116+
assertEquals("Correct choice" + System.getProperty("line.separator"), output);
117+
}
118+
119+
public void testJSR512FailureInstanceMainPrivateNoArgsConstructor() throws Exception {
120+
File pom = new File(getBasedir(), "src/test/projects/project25/pom.xml");
121+
try {
122+
execute(pom, "java");
123+
fail("Expected Exception to be thrown but none was thrown");
124+
} catch (Throwable e) {
125+
assertTrue(e instanceof MojoExecutionException);
126+
127+
assertEquals("The specified mainClass doesn't contain a main method with appropriate signature.", e.getCause().getMessage());
128+
}
129+
}
130+
131+
public void testJSR512InheritedMain() throws Exception {
132+
File pom = new File(getBasedir(), "src/test/projects/project26/pom.xml");
133+
134+
String output = execute(pom, "java");
135+
136+
assertEquals("Correct choice arg1 arg2" + System.getProperty("line.separator"), output);
137+
}
138+
94139
/**
95140
* MEXEC-10 Check that an execution with no arguments and an system property with no value produces the expected
96141
* result<br>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package org.codehaus.mojo.exec;
2+
3+
/**
4+
* Tests a main method that's not public, not static, and has no parameters.
5+
*/
6+
public class JSR512DummyMain1 {
7+
void main() {
8+
System.out.println("Correct choice");
9+
}
10+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package org.codehaus.mojo.exec;
2+
3+
/**
4+
* Tests that the main method with String[] parameter is preferred over that with no parameters.
5+
*/
6+
public class JSR512DummyMain2 {
7+
void main(String... args) {
8+
StringBuilder buffer = new StringBuilder("Correct choice");
9+
10+
for (String arg : args) {
11+
buffer.append(" ").append(arg);
12+
}
13+
14+
System.out.println(buffer.toString());
15+
}
16+
17+
void main() {
18+
System.out.println("Wrong choice");
19+
}
20+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package org.codehaus.mojo.exec;
2+
3+
/**
4+
* Tests invocation of static main method without args.
5+
*/
6+
public class JSR512DummyMain3 {
7+
int main(String... args) {
8+
StringBuilder buffer = new StringBuilder("Wrong choice");
9+
10+
for (String arg : args) {
11+
buffer.append(System.getProperty("line.separator")).append(arg);
12+
}
13+
14+
System.out.println(buffer.toString());
15+
return buffer.length();
16+
}
17+
18+
static void main() {
19+
System.out.println("Correct choice");
20+
}
21+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package org.codehaus.mojo.exec;
2+
3+
/**
4+
* Testing failure with an instance main and private no-arg constructor.
5+
*/
6+
public class JSR512DummyMain4 {
7+
private JSR512DummyMain4() {}
8+
9+
void main() {
10+
System.out.println("Wrong choice");
11+
}
12+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package org.codehaus.mojo.exec;
2+
3+
/**
4+
* Testing inherited main method. Must consider superclass main methods and then pick the appropriate one.
5+
*/
6+
class JSR512DummySuper {
7+
void main(String... args) {
8+
StringBuilder buffer = new StringBuilder("Correct choice");
9+
10+
for (String arg : args) {
11+
buffer.append(" ").append(arg);
12+
}
13+
14+
System.out.println(buffer.toString());
15+
}
16+
}
17+
18+
public class JSR512DummyMain5 extends JSR512DummySuper {
19+
void main() {
20+
System.out.println("Wrong choice");
21+
}
22+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<project>
2+
<modelVersion>4.0.0</modelVersion>
3+
<groupId>org.cb.maven.plugins.exec</groupId>
4+
<artifactId>project4</artifactId>
5+
<version>0.1</version>
6+
<packaging>jar</packaging>
7+
<name>Maven Exec Plugin</name>
8+
<inceptionYear>2005</inceptionYear>
9+
<licenses>
10+
<license>
11+
<name>Apache License 2</name>
12+
<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
13+
<distribution>repo</distribution>
14+
</license>
15+
</licenses>
16+
17+
<build>
18+
<plugins>
19+
<plugin>
20+
<groupId>org.codehaus.mojo</groupId>
21+
<artifactId>exec-maven-plugin</artifactId>
22+
<executions>
23+
<execution>
24+
<phase>test</phase>
25+
<goals>
26+
<goal>java</goal>
27+
</goals>
28+
</execution>
29+
</executions>
30+
<configuration>
31+
<mainClass>org.codehaus.mojo.exec.JSR512DummyMain1</mainClass>
32+
</configuration>
33+
</plugin>
34+
</plugins>
35+
</build>
36+
37+
</project>
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<project>
2+
<modelVersion>4.0.0</modelVersion>
3+
<groupId>org.cb.maven.plugins.exec</groupId>
4+
<artifactId>project4</artifactId>
5+
<version>0.1</version>
6+
<packaging>jar</packaging>
7+
<name>Maven Exec Plugin</name>
8+
<inceptionYear>2005</inceptionYear>
9+
<licenses>
10+
<license>
11+
<name>Apache License 2</name>
12+
<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
13+
<distribution>repo</distribution>
14+
</license>
15+
</licenses>
16+
17+
<build>
18+
<plugins>
19+
<plugin>
20+
<groupId>org.codehaus.mojo</groupId>
21+
<artifactId>exec-maven-plugin</artifactId>
22+
<executions>
23+
<execution>
24+
<phase>test</phase>
25+
<goals>
26+
<goal>java</goal>
27+
</goals>
28+
</execution>
29+
</executions>
30+
<configuration>
31+
<mainClass>org.codehaus.mojo.exec.JSR512DummyMain2</mainClass>
32+
<arguments>
33+
<argument>arg1</argument>
34+
<argument>arg2</argument>
35+
</arguments>
36+
</configuration>
37+
</plugin>
38+
</plugins>
39+
</build>
40+
41+
</project>
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<project>
2+
<modelVersion>4.0.0</modelVersion>
3+
<groupId>org.cb.maven.plugins.exec</groupId>
4+
<artifactId>project4</artifactId>
5+
<version>0.1</version>
6+
<packaging>jar</packaging>
7+
<name>Maven Exec Plugin</name>
8+
<inceptionYear>2005</inceptionYear>
9+
<licenses>
10+
<license>
11+
<name>Apache License 2</name>
12+
<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
13+
<distribution>repo</distribution>
14+
</license>
15+
</licenses>
16+
17+
<build>
18+
<plugins>
19+
<plugin>
20+
<groupId>org.codehaus.mojo</groupId>
21+
<artifactId>exec-maven-plugin</artifactId>
22+
<executions>
23+
<execution>
24+
<phase>test</phase>
25+
<goals>
26+
<goal>java</goal>
27+
</goals>
28+
</execution>
29+
</executions>
30+
<configuration>
31+
<mainClass>org.codehaus.mojo.exec.JSR512DummyMain3</mainClass>
32+
</configuration>
33+
</plugin>
34+
</plugins>
35+
</build>
36+
37+
</project>

0 commit comments

Comments
 (0)