Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Eliminating need for agent #28

Merged
merged 1 commit into from
Aug 19, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,18 @@

import com.google.common.base.Joiner;
import com.google.common.collect.ImmutableMap;
import java.io.*;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.*;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.plugin.*;
import org.apache.maven.plugins.annotations.*;
import org.apache.maven.project.MavenProject;
import org.apache.maven.toolchain.*;

import java.io.*;
import java.util.*;

import static org.twdata.maven.mojoexecutor.MojoExecutor.*;

Expand Down Expand Up @@ -63,6 +67,16 @@ abstract class ProcessClassesMojo extends AbstractMojo {
*/
@Parameter(defaultValue = "1.7", property = "retrolambdaTarget", required = true)
public String target;

/**
* Should we start new process or perform the retrolambdafication in the
* same VM as Maven runs in (which has to be 1.8 then)? If the VM is
* forked, it uses -javaagent argument to intercept class definitions.
* When we run in the same process, we hook into the class generation
* by internal "lambda dumping" API.
*/
@Parameter(defaultValue = "false")
public boolean fork;

protected abstract File getInputDir();

Expand All @@ -79,7 +93,11 @@ public void execute() throws MojoExecutionException {
retrieveRetrolambdaJar(version);

getLog().info("Processing classes with Retrolambda");
processClasses();
if (fork) {
processClassesWithAgent();
} else {
processClasses();
}
}

String getJavaCommand() {
Expand Down Expand Up @@ -130,7 +148,7 @@ private void retrieveRetrolambdaJar(String version) throws MojoExecutionExceptio
executionEnvironment(project, session, pluginManager));
}

private void processClasses() throws MojoExecutionException {
private void processClassesWithAgent() throws MojoExecutionException {
String retrolambdaJar = getRetrolambdaJarPath();
executeMojo(
plugin(groupId("org.apache.maven.plugins"),
Expand All @@ -155,6 +173,33 @@ private void processClasses() throws MojoExecutionException {
element("arg", attribute("value", retrolambdaJar))))),
executionEnvironment(project, session, pluginManager));
}

private void processClasses() throws MojoExecutionException {
String retrolambdaJar = getRetrolambdaJarPath();
File jar = new File(retrolambdaJar);

try {
StringBuilder sb = new StringBuilder();
sb.append(getInputDir());
for (Artifact a : project.getArtifacts()) {
if (a.getFile() != null) {
sb.append(File.pathSeparator);
sb.append(a.getFile());
}
}

URLClassLoader url = new URLClassLoader(new URL[] { jar.toURI().toURL() });
Class<?> mainClass = Class.forName("net.orfjackal.retrolambda.Main", true, url);
System.setProperty("retrolambda.bytecodeVersion", "" + targetBytecodeVersions.get(target));
System.setProperty("retrolambda.inputDir", getInputDir().getAbsolutePath());
System.setProperty("retrolambda.outputDir", getOutputDir().getAbsolutePath());
System.setProperty("retrolambda.classpath", sb.toString());
Method main = mainClass.getMethod("main", String[].class);
main.invoke(null, (Object) new String[0]);
} catch (Exception ex) {
throw new MojoExecutionException("Cannot initialize classloader for " + retrolambdaJar, ex);
}
}

private String getRetrolambdaJarPath() {
return getRetrolambdaJarDir() + "/" + getRetrolambdaJarName();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public Config(Properties p) {
}

public boolean isFullyConfigured() {
return hasAllRequiredProperties() && PreMain.isAgentLoaded();
return hasAllRequiredProperties();
}

private boolean hasAllRequiredProperties() {
Expand Down
Loading