Skip to content

Commit

Permalink
[#3143] Review and update jopatai's work on the maven/ecj agent jar
Browse files Browse the repository at this point in the history
* Update copyright headers
* Update code style (tabs, not spaces, spaces around + operator - that's about it)
* Use `x.class.getResourceAsStream`, not `x.getClass().` - minor mostly irrelevant nit.
* Rename and re-locate the jar itself.
* 'ecj' as an alias for this command seems a bit too cavalier' removed it.
* The source is in its own 'root' src dir, it doesn't really fit in the eclipse agent sources - it's more a maven agent.
* Fixed a bug where a filehandle wasn't safe closed. Mostly irrelevant (JVM would quite afterwards anyway).
* Slight rewording of the ecj docs.
  • Loading branch information
rzwitserloot committed Apr 2, 2022
1 parent 110434f commit c8607ba
Show file tree
Hide file tree
Showing 8 changed files with 115 additions and 121 deletions.
13 changes: 6 additions & 7 deletions buildScripts/compile.ant.xml
@@ -1,5 +1,5 @@
<!--
Copyright (C) 2020-2021 The Project Lombok Authors.
Copyright (C) 2020-2022 The Project Lombok Authors.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -74,13 +74,13 @@ This buildfile is part of projectlombok.org. It takes care of compiling and buil
</jar>
</target>

<target name="create.agentBootstrap" depends="-setup.build" description="Compiles the Maven ECJ bootstrap agent">
<ivy:compile destdir="build/agentBootstrap" source="1.6" target="1.6" ecj="true">
<target name="create.mavenEcjBootstrapAgent" depends="-setup.build" description="Compiles the Maven ECJ bootstrap agent">
<ivy:compile destdir="build/mavenEcjBootstrapAgent" source="1.6" target="1.6" ecj="true">
<bootclasspath path="${jdk6-rt.loc}" />
<src path="src/eclipseAgent" />
<include name="lombok/launch/AgentBootstrap.java" />
<src path="src/mavenEcjBootstrapAgent" />
</ivy:compile>
<jar destfile="build/lombok-main/lombok/eclipse/agent/lombok-bootstrap.jar" basedir="build/agentBootstrap" includes="lombok/launch/AgentBootstrap*.class">
<mkdir dir="build/lombok-main/lombok/launch" />
<jar destfile="build/lombok-main/lombok/launch/mavenEcjBootstrapAgent.jar" basedir="build/mavenEcjBootstrapAgent">
<manifest>
<attribute name="Premain-Class" value="lombok.launch.AgentBootstrap" />
<attribute name="Can-Redefine-Classes" value="true" />
Expand Down Expand Up @@ -183,7 +183,6 @@ This buildfile is part of projectlombok.org. It takes care of compiling and buil
<src path="src/eclipseAgent" />
<src path="src/delombok" />
<exclude name="**/*Transplants.java" />
<exclude name="**/AgentBootstrap.java" />
<classpath refid="cp.build" />
<classpath refid="cp.eclipse-oxygen" />
<classpath refid="cp.javac6" />
Expand Down
3 changes: 2 additions & 1 deletion buildScripts/create-eclipse-project.ant.xml
@@ -1,5 +1,5 @@
<!--
Copyright (C) 2010-2021 The Project Lombok Authors.
Copyright (C) 2010-2022 The Project Lombok Authors.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -34,6 +34,7 @@ This buildfile is part of projectlombok.org. It creates the infrastructure neede
<srcdir dir="src/launch" />
<srcdir dir="src/utils" />
<srcdir dir="src/eclipseAgent" />
<srcdir dir="src/mavenEcjBootstrapAgent" />
<srcdir dir="src/installer" />
<srcdir dir="src/delombok" />
<srcdir dir="src/stubs" srcout="bin/stubs" />
Expand Down
3 changes: 2 additions & 1 deletion buildScripts/create-intellij-project.ant.xml
@@ -1,5 +1,5 @@
<!--
Copyright (C) 2010-2021 The Project Lombok Authors.
Copyright (C) 2010-2022 The Project Lombok Authors.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -45,6 +45,7 @@ This buildfile is part of projectlombok.org. It creates the infrastructure neede
<srcdir dir="src/launch" />
<srcdir dir="src/utils" />
<srcdir dir="src/eclipseAgent" />
<srcdir dir="src/mavenEcjBootstrapAgent" />
<srcdir dir="src/installer" />
<srcdir dir="src/delombok" />
<srcdir dir="src/stubs" />
Expand Down
3 changes: 2 additions & 1 deletion buildScripts/maven.ant.xml
@@ -1,5 +1,5 @@
<!--
Copyright (C) 2020 The Project Lombok Authors.
Copyright (C) 2020-2022 The Project Lombok Authors.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -43,6 +43,7 @@ This buildfile is part of projectlombok.org. It makes maven-compatible repositor
<fileset dir="src/launch" />
<fileset dir="src/utils" />
<fileset dir="src/eclipseAgent" />
<fileset dir="src/mavenEcjBootstrapAgent" />
<fileset dir="src/installer" />
<fileset dir="src/delombok" />
<fileset dir="test/transform/src" />
Expand Down
35 changes: 18 additions & 17 deletions src/eclipseAgent/lombok/eclipse/agent/MavenEcjBootstrapApp.java
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2009-2022 The Project Lombok Authors.
* Copyright (C) 2022 The Project Lombok Authors.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand All @@ -26,7 +26,6 @@
import java.io.FileWriter;
import java.io.InputStream;
import java.io.PrintStream;
import java.util.Arrays;
import java.util.List;

import com.zwitserloot.cmdreader.CmdReader;
Expand All @@ -45,18 +44,14 @@ public class MavenEcjBootstrapApp extends LombokApp {

@Override public String getAppDescription() {
return "Creates .mvn/jvm.config and .mvn/lombok-bootstrap.jar for\n" +
"use with the ECJ compiler.";
}

@Override public List<String> getAppAliases() {
return Arrays.asList("ecj");
"use with the ECJ compiler.";
}

private static class CmdArgs {
@Shorthand("w")
@Description("Overwrite existing files. Defaults to false.")
boolean overwrite = false;

@Shorthand("o")
@Description("The root of a Maven project. Defaults to the current working directory.")
String output;
Expand Down Expand Up @@ -103,7 +98,7 @@ private int makeMvn(File mvn) {
err = e;
}
if (result != 0) {
System.err.println("Could not create "+mvn.getPath());
System.err.println("Could not create " + mvn.getPath());
if (err != null) err.printStackTrace(System.err);
}
return result;
Expand Down Expand Up @@ -136,15 +131,21 @@ private int makeJar(File mvn, boolean overwrite) {
return 1;
}
try {
InputStream input = this.getClass().getResourceAsStream("/lombok/eclipse/agent/lombok-bootstrap.jar");
InputStream input = MavenEcjBootstrapApp.class.getResourceAsStream("/lombok/launch/mavenEcjBootstrapAgent.jar");
FileOutputStream output = new FileOutputStream(jar);
byte[] buffer = new byte[4096];
int length;
while ((length = input.read(buffer)) > 0) output.write(buffer, 0, length);
output.flush();
output.close();
System.out.println("Successfully created: " + canonical(jar));
return 0;
try {
byte[] buffer = new byte[4096];
int length;
while ((length = input.read(buffer)) > 0) output.write(buffer, 0, length);
output.flush();
output.close();
System.out.println("Successfully created: " + canonical(jar));
return 0;
} finally {
try {
output.close();
} catch (Exception ignore) {}
}
} catch (Exception e) {
System.err.println("Could not create: " + canonical(jar));
e.printStackTrace(System.err);
Expand Down
92 changes: 0 additions & 92 deletions src/eclipseAgent/lombok/launch/AgentBootstrap.java

This file was deleted.

@@ -0,0 +1,83 @@
/*
* Copyright (C) 2022 The Project Lombok Authors.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package lombok.launch;

import java.lang.instrument.ClassFileTransformer;
import java.lang.instrument.IllegalClassFormatException;
import java.lang.instrument.Instrumentation;
import java.net.URL;
import java.net.URLClassLoader;
import java.security.ProtectionDomain;
import java.util.jar.JarFile;

/**
* This Java agent does not transform bytecode, but acts as a watcher that can
* figure out when it is appropriate to load Lombok itself within a Maven
* execution.
*
* It relies on several facts:
* <ul>
* <li>maven-compiler-plugin contains an {@code AbstractCompilerMojo} class that
* compiler instances extend.
* <li>Maven loaders are {@code ClassRealms}, which extend {@code URLClassLoader}.
* <li>Each plugin dependency in the <em>pom.xml </em>is represented as a file URL on the
* ClassRealm that points to the artifact.
* <li>URLs to Maven artifacts contain the group and artifact ids
* ({@code [...]/groupid/artifactid/ver/artifactid-ver.jar}).
* <li>The Lombok Java agent class is {@code lombok.launch.Agent}.
* </ul>
* Given all of the above, the transformer simply waits for {@code AbstractCompilerMojo}
* to be loaded, then uses the loader to find the path to the Lombok jar file,
* and finally loads the Lombok agent using reflection.
*/
public final class MavenEcjBootstrapAgent {
private static final String MAVEN_COMPILER_TRIGGER_CLASS = "org/apache/maven/plugin/compiler/AbstractCompilerMojo";
private static final String LOMBOK_URL_IDENTIFIER = "/org/projectlombok/lombok/";
private static final String LOMBOK_AGENT_CLASS = "lombok.launch.Agent";
private static final byte[] NOT_TRANSFORMED = null;

private MavenEcjBootstrapAgent() {}

public static void premain(final String agentArgs, final Instrumentation instrumentation) {
instrumentation.addTransformer(new ClassFileTransformer() {
@Override public byte[] transform(final ClassLoader loader, final String className, final Class<?> cbr, final ProtectionDomain pd, final byte[] cfb) throws IllegalClassFormatException {
if (MAVEN_COMPILER_TRIGGER_CLASS.equals(className)) {
for (final URL url : ((URLClassLoader) loader).getURLs()) {
if (url.getPath().contains(LOMBOK_URL_IDENTIFIER)) {
try {
instrumentation.appendToSystemClassLoaderSearch(new JarFile(url.getPath()));
MavenEcjBootstrapAgent.class.getClassLoader().loadClass(LOMBOK_AGENT_CLASS).getDeclaredMethod("premain", String.class, Instrumentation.class).invoke(null, agentArgs, instrumentation);
instrumentation.removeTransformer(this);
break;
} catch (Exception e) {
// There are no appropriate loggers available at
// this point in time.
e.printStackTrace(System.err);
}
}
}
}
return NOT_TRANSFORMED;
}
});
}
}
4 changes: 2 additions & 2 deletions website/templates/setup/ecj.html
Expand Up @@ -12,14 +12,14 @@
If you're using a tool based on ECJ, adding these VM arguments and adding <code>lombok.jar</code> to the classpath should work.
</p>
</@s.introduction>

<@s.section title="Maven">
<p>
Lombok comes with a tiny bootstrap agent that can be included in your project to allow ECJ to easily work with Maven.
To create this agent, run:
<pre>java -jar lombok.jar createMavenECJBootstrap -o <em class="note">/path/to/project/root</em></pre>
</p><p>
The -o path should be the location of your <code>pom.xml</code>.
The -o path should contain your <code>pom.xml</code>.
</p><p>
This will create two files, <code>.mvn/jvm.config</code> and <code>.mvn/lombok-bootstrap.jar</code>. Maven will use these files
to activate the standard Lombok Java agent at the right time. These can be committed in source control for a portable build.
Expand Down

1 comment on commit c8607ba

@jopatai
Copy link
Contributor

@jopatai jopatai commented on c8607ba Apr 4, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rzwitserloot One minor feedback on this. If I were a developer on a project and came across a file in the source code called "mavenEcjBootstrapAgent.jar", I would have no indication at all that it's related to Lombok. Maven would be obvious, though, since the project uses pom.xml and the file is referenced by jvm.config. Should the name include "Lombok" in some way?

Please sign in to comment.