From 310862d94e3a4705faf0d2b6ed9cdb40247b39ca Mon Sep 17 00:00:00 2001 From: Reinier Zwitserloot Date: Sun, 11 Oct 2009 07:54:25 +0200 Subject: Integrated lombok.jar and lombok.eclipse.agent.jar into one jar that installs, is a javac apt processor, and an agent, all at once. --- buildScripts/compile.ant.xml | 49 +++----------- buildScripts/deps.ant.xml | 8 +-- lib/README | 3 +- lib/eclipse.agent/lombok-patcher.jar | Bin 1490329 -> 0 bytes lib/lombok/lombok-patcher.jar | Bin 0 -> 1497998 bytes lib/netbeans.agent/.gitkeep | 1 - src/lombok/eclipse/TransformEclipseAST.java | 36 ++--------- src/lombok/installer/EclipseLocation.java | 71 ++++++++------------- src/lombok/installer/Installer.java | 4 +- .../lombok/eclipse/agent/EclipsePatcher.java | 25 ++++++-- 10 files changed, 69 insertions(+), 128 deletions(-) delete mode 100644 lib/eclipse.agent/lombok-patcher.jar create mode 100644 lib/lombok/lombok-patcher.jar delete mode 100644 lib/netbeans.agent/.gitkeep diff --git a/buildScripts/compile.ant.xml b/buildScripts/compile.ant.xml index 82ee07f7..075bc394 100644 --- a/buildScripts/compile.ant.xml +++ b/buildScripts/compile.ant.xml @@ -34,35 +34,21 @@ lombok code including the various agents. - - - - - - + - - - - - - - - - - + @@ -74,13 +60,16 @@ lombok code including the various agents. javac 1.5, partly because they completely rewrote large swaths of javac, and partly because our injection mechanism (annotations) doesn't work very well on javac 1.5, hence, when using javac, we do demand you're on 1.6. --> - + + + + - + - + @@ -93,35 +82,17 @@ lombok code including the various agents. lombok.javac.apt.Processor - - - - - - - - - - - - - - - - - - - - + + diff --git a/buildScripts/deps.ant.xml b/buildScripts/deps.ant.xml index 1c6919ee..b9d447f8 100644 --- a/buildScripts/deps.ant.xml +++ b/buildScripts/deps.ant.xml @@ -36,16 +36,16 @@ and building dependencies. - + + targetfile="lib/lombok/lombok-patcher.jar" /> - - ** UPDATED: lib/eclipse.agent/lombok-patcher.jar + + ** UPDATED: lib/lombok/lombok-patcher.jar diff --git a/lib/README b/lib/README index 1e605c10..e776bb9c 100644 --- a/lib/README +++ b/lib/README @@ -1,5 +1,4 @@ 'lib' is defined as those things which are required when actually running lombok. -The main lombok code requires all libraries in the 'lombok' directory, whilst the agents -require the contents of both the 'agent' directory and the agent's specific directory (e.g. 'eclipse.agent'). +The main lombok code requires all libraries in the 'lombok' directory. For dependencies which are required only at compile time, see the 'deps' directory. diff --git a/lib/eclipse.agent/lombok-patcher.jar b/lib/eclipse.agent/lombok-patcher.jar deleted file mode 100644 index ec614176..00000000 Binary files a/lib/eclipse.agent/lombok-patcher.jar and /dev/null differ diff --git a/lib/lombok/lombok-patcher.jar b/lib/lombok/lombok-patcher.jar new file mode 100644 index 00000000..86d4a889 Binary files /dev/null and b/lib/lombok/lombok-patcher.jar differ diff --git a/lib/netbeans.agent/.gitkeep b/lib/netbeans.agent/.gitkeep deleted file mode 100644 index 2ded1cf3..00000000 --- a/lib/netbeans.agent/.gitkeep +++ /dev/null @@ -1 +0,0 @@ -Empty dir placeholder diff --git a/src/lombok/eclipse/TransformEclipseAST.java b/src/lombok/eclipse/TransformEclipseAST.java index 691327b1..6eac4196 100644 --- a/src/lombok/eclipse/TransformEclipseAST.java +++ b/src/lombok/eclipse/TransformEclipseAST.java @@ -22,11 +22,9 @@ package lombok.eclipse; import java.lang.reflect.Field; -import java.util.Arrays; -import java.util.Collections; -import java.util.List; import lombok.eclipse.EclipseAST.Node; +import lombok.patcher.Symbols; import org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration; import org.eclipse.jdt.internal.compiler.ast.Annotation; @@ -77,24 +75,6 @@ public class TransformEclipseAST { handlers = l; } - private static final List DONT_RUN_LIST = Collections.unmodifiableList(Arrays.asList( - "org.eclipse.jdt.internal.corext.util.CodeFormatterUtil." - )); - - - /** - * Returns 'true' if the stack trace indicates lombok definitely SHOULD run for this parse job, by checking the context, - * and returns 'false' if the stack trace indicates lombok should definitely NOT run. - * - * Returns null if it can't tell (you probably should default to running lombok if you don't know). - */ - private static Boolean analyzeStackTrace(StackTraceElement[] trace) { - for ( StackTraceElement e : trace ) - if ( e.toString().contains(DONT_RUN_LIST.get(0)) ) return false; - return null; - //potential speedup: if trace contains org.eclipse.swt.widgets. -> stop - nothing interesting ever follows that. I think. - } - public static void transform_swapped(CompilationUnitDeclaration ast, Parser parser) { transform(parser, ast); } @@ -111,29 +91,27 @@ public class TransformEclipseAST { * @param ast The AST node belonging to the compilation unit (java speak for a single source file). */ public static void transform(Parser parser, CompilationUnitDeclaration ast) { - if ( disableLombok ) return; + if (disableLombok) return; - Boolean parse = analyzeStackTrace(new Throwable().getStackTrace()); - - if ( parse != null && parse == false ) return; + if (Symbols.hasSymbol("lombok.disable")) return; try { EclipseAST existing = getCache(ast); - if ( existing == null ) { + if (existing == null) { existing = new EclipseAST(ast); setCache(ast, existing); } else existing.reparse(); new TransformEclipseAST(existing).go(); - } catch ( Throwable t ) { + } catch (Throwable t) { try { String message = "Lombok can't parse this source: " + t.toString(); EclipseAST.addProblemToCompilationResult(ast, false, message, 0, 0); t.printStackTrace(); - } catch ( Throwable t2 ) { + } catch (Throwable t2) { try { Eclipse.error(ast, "Can't create an error in the problems dialog while adding: " + t.toString(), t2); - } catch ( Throwable t3 ) { + } catch (Throwable t3) { //This seems risky to just silently turn off lombok, but if we get this far, something pretty //drastic went wrong. For example, the eclipse help system's JSP compiler will trigger a lombok call, //but due to class loader shenanigans we'll actually get here due to a cascade of diff --git a/src/lombok/installer/EclipseLocation.java b/src/lombok/installer/EclipseLocation.java index d6a2f4b7..2b7e021d 100644 --- a/src/lombok/installer/EclipseLocation.java +++ b/src/lombok/installer/EclipseLocation.java @@ -31,10 +31,8 @@ import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Arrays; import java.util.List; -import java.util.jar.JarFile; import java.util.regex.Matcher; import java.util.regex.Pattern; -import java.util.zip.ZipEntry; import javax.swing.JFrame; import javax.swing.JOptionPane; @@ -205,12 +203,15 @@ final class EclipseLocation { null); } - File agentJar = new File(dir, "lombok.eclipse.agent.jar"); - if ( agentJar.exists() ) { - if ( !agentJar.delete() ) throw new UninstallException( - "Can't delete " + agentJar.getAbsolutePath() + - " - perhaps the installer does not have the access rights to do so.", - null); + /* legacy code - lombok at one point used to have a separate jar for the eclipse agent. + * Leave this code in to delete it for those upgrading from an old version. */ { + File agentJar = new File(dir, "lombok.eclipse.agent.jar"); + if ( agentJar.exists() ) { + if ( !agentJar.delete() ) throw new UninstallException( + "Can't delete " + agentJar.getAbsolutePath() + + " - perhaps the installer does not have the access rights to do so.", + null); + } } File iniFile = new File(dir, "eclipse.ini"); @@ -230,7 +231,9 @@ final class EclipseLocation { boolean first = true; for ( String elem : m.group(1).split(Pattern.quote(File.pathSeparator)) ) { if ( elem.toLowerCase().endsWith("lombok.jar") ) continue; - if ( elem.toLowerCase().endsWith("lombok.eclipse.agent.jar") ) continue; + /* legacy code -see previous comment that starts with 'legacy' */ { + if ( elem.toLowerCase().endsWith("lombok.eclipse.agent.jar") ) continue; + } if ( first ) first = false; else elemBuilder.append(File.pathSeparator); elemBuilder.append(elem); @@ -295,33 +298,11 @@ final class EclipseLocation { //is no less bad than aborting, and this situation should be rare to the point of non-existence. File lombokJar = new File(iniFile.getParentFile(), "lombok.jar"); - File agentJar = new File(iniFile.getParentFile(), "lombok.eclipse.agent.jar"); File ourJar = EclipseFinder.findOurJar(); byte[] b = new byte[524288]; boolean readSucceeded = false; try { - JarFile jar = new JarFile(ourJar); - - try { - ZipEntry entry = jar.getEntry("lombok.eclipse.agent.jar"); - InputStream in = jar.getInputStream(entry); - FileOutputStream out = new FileOutputStream(agentJar); - try { - while ( true ) { - int r = in.read(b); - if ( r == -1 ) break; - readSucceeded = true; - out.write(b, 0, r); - } - } finally { - out.close(); - } - } finally { - jar.close(); - - } - FileOutputStream out = new FileOutputStream(lombokJar); InputStream in = new FileInputStream(ourJar); try { @@ -336,13 +317,16 @@ final class EclipseLocation { } catch ( IOException e ) { try { lombokJar.delete(); - agentJar.delete(); } catch ( Throwable ignore ) {} if ( !readSucceeded ) throw new InstallException("I can't read my own jar file. I think you've found a bug in this installer! I suggest you restart it " + "and use the 'what do I do' link, to manually install lombok. And tell us about this. Thanks!", e); throw new InstallException("I can't write to your Eclipse directory, probably because this installer does not have the access rights.", e); } + /* legacy - delete lombok.eclipse.agent.jar if its there, which lombok no longer uses. */ { + new File(lombokJar.getParentFile(), "lombok.eclipse.agent.jar").delete(); + } + try { FileInputStream fis = new FileInputStream(iniFile); try { @@ -357,7 +341,9 @@ final class EclipseLocation { boolean first = true; for ( String elem : m.group(1).split(Pattern.quote(File.pathSeparator)) ) { if ( elem.toLowerCase().endsWith("lombok.jar") ) continue; - if ( elem.toLowerCase().endsWith("lombok.eclipse.agent.jar") ) continue; + /* legacy code -see previous comment that starts with 'legacy' */ { + if ( elem.toLowerCase().endsWith("lombok.eclipse.agent.jar") ) continue; + } if ( first ) first = false; else elemBuilder.append(File.pathSeparator); elemBuilder.append(elem); @@ -374,13 +360,11 @@ final class EclipseLocation { } String fullPathToLombok = fullPathRequired ? (lombokJar.getParentFile().getCanonicalPath() + File.separator) : ""; - String fullPathToAgent = fullPathRequired ? (agentJar.getParentFile().getCanonicalPath() + File.separator) : ""; newContents.append(String.format( - "-javaagent:%slombok.eclipse.agent.jar", fullPathToLombok)).append(OS_NEWLINE); + "-javaagent:%slombok.jar", fullPathToLombok)).append(OS_NEWLINE); newContents.append(String.format( - "-Xbootclasspath/a:%slombok.jar" + File.pathSeparator + "%slombok.eclipse.agent.jar", - fullPathToLombok, fullPathToAgent)).append(OS_NEWLINE); + "-Xbootclasspath/a:%slombok.jar", fullPathToLombok)).append(OS_NEWLINE); FileOutputStream fos = new FileOutputStream(iniFile); try { @@ -395,7 +379,6 @@ final class EclipseLocation { } finally { if ( !installSucceeded ) try { lombokJar.delete(); - agentJar.delete(); } catch ( Throwable ignore ) {} } } @@ -406,12 +389,12 @@ final class EclipseLocation { } for ( File dir : failedDirs ) { - //If we're updating the old installation might have worked by putting the lombok jars in a different place. - //We'll delete these old files. - try { - new File(dir, "lombok.jar").delete(); - new File(dir, "lombok.eclipse.agent.jar").delete(); - } catch ( Throwable ignore ) {} + /* Legacy code - lombok's installer used to install in other places. To keep the user's eclipse dir clean, we'll delete these. */ { + try { + new File(dir, "lombok.jar").delete(); + new File(dir, "lombok.eclipse.agent.jar").delete(); + } catch ( Throwable ignore ) {} + } } } } diff --git a/src/lombok/installer/Installer.java b/src/lombok/installer/Installer.java index 89ebfc37..695de4b7 100644 --- a/src/lombok/installer/Installer.java +++ b/src/lombok/installer/Installer.java @@ -810,10 +810,8 @@ public class Installer { private static final String HOW_I_WORK_EXPLANATION = "
    " + "
  1. First, I copy myself (lombok.jar) to your Eclipse install directory.
  2. " + - "
  3. Then, I unpack lombok.eclipse.agent.jar like so:
    " + - "
    jar xvf lombok.jar lombok.eclipse.agent.jar
  4. " + "
  5. Then, I edit the eclipse.ini file to add the following two entries:
    " + - "
    -Xbootclasspath/a:lombok.jar%1$slombok.eclipse.agent.jar
    " + + "
    -Xbootclasspath/a:lombok.jar
    " + "-javaagent:lombok.jar
" + "
" + "That's all there is to it. Note that on Mac OS X, eclipse.ini is hidden in
" + diff --git a/src_eclipseagent/lombok/eclipse/agent/EclipsePatcher.java b/src_eclipseagent/lombok/eclipse/agent/EclipsePatcher.java index 6efddf61..d1759127 100644 --- a/src_eclipseagent/lombok/eclipse/agent/EclipsePatcher.java +++ b/src_eclipseagent/lombok/eclipse/agent/EclipsePatcher.java @@ -56,7 +56,7 @@ public class EclipsePatcher { ScriptManager sm = new ScriptManager(); sm.registerTransformer(instrumentation); EquinoxClassLoader.getInstance().addPrefix("lombok."); - EquinoxClassLoader.getInstance().registerScripts(sm); + EquinoxClassLoader.registerScripts(sm); patchLombokizeAST(sm); patchAvoidReparsingGeneratedCode(sm); @@ -66,7 +66,7 @@ public class EclipsePatcher { if (reloadExistingClasses) sm.reloadClasses(instrumentation); } - + private static void patchHideGeneratedNodes(ScriptManager sm) { sm.addScript(ScriptBuilder.wrapReturnValue() .target(new MethodTarget("org.eclipse.jdt.internal.corext.dom.LinkedNodeFinder", "findByNode")) @@ -75,6 +75,19 @@ public class EclipsePatcher { "([Lorg/eclipse/jdt/core/dom/SimpleName;)[Lorg/eclipse/jdt/core/dom/SimpleName;")) .request(StackRequest.RETURN_VALUE).build()); + patchRefactorScripts(sm); + patchFormatters(sm); + } + + private static void patchFormatters(ScriptManager sm) { + sm.addScript(ScriptBuilder.setSymbolDuringMethodCall() + .target(new MethodTarget("org.eclipse.jdt.internal.ui.text.java.JavaFormattingStrategy", "format", "void")) + .callToWrap(new Hook("org/eclipse/jdt/internal/corext/util/CodeFormatterUtil", "reformat", + "(ILjava/lang/String;IIILjava/lang/String;Ljava/util/Map;)Lorg/eclipse/text/edits/TextEdit;")) + .symbol("lombok.disable").build()); + } + + private static void patchRefactorScripts(ScriptManager sm) { sm.addScript(ScriptBuilder.exitEarly() .target(new MethodTarget("org.eclipse.jdt.core.dom.rewrite.ASTRewrite", "replace")) .target(new MethodTarget("org.eclipse.jdt.core.dom.rewrite.ASTRewrite", "remove")) @@ -89,14 +102,14 @@ public class EclipsePatcher { "([Lorg/eclipse/jdt/core/IMethod;)[Lorg/eclipse/jdt/core/IMethod;")) .transplant().build()); } - + private static void patchCatchReparse(ScriptManager sm) { 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()); } - + private static void patchSetGeneratedFlag(ScriptManager sm) { sm.addScript(ScriptBuilder.addField() .targetClass("org.eclipse.jdt.internal.compiler.ast.ASTNode") @@ -150,7 +163,7 @@ public class EclipsePatcher { "(Lorg/eclipse/jdt/core/dom/SimpleName;Ljava/lang/Object;)V")) .transplant().build()); } - + private static void patchAvoidReparsingGeneratedCode(ScriptManager sm) { final String PARSER_SIG1 = "org.eclipse.jdt.internal.compiler.parser.Parser"; sm.addScript(ScriptBuilder.exitEarly() @@ -175,7 +188,7 @@ public class EclipsePatcher { .decisionMethod(new Hook("lombok/eclipse/agent/PatchFixes", "checkBit24", "(Ljava/lang/Object;)Z")) .transplant().request(StackRequest.PARAM1).build()); } - + private static void patchLombokizeAST(ScriptManager sm) { sm.addScript(ScriptBuilder.addField() .targetClass("org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration") -- cgit