From 8c8162d06305f96e9ed8b9eb2e8dd91f0961b7b1 Mon Sep 17 00:00:00 2001 From: Robert Scholte Date: Sat, 9 Feb 2019 17:32:48 +0100 Subject: [PATCH] Switch to methodHandles (#119) --- pom.xml | 3 ++ src/it/mrm/repository/parent-0.1.pom | 2 +- src/it/projects/jigsaw/pom.xml | 4 +-- .../mexec-29-non-static/verify.groovy | 17 ++-------- .../mexec-29-wrong-signature/verify.groovy | 14 ++------ src/it/projects/mexec-98/verify.groovy | 2 +- .../org/codehaus/mojo/exec/ExecJavaMojo.java | 32 ++++++++----------- 7 files changed, 24 insertions(+), 50 deletions(-) diff --git a/pom.xml b/pom.xml index d5cdd5ab..5d92f9ef 100644 --- a/pom.xml +++ b/pom.xml @@ -228,6 +228,9 @@ java17 1.0 + + java.lang.invoke.MethodHandle + diff --git a/src/it/mrm/repository/parent-0.1.pom b/src/it/mrm/repository/parent-0.1.pom index e0c70f46..29f030dd 100644 --- a/src/it/mrm/repository/parent-0.1.pom +++ b/src/it/mrm/repository/parent-0.1.pom @@ -58,7 +58,7 @@ org.apache.maven.plugins maven-compiler-plugin - 3.1 + 3.8.0 org.apache.maven.plugins diff --git a/src/it/projects/jigsaw/pom.xml b/src/it/projects/jigsaw/pom.xml index 8ddbbde0..5fb6f0dc 100644 --- a/src/it/projects/jigsaw/pom.xml +++ b/src/it/projects/jigsaw/pom.xml @@ -17,10 +17,8 @@ org.apache.maven.plugins maven-compiler-plugin - 3.6.1 - 9 - 9 + 9 diff --git a/src/it/projects/mexec-29-non-static/verify.groovy b/src/it/projects/mexec-29-non-static/verify.groovy index bea2130e..ff48f16d 100644 --- a/src/it/projects/mexec-29-non-static/verify.groovy +++ b/src/it/projects/mexec-29-non-static/verify.groovy @@ -17,18 +17,5 @@ * under the License. */ -import java.io.* -import java.util.* - -t = new IntegrationBase() - -def buildLog = new File( basedir, "build.log" ) - -t.checkExistenceAndContentOfAFile(buildLog, [ - "[DEBUG] Setting accessibility to true in order to invoke main().", - "org.apache.maven.plugin.MojoExecutionException: Can't call main(String[])-method because it is not static.", -]) - - - - +def buildLog = new File( basedir, 'build.log' ) +assert buildLog.text.contains('The specified mainClass doesn\'t contain a main method with appropriate signature.') \ No newline at end of file diff --git a/src/it/projects/mexec-29-wrong-signature/verify.groovy b/src/it/projects/mexec-29-wrong-signature/verify.groovy index 33129491..1894d5b0 100644 --- a/src/it/projects/mexec-29-wrong-signature/verify.groovy +++ b/src/it/projects/mexec-29-wrong-signature/verify.groovy @@ -17,15 +17,5 @@ * under the License. */ - import java.io.* - import java.util.* - - t = new IntegrationBase() - - def buildLog = new File( basedir, "build.log" ) - - t.checkExistenceAndContentOfAFile(buildLog, [ - "java.lang.Exception: The specified mainClass doesn't contain a main method with appropriate signature." - ]) - - \ No newline at end of file +def buildLog = new File( basedir, 'build.log' ) +assert buildLog.text.contains('java.lang.NoSuchMethodException: no such method: org.codehaus.mojo.exec.Main.main(String[])void') diff --git a/src/it/projects/mexec-98/verify.groovy b/src/it/projects/mexec-98/verify.groovy index 6fc2a164..00299a6e 100644 --- a/src/it/projects/mexec-98/verify.groovy +++ b/src/it/projects/mexec-98/verify.groovy @@ -18,4 +18,4 @@ */ File log = new File(basedir, 'build.log') assert log.exists() -assert log.getText().contains( "Can't call main(String[])-method because it is not static.") \ No newline at end of file +assert log.getText().contains( "The specified mainClass doesn't contain a main method with appropriate signature.") \ No newline at end of file diff --git a/src/main/java/org/codehaus/mojo/exec/ExecJavaMojo.java b/src/main/java/org/codehaus/mojo/exec/ExecJavaMojo.java index 315a9493..0198589d 100644 --- a/src/main/java/org/codehaus/mojo/exec/ExecJavaMojo.java +++ b/src/main/java/org/codehaus/mojo/exec/ExecJavaMojo.java @@ -20,9 +20,10 @@ */ import java.io.File; +import java.lang.invoke.MethodHandle; +import java.lang.invoke.MethodHandles; +import java.lang.invoke.MethodType; import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; -import java.lang.reflect.Modifier; import java.net.MalformedURLException; import java.net.URL; import java.net.URLClassLoader; @@ -266,22 +267,17 @@ public void run() { try { - Method main = - Thread.currentThread().getContextClassLoader().loadClass( mainClass ).getMethod( "main", - new Class[] { - String[].class } ); - if ( !main.isAccessible() ) - { - getLog().debug( "Setting accessibility to true in order to invoke main()." ); - main.setAccessible( true ); - } - if ( !Modifier.isStatic( main.getModifiers() ) ) - { - throw new MojoExecutionException( "Can't call main(String[])-method because it is not static." ); - } - main.invoke( null, new Object[] { arguments } ); + Class bootClass = Thread.currentThread().getContextClassLoader().loadClass( mainClass ); + + MethodHandles.Lookup publicLookup = MethodHandles.publicLookup(); + + MethodHandle mainHandle = + publicLookup.findStatic( bootClass, "main", + MethodType.methodType( void.class, String[].class ) ); + + mainHandle.invoke( arguments ); } - catch ( NoSuchMethodException e ) + catch ( IllegalAccessException e ) { // just pass it on Thread.currentThread().getThreadGroup().uncaughtException( Thread.currentThread(), new Exception( "The specified mainClass doesn't contain a main method with appropriate signature.", @@ -292,7 +288,7 @@ public void run() Throwable exceptionToReport = e.getCause() != null ? e.getCause() : e; Thread.currentThread().getThreadGroup().uncaughtException( Thread.currentThread(), exceptionToReport ); } - catch ( Exception e ) + catch ( Throwable e ) { // just pass it on Thread.currentThread().getThreadGroup().uncaughtException( Thread.currentThread(), e ); }