From 7bbf7789f8db8573dc76f215f092ddd2d8a8cbfd Mon Sep 17 00:00:00 2001 From: Reinier Zwitserloot Date: Fri, 19 Jun 2009 03:24:17 +0200 Subject: List of stuff we still need to do as well as research notes on how to get there. --- .../lombok/eclipse/agent/EclipseParserPatcher.java | 104 --------------------- .../lombok/eclipse/agent/EclipsePatcher.java | 104 +++++++++++++++++++++ 2 files changed, 104 insertions(+), 104 deletions(-) delete mode 100644 src_eclipseagent/lombok/eclipse/agent/EclipseParserPatcher.java create mode 100644 src_eclipseagent/lombok/eclipse/agent/EclipsePatcher.java (limited to 'src_eclipseagent') diff --git a/src_eclipseagent/lombok/eclipse/agent/EclipseParserPatcher.java b/src_eclipseagent/lombok/eclipse/agent/EclipseParserPatcher.java deleted file mode 100644 index 1418252d..00000000 --- a/src_eclipseagent/lombok/eclipse/agent/EclipseParserPatcher.java +++ /dev/null @@ -1,104 +0,0 @@ -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.eclipse.agent.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.eclipse.agent.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/EclipsePatcher.java b/src_eclipseagent/lombok/eclipse/agent/EclipsePatcher.java new file mode 100644 index 00000000..1418252d --- /dev/null +++ b/src_eclipseagent/lombok/eclipse/agent/EclipsePatcher.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.eclipse.agent.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.eclipse.agent.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."); + } + } + } + } +} -- cgit