diff options
author | Reinier Zwitserloot <reinier@tipit.to> | 2009-09-29 17:15:24 +0200 |
---|---|---|
committer | Reinier Zwitserloot <reinier@tipit.to> | 2009-09-29 17:15:24 +0200 |
commit | b3d191e30023a4d80ffc88646446f2df0bc2f353 (patch) | |
tree | f3a4592ef412ca60deaa59c3af245fb2056fca95 /src_eclipseagent/lombok | |
parent | 27a3efeb1cb6f79ecefc6e641ba78de6d69406c3 (diff) | |
download | lombok-b3d191e30023a4d80ffc88646446f2df0bc2f353.tar.gz lombok-b3d191e30023a4d80ffc88646446f2df0bc2f353.tar.bz2 lombok-b3d191e30023a4d80ffc88646446f2df0bc2f353.zip |
Everything seems to be working smoothly! Perhaps time to make this the main branch...
Diffstat (limited to 'src_eclipseagent/lombok')
-rw-r--r-- | src_eclipseagent/lombok/eclipse/agent/EclipsePatcher.java | 186 | ||||
-rw-r--r-- | src_eclipseagent/lombok/eclipse/agent/PatchFixes.java | 14 |
2 files changed, 70 insertions, 130 deletions
diff --git a/src_eclipseagent/lombok/eclipse/agent/EclipsePatcher.java b/src_eclipseagent/lombok/eclipse/agent/EclipsePatcher.java index c7aa48ff..83b6ec94 100644 --- a/src_eclipseagent/lombok/eclipse/agent/EclipsePatcher.java +++ b/src_eclipseagent/lombok/eclipse/agent/EclipsePatcher.java @@ -21,33 +21,14 @@ */ 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.reflect.InvocationTargetException; -import java.net.URI; -import java.net.URLDecoder; -import java.nio.charset.Charset; -import java.security.ProtectionDomain; -import java.util.HashMap; -import java.util.Map; -import java.util.jar.JarFile; -import java.util.regex.Matcher; -import java.util.regex.Pattern; -import org.objectweb.asm.Opcodes; - -import lombok.Lombok; -import lombok.core.SpiLoadUtil; import lombok.patcher.Hook; import lombok.patcher.MethodTarget; import lombok.patcher.ScriptManager; import lombok.patcher.StackRequest; -import lombok.patcher.scripts.AddFieldScript; -import lombok.patcher.scripts.ExitFromMethodEarlyScript; -import lombok.patcher.scripts.WrapReturnValuesScript; +import lombok.patcher.equinox.EquinoxClassLoader; +import lombok.patcher.scripts.ScriptBuilder; /** * This is a java-agent that patches some of eclipse's classes so AST Nodes are handed off to Lombok @@ -59,135 +40,80 @@ import lombok.patcher.scripts.WrapReturnValuesScript; public class EclipsePatcher { private EclipsePatcher() {} - private static Map<String, EclipseTransformer> transformers = new HashMap<String, EclipseTransformer>(); - static { - try { - for ( EclipseTransformer transformer : SpiLoadUtil.findServices(EclipseTransformer.class) ) { - String targetClassName = transformer.getTargetClassName(); - transformers.put(targetClassName, transformer); - } - } catch ( Throwable t ) { - throw Lombok.sneakyThrow(t); - } - } - - private static class Patcher implements ClassFileTransformer { - public byte[] transform(ClassLoader loader, String className, - Class<?> classBeingRedefined, - ProtectionDomain protectionDomain, byte[] classfileBuffer) - throws IllegalClassFormatException { - -// ClassLoader classLoader = Patcher.class.getClassLoader(); -// if ( classLoader == null ) classLoader = ClassLoader.getSystemClassLoader(); - - EclipseTransformer transformer = transformers.get(className); - if ( transformer != null ) return transformer.transform(classfileBuffer); - - return null; - } - } - 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(); - //On java 1.5, you don't have these methods, so you'll be forced to manually -Xbootclasspath/a them in. - tryCallMethod(instrumentation, "appendToSystemClassLoaderSearch", path + "/lombok.jar"); - tryCallMethod(instrumentation, "appendToBootstrapClassLoaderSearch", path + "/lombok.eclipse.agent.jar"); + registerPatchScripts(instrumentation, true); } - private static void tryCallMethod(Object o, String methodName, String path) { - try { - Instrumentation.class.getMethod(methodName, JarFile.class).invoke(o, new JarFile(path)); - } catch ( Throwable ignore ) {} - } - - private static String findPathOfOurClassloader() throws Exception { - URI uri = EclipsePatcher.class.getResource("/" + EclipsePatcher.class.getName().replace('.', '/') + ".class").toURI(); - Pattern p = Pattern.compile("^jar:file:([^\\!]+)\\!.*\\.class$"); - Matcher m = p.matcher(uri.toString()); - if ( !m.matches() ) return "."; - String rawUri = m.group(1); - return new File(URLDecoder.decode(rawUri, Charset.defaultCharset().name())).getParent(); + public static void premain(String agentArgs, Instrumentation instrumentation) throws Exception { + registerPatchScripts(instrumentation, false); } - public static void premain(String agentArgs, Instrumentation instrumentation) throws Exception { - registerPatcher(instrumentation, false); - addLombokToSearchPaths(instrumentation); + private static void registerPatchScripts(Instrumentation instrumentation, boolean reloadExistingClasses) { ScriptManager sm = new ScriptManager(); sm.registerTransformer(instrumentation); + EquinoxClassLoader.getInstance().addPrefix("lombok."); + EquinoxClassLoader.getInstance().registerScripts(sm); - sm.addScript(new WrapReturnValuesScript( - new MethodTarget("org.eclipse.jdt.core.dom.ASTConverter", "retrieveStartingCatchPosition"), - new Hook("java/lombok/eclipse/PatchFixes", "fixRetrieveStartingCatchPosition", "(I)I"), - StackRequest.PARAM1)); + sm.addScript(ScriptBuilder.wrapReturnValue() + .target(new MethodTarget("org.eclipse.jdt.core.dom.ASTConverter", "retrieveStartingCatchPosition")) + .wrapMethod(new Hook("lombok/eclipse/agent/PatchFixes", "fixRetrieveStartingCatchPosition", "(I)I")) + .transplant().request(StackRequest.PARAM1).build()); - sm.addScript(new AddFieldScript("org.eclipse.jdt.internal.compiler.ast.ASTNode", - Opcodes.ACC_PUBLIC | Opcodes.ACC_TRANSIENT, "$generatedBy", "Lorg/eclipse/jdt/internal/compiler/ast/ASTNode;")); + sm.addScript(ScriptBuilder.addField() + .targetClass("org.eclipse.jdt.internal.compiler.ast.ASTNode") + .fieldName("$generatedBy") + .fieldType("Lorg/eclipse/jdt/internal/compiler/ast/ASTNode;") + .setPublic().setTransient().build()); - sm.addScript(new AddFieldScript("org.eclipse.jdt.core.dom.ASTNode", - Opcodes.ACC_PUBLIC | Opcodes.ACC_TRANSIENT, "$isGenerated", "Z")); + sm.addScript(ScriptBuilder.addField() + .targetClass("org.eclipse.jdt.core.dom.ASTNode") + .fieldName("$isGenerated").fieldType("Z") + .setPublic().setTransient().build()); - sm.addScript(new AddFieldScript("org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration", - Opcodes.ACC_PUBLIC | Opcodes.ACC_TRANSIENT, "$lombokAST", "Ljava/lang/Object;")); + sm.addScript(ScriptBuilder.addField() + .targetClass("org.eclipse.jdt.internal.compiler.CompilationUnitDeclaration") + .fieldName("$lombokAST").fieldType("Ljava/lang/Object;") + .setPublic().setTransient().build()); - sm.addScript(new WrapReturnValuesScript( - new MethodTarget("org.eclipse.jdt.internal.compiler.parser.Parser", "getMethodBodies", "void", - "org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration"), - new Hook("java/lombok/eclipse/ClassLoaderWorkaround", "transformCompilationUnitDeclaration", - "(Ljava/lang/Object;Ljava/lang/Object;)V"), StackRequest.THIS, StackRequest.PARAM1)); + final String PARSER_SIG1 = "org.eclipse.jdt.internal.compiler.parser.Parser"; + final String PARSER_SIG2 = "Lorg/eclipse/jdt/internal/compiler/parser/Parser;"; + final String CUD_SIG1 = "org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration"; + final String CUD_SIG2 = "Lorg/eclipse/jdt/internal/compiler/ast/CompilationUnitDeclaration;"; - sm.addScript(new WrapReturnValuesScript( - new MethodTarget("org.eclipse.jdt.internal.compiler.parser.Parser", "endParse", - "org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration", "int"), - new Hook("java/lombok/eclipse/ClassLoaderWorkaround", "transformCompilationUnitDeclarationSwapped", - "(Ljava/lang/Object;Ljava/lang/Object;)V"), StackRequest.THIS, StackRequest.RETURN_VALUE)); + sm.addScript(ScriptBuilder.wrapReturnValue() + .target(new MethodTarget(PARSER_SIG1, "getMethodBodies", "void", CUD_SIG1)) + .wrapMethod(new Hook("lombok/eclipse/TransformEclipseAST", "transform", + "(" + PARSER_SIG2 + CUD_SIG2 + ")V")) + .request(StackRequest.THIS, StackRequest.PARAM1).build()); - sm.addScript(new ExitFromMethodEarlyScript( - new MethodTarget("org.eclipse.jdt.internal.compiler.parser.Parser", "parse", "void", + sm.addScript(ScriptBuilder.wrapReturnValue() + .target(new MethodTarget(PARSER_SIG1, "endParse", CUD_SIG1, "int")) + .wrapMethod(new Hook("lombok/eclipse/TransformEclipseAST", "transform_swapped", + "(" + CUD_SIG2 + PARSER_SIG2 + ")V")) + .request(StackRequest.THIS, StackRequest.RETURN_VALUE).build()); + + sm.addScript(ScriptBuilder.exitEarly() + .target(new MethodTarget(PARSER_SIG1, "parse", "void", "org.eclipse.jdt.internal.compiler.ast.MethodDeclaration", - "org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration"), - new Hook("java/lombok/eclipse/PatchFixes", "checkBit24", - "(Ljava/lang/Object;)Z"), null, StackRequest.PARAM1)); + "org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration")) + .decisionMethod(new Hook("lombok/eclipse/agent/PatchFixes", "checkBit24", "(Ljava/lang/Object;)Z")) + .transplant().request(StackRequest.PARAM1).build()); - sm.addScript(new ExitFromMethodEarlyScript( - new MethodTarget("org.eclipse.jdt.internal.compiler.parser.Parser", "parse", "void", + sm.addScript(ScriptBuilder.exitEarly() + .target(new MethodTarget(PARSER_SIG1, "parse", "void", "org.eclipse.jdt.internal.compiler.ast.ConstructorDeclaration", - "org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration", "boolean"), - new Hook("java/lombok/eclipse/PatchFixes", "checkBit24", - "(Ljava/lang/Object;)Z"), null, StackRequest.PARAM1)); + "org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration", "boolean")) + .decisionMethod(new Hook("lombok/eclipse/agent/PatchFixes", "checkBit24", "(Ljava/lang/Object;)Z")) + .transplant().request(StackRequest.PARAM1).build()); - sm.addScript(new ExitFromMethodEarlyScript( - new MethodTarget("org.eclipse.jdt.internal.compiler.parser.Parser", "parse", "void", + sm.addScript(ScriptBuilder.exitEarly() + .target(new MethodTarget(PARSER_SIG1, "parse", "void", "org.eclipse.jdt.internal.compiler.ast.Initializer", "org.eclipse.jdt.internal.compiler.ast.TypeDeclaration", - "org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration"), - new Hook("java/lombok/eclipse/PatchFixes", "checkBit24", - "(Ljava/lang/Object;)Z"), null, StackRequest.PARAM1)); - } - - private static void registerPatcher(Instrumentation instrumentation, boolean transformExisting) throws IOException { - instrumentation.addTransformer(new Patcher()/*, true*/); + "org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration")) + .decisionMethod(new Hook("lombok/eclipse/agent/PatchFixes", "checkBit24", "(Ljava/lang/Object;)Z")) + .transplant().request(StackRequest.PARAM1).build()); - if ( transformExisting ) for ( Class<?> c : instrumentation.getAllLoadedClasses() ) { - if ( transformers.containsKey(c.getName()) ) { - try { - //instrumentation.retransformClasses(c); - //not in java 1.5. - Instrumentation.class.getMethod("retransformClasses", Class[].class).invoke(instrumentation, - new Object[] { new Class[] {c }}); - } catch ( InvocationTargetException e ) { - 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."); - } catch ( Throwable t ) { - throw new UnsupportedOperationException( - "This appears to be a java 1.5 instance, which cannot reload already loaded classes. " + - "You'll have to restart eclipse in order to use Lombok in eclipse."); - } - } - } + if (reloadExistingClasses) sm.reloadClasses(instrumentation); } } diff --git a/src_eclipseagent/lombok/eclipse/agent/PatchFixes.java b/src_eclipseagent/lombok/eclipse/agent/PatchFixes.java new file mode 100644 index 00000000..e36823b2 --- /dev/null +++ b/src_eclipseagent/lombok/eclipse/agent/PatchFixes.java @@ -0,0 +1,14 @@ +package lombok.eclipse.agent; + +public class PatchFixes { + public static int fixRetrieveStartingCatchPosition(int in) { + return in; + } + + private static final int BIT24 = 0x800000; + + public static boolean checkBit24(Object node) throws Exception { + int bits = (Integer)(node.getClass().getField("bits").get(node)); + return (bits & BIT24) != 0; + } +} |