mirror of
https://github.com/tiennm99/lombok.git
synced 2026-10-14 16:17:34 +00:00
Now you no longer need to add a -Xbootclasspath/a parameter to eclipse.ini; the agent fixes the classpath all by its lonesome. Wahey!
This commit is contained in:
1 parent
65de2407ae
commit
cd1ef24d0b
3 files changed
+60
-24
No files matched your search
@@ -8,14 +8,8 @@ import org.objectweb.asm.FieldVisitor;
|
||||
import org.objectweb.asm.Opcodes;
|
||||
|
||||
class EclipseCUDTransformer {
|
||||
private final byte[] in;
|
||||
|
||||
EclipseCUDTransformer(byte[] classfileBuffer) {
|
||||
in = classfileBuffer;
|
||||
}
|
||||
|
||||
byte[] transform() {
|
||||
ClassReader reader = new ClassReader(in);
|
||||
public byte[] transform(byte[] classfileBuffer) {
|
||||
ClassReader reader = new ClassReader(classfileBuffer);
|
||||
ClassWriter writer = new ClassWriter(reader, 0);
|
||||
|
||||
ClassAdapter adapter = new CUDPatcherAdapter(writer);
|
||||
@@ -29,7 +23,7 @@ class EclipseCUDTransformer {
|
||||
}
|
||||
|
||||
@Override public void visitEnd() {
|
||||
FieldVisitor fv = cv.visitField(Opcodes.ACC_PUBLIC, "$lombokAST", "Ljava/lang/Object;", null, null);
|
||||
FieldVisitor fv = cv.visitField(Opcodes.ACC_PUBLIC | Opcodes.ACC_TRANSIENT, "$lombokAST", "Ljava/lang/Object;", null, null);
|
||||
fv.visitEnd();
|
||||
cv.visitEnd();
|
||||
}
|
||||
|
||||
@@ -1,12 +1,22 @@
|
||||
package lombok.agent.eclipse;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.lang.instrument.ClassFileTransformer;
|
||||
import java.lang.instrument.IllegalClassFormatException;
|
||||
import java.lang.instrument.Instrumentation;
|
||||
import java.lang.instrument.UnmodifiableClassException;
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.lang.reflect.Method;
|
||||
import java.net.URI;
|
||||
import java.security.ProtectionDomain;
|
||||
import java.util.jar.JarFile;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public class EclipseParserPatcher {
|
||||
private EclipseParserPatcher() {}
|
||||
|
||||
private static class Patcher implements ClassFileTransformer {
|
||||
@Override public byte[] transform(ClassLoader loader, String className,
|
||||
Class<?> classBeingRedefined,
|
||||
@@ -14,31 +24,69 @@ public class EclipseParserPatcher {
|
||||
throws IllegalClassFormatException {
|
||||
|
||||
if ( ECLIPSE_PARSER_CLASS_NAME.equals(className) ) {
|
||||
EclipseParserTransformer transformer = new EclipseParserTransformer(classfileBuffer);
|
||||
return transformer.transform();
|
||||
try {
|
||||
return runTransform("lombok.agent.eclipse.EclipseParserTransformer", classfileBuffer);
|
||||
} catch ( Throwable t ) {
|
||||
System.err.println("Wasn't able to patch eclipse's Parser class:");
|
||||
t.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
if ( ECLIPSE_CUD_CLASS_NAME.equals(className) ) {
|
||||
EclipseCUDTransformer transformer = new EclipseCUDTransformer(classfileBuffer);
|
||||
return transformer.transform();
|
||||
try {
|
||||
return runTransform("lombok.agent.eclipse.EclipseCUDTransformer", classfileBuffer);
|
||||
} catch ( Throwable t ) {
|
||||
System.err.println("Wasn't able to patch eclipse's CompilationUnitDeclaration class:");
|
||||
t.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private static byte[] runTransform(String className, byte[] classfileBuffer) throws Exception {
|
||||
Class<?> transformerClass = Class.forName(className);
|
||||
Constructor<?> constructor = transformerClass.getDeclaredConstructor();
|
||||
constructor.setAccessible(true);
|
||||
Object instance = constructor.newInstance();
|
||||
Method m = transformerClass.getDeclaredMethod("transform", byte[].class);
|
||||
m.setAccessible(true);
|
||||
return (byte[])m.invoke(instance, classfileBuffer);
|
||||
}
|
||||
|
||||
static final String ECLIPSE_CUD_CLASS_NAME = "org/eclipse/jdt/internal/compiler/ast/CompilationUnitDeclaration";
|
||||
static final String ECLIPSE_PARSER_CLASS_NAME = "org/eclipse/jdt/internal/compiler/parser/Parser";
|
||||
|
||||
public static void agentmain(String agentArgs, Instrumentation instrumentation) {
|
||||
public static void agentmain(String agentArgs, Instrumentation instrumentation) throws Exception {
|
||||
registerPatcher(instrumentation, true);
|
||||
addLombokToSearchPaths(instrumentation);
|
||||
}
|
||||
|
||||
public static void premain(String agentArgs, Instrumentation instrumentation) {
|
||||
private static void addLombokToSearchPaths(Instrumentation instrumentation) throws Exception {
|
||||
String path = findPathOfOurClassloader();
|
||||
instrumentation.appendToSystemClassLoaderSearch(new JarFile(path + "/lombok.jar"));
|
||||
instrumentation.appendToBootstrapClassLoaderSearch(new JarFile(path + "/lombok.eclipse.agent.jar"));
|
||||
|
||||
}
|
||||
|
||||
private static String findPathOfOurClassloader() throws Exception {
|
||||
ClassLoader loader = EclipseParserPatcher.class.getClassLoader();
|
||||
if ( loader == null ) loader = ClassLoader.getSystemClassLoader();
|
||||
|
||||
URI uri = loader.getResource(EclipseParserPatcher.class.getName().replace('.', '/') + ".class").toURI();
|
||||
Pattern p = Pattern.compile("^jar:file:([^\\!]+)\\!.*\\.class$");
|
||||
Matcher m = p.matcher(uri.toString());
|
||||
if ( !m.matches() ) return ".";
|
||||
return new File(m.group(1)).getParent();
|
||||
}
|
||||
|
||||
public static void premain(String agentArgs, Instrumentation instrumentation) throws Exception {
|
||||
registerPatcher(instrumentation, false);
|
||||
addLombokToSearchPaths(instrumentation);
|
||||
}
|
||||
|
||||
private static void registerPatcher(Instrumentation instrumentation, boolean transformExisting) {
|
||||
private static void registerPatcher(Instrumentation instrumentation, boolean transformExisting) throws IOException {
|
||||
instrumentation.addTransformer(new Patcher(), true);
|
||||
|
||||
if ( transformExisting ) for ( Class<?> c : instrumentation.getAllLoadedClasses() ) {
|
||||
|
||||
@@ -34,14 +34,8 @@ class EclipseParserTransformer {
|
||||
rewriters = Collections.unmodifiableMap(map);
|
||||
}
|
||||
|
||||
private final byte[] in;
|
||||
|
||||
EclipseParserTransformer(byte[] classfileBuffer) {
|
||||
in = classfileBuffer;
|
||||
}
|
||||
|
||||
byte[] transform() {
|
||||
ClassReader reader = new ClassReader(in);
|
||||
public byte[] transform(byte[] classfileBuffer) {
|
||||
ClassReader reader = new ClassReader(classfileBuffer);
|
||||
ClassWriter writer = new ClassWriter(reader, 0);
|
||||
|
||||
ClassAdapter adapter = new ParserPatcherAdapter(writer);
|
||||
|
||||
Reference in new issue
Block a user