Skip to content

Commit

Permalink
Switch to methodHandles (#119)
Browse files Browse the repository at this point in the history
  • Loading branch information
rfscholte authored Feb 9, 2019
1 parent cdd4061 commit 8c8162d
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 50 deletions.
3 changes: 3 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,9 @@
<artifactId>java17</artifactId>
<version>1.0</version>
</signature>
<ignores>
<ignore>java.lang.invoke.MethodHandle</ignore>
</ignores>
</configuration>
</plugin>
<plugin>
Expand Down
2 changes: 1 addition & 1 deletion src/it/mrm/repository/parent-0.1.pom
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<version>3.8.0</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
Expand Down
4 changes: 1 addition & 3 deletions src/it/projects/jigsaw/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,8 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version>
<configuration>
<source>9</source>
<target>9</target>
<release>9</release>
</configuration>
</plugin>
<plugin>
Expand Down
17 changes: 2 additions & 15 deletions src/it/projects/mexec-29-non-static/verify.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -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.')
14 changes: 2 additions & 12 deletions src/it/projects/mexec-29-wrong-signature/verify.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -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."
])


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')
2 changes: 1 addition & 1 deletion src/it/projects/mexec-98/verify.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
assert log.getText().contains( "The specified mainClass doesn't contain a main method with appropriate signature.")
32 changes: 14 additions & 18 deletions src/main/java/org/codehaus/mojo/exec/ExecJavaMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.",
Expand All @@ -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 );
}
Expand Down

0 comments on commit 8c8162d

Please sign in to comment.