aboutsummaryrefslogtreecommitdiff
path: root/src_eclipseagent/lombok/eclipse
diff options
context:
space:
mode:
Diffstat (limited to 'src_eclipseagent/lombok/eclipse')
-rw-r--r--src_eclipseagent/lombok/eclipse/agent/EclipseCUDTransformer.java31
-rw-r--r--src_eclipseagent/lombok/eclipse/agent/EclipseParserPatcher.java104
-rw-r--r--src_eclipseagent/lombok/eclipse/agent/EclipseParserTransformer.java142
3 files changed, 277 insertions, 0 deletions
diff --git a/src_eclipseagent/lombok/eclipse/agent/EclipseCUDTransformer.java b/src_eclipseagent/lombok/eclipse/agent/EclipseCUDTransformer.java
new file mode 100644
index 00000000..b1dc85cc
--- /dev/null
+++ b/src_eclipseagent/lombok/eclipse/agent/EclipseCUDTransformer.java
@@ -0,0 +1,31 @@
+package lombok.eclipse.agent;
+
+import org.objectweb.asm.ClassAdapter;
+import org.objectweb.asm.ClassReader;
+import org.objectweb.asm.ClassVisitor;
+import org.objectweb.asm.ClassWriter;
+import org.objectweb.asm.FieldVisitor;
+import org.objectweb.asm.Opcodes;
+
+class EclipseCUDTransformer {
+ public byte[] transform(byte[] classfileBuffer) {
+ ClassReader reader = new ClassReader(classfileBuffer);
+ ClassWriter writer = new ClassWriter(reader, 0);
+
+ ClassAdapter adapter = new CUDPatcherAdapter(writer);
+ reader.accept(adapter, 0);
+ return writer.toByteArray();
+ }
+
+ private static class CUDPatcherAdapter extends ClassAdapter {
+ CUDPatcherAdapter(ClassVisitor cv) {
+ super(cv);
+ }
+
+ @Override public void visitEnd() {
+ FieldVisitor fv = cv.visitField(Opcodes.ACC_PUBLIC | Opcodes.ACC_TRANSIENT, "$lombokAST", "Ljava/lang/Object;", null, null);
+ fv.visitEnd();
+ cv.visitEnd();
+ }
+ }
+}
diff --git a/src_eclipseagent/lombok/eclipse/agent/EclipseParserPatcher.java b/src_eclipseagent/lombok/eclipse/agent/EclipseParserPatcher.java
new file mode 100644
index 00000000..8ef90dee
--- /dev/null
+++ b/src_eclipseagent/lombok/eclipse/agent/EclipseParserPatcher.java
@@ -0,0 +1,104 @@
+package lombok.eclipse.agent;
+
+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,
+ ProtectionDomain protectionDomain, byte[] classfileBuffer)
+ throws IllegalClassFormatException {
+
+ if ( ECLIPSE_PARSER_CLASS_NAME.equals(className) ) {
+ 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) ) {
+ 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) throws Exception {
+ registerPatcher(instrumentation, true);
+ addLombokToSearchPaths(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) throws IOException {
+ instrumentation.addTransformer(new Patcher(), true);
+
+ if ( transformExisting ) for ( Class<?> c : instrumentation.getAllLoadedClasses() ) {
+ if ( c.getName().equals(ECLIPSE_PARSER_CLASS_NAME) ) {
+ try {
+ instrumentation.retransformClasses(c);
+ } catch ( UnmodifiableClassException ex ) {
+ throw new UnsupportedOperationException(
+ "The eclipse parser class is already loaded and cannot be modified. " +
+ "You'll have to restart eclipse in order to use Lombok in eclipse.");
+ }
+ }
+ }
+ }
+}
diff --git a/src_eclipseagent/lombok/eclipse/agent/EclipseParserTransformer.java b/src_eclipseagent/lombok/eclipse/agent/EclipseParserTransformer.java
new file mode 100644
index 00000000..399a94ce
--- /dev/null
+++ b/src_eclipseagent/lombok/eclipse/agent/EclipseParserTransformer.java
@@ -0,0 +1,142 @@
+package lombok.eclipse.agent;
+
+import java.lang.reflect.Constructor;
+import java.lang.reflect.InvocationTargetException;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.objectweb.asm.ClassAdapter;
+import org.objectweb.asm.ClassReader;
+import org.objectweb.asm.ClassVisitor;
+import org.objectweb.asm.ClassWriter;
+import org.objectweb.asm.Label;
+import org.objectweb.asm.MethodAdapter;
+import org.objectweb.asm.MethodVisitor;
+import org.objectweb.asm.Opcodes;
+
+class EclipseParserTransformer {
+ private static final String COMPILER_PKG =
+ "Lorg/eclipse/jdt/internal/compiler/ast/";
+ private static final String TARGET_STATIC_CLASS = "java/lombok/ClassLoaderWorkaround";
+ private static final String TARGET_STATIC_METHOD_NAME = "transformCompilationUnitDeclaration";
+ private static final String TARGET_STATIC_METHOD_DESC = "(Ljava/lang/Object;Ljava/lang/Object;)V";
+
+ private static final Map<String, Class<? extends MethodVisitor>> rewriters;
+
+ static {
+ Map<String, Class<? extends MethodVisitor>> map = new HashMap<String, Class<? extends MethodVisitor>>();
+ map.put(String.format("endParse(I)%sCompilationUnitDeclaration;", COMPILER_PKG), EndParsePatcher.class);
+ map.put(String.format("getMethodBodies(%sCompilationUnitDeclaration;)V", COMPILER_PKG), GetMethodBodiesPatcher.class);
+ map.put(String.format("parse(%1$sMethodDeclaration;%1$sCompilationUnitDeclaration;)V", COMPILER_PKG), ParseBlockContainerPatcher.class);
+ map.put(String.format("parse(%1$sConstructorDeclaration;%1$sCompilationUnitDeclaration;Z)V", COMPILER_PKG), ParseBlockContainerPatcher.class);
+ map.put(String.format("parse(%1$sInitializer;%1$sTypeDeclaration;%1$sCompilationUnitDeclaration;)V", COMPILER_PKG), ParseBlockContainerPatcher.class);
+ rewriters = Collections.unmodifiableMap(map);
+ }
+
+ public byte[] transform(byte[] classfileBuffer) {
+ ClassReader reader = new ClassReader(classfileBuffer);
+ ClassWriter writer = new ClassWriter(reader, 0);
+
+ ClassAdapter adapter = new ParserPatcherAdapter(writer);
+ reader.accept(adapter, 0);
+ return writer.toByteArray();
+ }
+
+ public static RuntimeException sneakyThrow(Throwable t) {
+ if ( t == null ) throw new NullPointerException("t");
+ EclipseParserTransformer.<RuntimeException>sneakyThrow0(t);
+ return null;
+ }
+
+ @SuppressWarnings("unchecked")
+ private static <T extends Throwable> void sneakyThrow0(Throwable t) throws T {
+ throw (T)t;
+ }
+
+ private static class ParserPatcherAdapter extends ClassAdapter {
+ public ParserPatcherAdapter(ClassVisitor cv) {
+ super(cv);
+ }
+
+ @Override public MethodVisitor visitMethod(int access, String name, String desc,
+ String signature, String[] exceptions) {
+ MethodVisitor writerVisitor = super.visitMethod(access, name, desc, signature, exceptions);
+ Class<? extends MethodVisitor> targetVisitorClass = rewriters.get(name+desc);
+ if ( targetVisitorClass == null ) return writerVisitor;
+
+ try {
+ Constructor<? extends MethodVisitor> c = targetVisitorClass.getDeclaredConstructor(MethodVisitor.class);
+ c.setAccessible(true);
+ return c.newInstance(writerVisitor);
+ } catch ( InvocationTargetException e ) {
+ throw sneakyThrow(e.getCause());
+ } catch ( Exception e ) {
+ //NoSuchMethodException: We know they exist.
+ //IllegalAccessException: We called setAccessible.
+ //InstantiationException: None of these classes are abstract.
+ throw sneakyThrow(e);
+ }
+ }
+ }
+
+ private static final int BIT24 = 0x800000;
+
+ static class GetMethodBodiesPatcher extends MethodAdapter {
+ GetMethodBodiesPatcher(MethodVisitor mv) {
+ super(mv);
+ }
+
+ @Override public void visitInsn(int opcode) {
+ if ( opcode == Opcodes.RETURN ) {
+ //injects: ClassLoaderWorkaround.transformCUD(parser, compilationUnitDeclaration);
+ super.visitVarInsn(Opcodes.ALOAD, 0);
+ super.visitVarInsn(Opcodes.ALOAD, 1);
+ super.visitMethodInsn(Opcodes.INVOKESTATIC, TARGET_STATIC_CLASS,
+ TARGET_STATIC_METHOD_NAME, TARGET_STATIC_METHOD_DESC);
+ }
+ super.visitInsn(opcode);
+ }
+ }
+
+ static class ParseBlockContainerPatcher extends MethodAdapter {
+ ParseBlockContainerPatcher(MethodVisitor mv) {
+ super(mv);
+ }
+
+ @Override public void visitCode() {
+ //injects: if ( constructorDeclaration.bits & BIT24 > 0 ) return;
+ mv.visitVarInsn(Opcodes.ALOAD, 1);
+ mv.visitFieldInsn(Opcodes.GETFIELD, "org/eclipse/jdt/internal/compiler/ast/ASTNode", "bits", "I");
+ mv.visitLdcInsn(Integer.valueOf(BIT24));
+ mv.visitInsn(Opcodes.IAND);
+ Label l0 = new Label();
+ mv.visitJumpInsn(Opcodes.IFLE, l0);
+ mv.visitInsn(Opcodes.RETURN);
+ mv.visitLabel(l0);
+ mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
+ super.visitCode();
+ }
+ }
+
+ static class EndParsePatcher extends MethodAdapter {
+ private static final String TARGET_STATIC_METHOD_NAME = "transformCompilationUnitDeclaration";
+
+ EndParsePatcher(MethodVisitor mv) {
+ super(mv);
+ }
+
+ @Override public void visitInsn(int opcode) {
+ if ( opcode == Opcodes.ARETURN ) {
+ //injects: ClassLoaderWorkaround.transformCUD(parser, compilationUnitDeclaration);
+ super.visitInsn(Opcodes.DUP);
+ super.visitVarInsn(Opcodes.ALOAD, 0);
+ super.visitInsn(Opcodes.SWAP);
+ super.visitMethodInsn(Opcodes.INVOKESTATIC, TARGET_STATIC_CLASS,
+ TARGET_STATIC_METHOD_NAME, TARGET_STATIC_METHOD_DESC);
+ }
+
+ super.visitInsn(opcode);
+ }
+ }
+}