From 489f506ba1f9ec99892797a912867322ea7d6478 Mon Sep 17 00:00:00 2001 From: grootjans Date: Tue, 17 May 2011 01:42:02 +0200 Subject: Added a layer of indirection between the patched calls for delegate to guard against situations in which lombok can't possibly work, since there is not enough of eclipse on the classpath. Major example of this is when we are called through jsps in the eclipse help. Also added an extra check before the patchval is performed by checking if previous patch calls failed in TransformEclipseAST. This fixes Issue #207. --- src/core/lombok/eclipse/TransformEclipseAST.java | 2 +- .../lombok/eclipse/agent/EclipsePatcher.java | 10 +-- .../lombok/eclipse/agent/PatchDelegate.java | 2 + .../eclipse/agent/PatchValEclipsePortal.java | 99 ++++++++++++++++++++++ .../lombok/installer/eclipse/EclipseFinder.java | 2 +- .../installer/eclipse/EclipseLocationProvider.java | 2 +- 6 files changed, 109 insertions(+), 8 deletions(-) create mode 100644 src/eclipseAgent/lombok/eclipse/agent/PatchValEclipsePortal.java (limited to 'src') diff --git a/src/core/lombok/eclipse/TransformEclipseAST.java b/src/core/lombok/eclipse/TransformEclipseAST.java index 443969f8..aada3ca4 100644 --- a/src/core/lombok/eclipse/TransformEclipseAST.java +++ b/src/core/lombok/eclipse/TransformEclipseAST.java @@ -53,7 +53,7 @@ public class TransformEclipseAST { private static final Field astCacheField; private static final HandlerLibrary handlers; - private static boolean disableLombok = false; + public static boolean disableLombok = false; static { Field f = null; diff --git a/src/eclipseAgent/lombok/eclipse/agent/EclipsePatcher.java b/src/eclipseAgent/lombok/eclipse/agent/EclipsePatcher.java index ae9c08c1..4efc6536 100644 --- a/src/eclipseAgent/lombok/eclipse/agent/EclipsePatcher.java +++ b/src/eclipseAgent/lombok/eclipse/agent/EclipsePatcher.java @@ -336,20 +336,20 @@ public class EclipsePatcher extends Agent { sm.addScript(ScriptBuilder.wrapReturnValue() .target(new MethodTarget(PARSER_SIG, "consumeExitVariableWithInitialization", "void")) .request(StackRequest.THIS) - .wrapMethod(new Hook("lombok.eclipse.agent.PatchValEclipse", "copyInitializationOfLocalDeclaration", "void", PARSER_SIG)) + .wrapMethod(new Hook("lombok.eclipse.agent.PatchValEclipsePortal", "copyInitializationOfLocalDeclaration", "void", "java.lang.Object")) .build()); sm.addScript(ScriptBuilder.wrapReturnValue() .target(new MethodTarget(PARSER_SIG, "consumeEnhancedForStatementHeader", "void")) .request(StackRequest.THIS) - .wrapMethod(new Hook("lombok.eclipse.agent.PatchValEclipse", "copyInitializationOfForEachIterable", "void", PARSER_SIG)) + .wrapMethod(new Hook("lombok.eclipse.agent.PatchValEclipsePortal", "copyInitializationOfForEachIterable", "void", "java.lang.Object")) .build()); sm.addScript(ScriptBuilder.wrapReturnValue() .target(new MethodTarget(ASTCONVERTER_SIG, "setModifiers", "void", VARIABLEDECLARATIONSTATEMENT_SIG, LOCALDECLARATION_SIG)) - .wrapMethod(new Hook("lombok.eclipse.agent.PatchValEclipse", "addFinalAndValAnnotationToVariableDeclarationStatement", - "void", "java.lang.Object", VARIABLEDECLARATIONSTATEMENT_SIG, LOCALDECLARATION_SIG)) - .transplant().request(StackRequest.THIS, StackRequest.PARAM1, StackRequest.PARAM2).build()); + .wrapMethod(new Hook("lombok.eclipse.agent.PatchValEclipsePortal", "addFinalAndValAnnotationToVariableDeclarationStatement", + "void", "java.lang.Object", "java.lang.Object", "java.lang.Object")) + .request(StackRequest.THIS, StackRequest.PARAM1, StackRequest.PARAM2).build()); } private static void addPatchesForVal(ScriptManager sm) { diff --git a/src/eclipseAgent/lombok/eclipse/agent/PatchDelegate.java b/src/eclipseAgent/lombok/eclipse/agent/PatchDelegate.java index 5680e8e1..ccc00f5d 100644 --- a/src/eclipseAgent/lombok/eclipse/agent/PatchDelegate.java +++ b/src/eclipseAgent/lombok/eclipse/agent/PatchDelegate.java @@ -102,6 +102,8 @@ public class PatchDelegate { } public static boolean handleDelegateForType(ClassScope scope) { + if (TransformEclipseAST.disableLombok) return false; + List stack = visited.get(); StringBuilder corrupted = null; for (ClassScopeEntry entry : stack) { diff --git a/src/eclipseAgent/lombok/eclipse/agent/PatchValEclipsePortal.java b/src/eclipseAgent/lombok/eclipse/agent/PatchValEclipsePortal.java new file mode 100644 index 00000000..cca1d4d0 --- /dev/null +++ b/src/eclipseAgent/lombok/eclipse/agent/PatchValEclipsePortal.java @@ -0,0 +1,99 @@ +package lombok.eclipse.agent; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; + +import lombok.Lombok; + +/* + * Copyright © 2011 Reinier Zwitserloot, Roel Spilker and Robbert Jan Grootjans. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +public class PatchValEclipsePortal { + static final String LOCALDECLARATION_SIG = "org.eclipse.jdt.internal.compiler.ast.LocalDeclaration"; + static final String PARSER_SIG = "org.eclipse.jdt.internal.compiler.parser.Parser"; + static final String VARIABLEDECLARATIONSTATEMENT_SIG = "org.eclipse.jdt.core.dom.VariableDeclarationStatement"; + static final String ASTCONVERTER_SIG = "org.eclipse.jdt.core.dom.ASTConverter"; + + public static void copyInitializationOfForEachIterable(Object parser) { + try { + Reflection.copyInitializationOfForEachIterable.invoke(null, parser); + } catch (NoClassDefFoundError e) { + //ignore, we don't have access to the correct ECJ classes, so lombok can't possibly + //do anything useful here. + } catch (IllegalAccessException e) { + Lombok.sneakyThrow(e); + } catch (InvocationTargetException e) { + Lombok.sneakyThrow(e); + } + } + + + public static void copyInitializationOfLocalDeclaration(Object parser) { + try { + Reflection.copyInitializationOfLocalDeclaration.invoke(null, parser); + } catch (NoClassDefFoundError e) { + //ignore, we don't have access to the correct ECJ classes, so lombok can't possibly + //do anything useful here. + } catch (IllegalAccessException e) { + Lombok.sneakyThrow(e); + } catch (InvocationTargetException e) { + Lombok.sneakyThrow(e); + } + } + + public static void addFinalAndValAnnotationToVariableDeclarationStatement(Object converter, Object out, Object in) { + try { + Reflection.addFinalAndValAnnotationToVariableDeclarationStatement.invoke(null, converter, out, in); + } catch (NoClassDefFoundError e) { + //ignore, we don't have access to the correct ECJ classes, so lombok can't possibly + //do anything useful here. + } catch (IllegalAccessException e) { + Lombok.sneakyThrow(e); + } catch (InvocationTargetException e) { + Lombok.sneakyThrow(e); + } + } + + private static final class Reflection { + public static final Method copyInitializationOfForEachIterable; + public static final Method copyInitializationOfLocalDeclaration; + public static final Method addFinalAndValAnnotationToVariableDeclarationStatement; + + static { + Method m = null, n = null, o = null; + try { + m = PatchValEclipse.class.getMethod("copyInitializationOfForEachIterable", Class.forName(PARSER_SIG)); + n = PatchValEclipse.class.getMethod("copyInitializationOfLocalDeclaration", Class.forName(PARSER_SIG)); + o = PatchValEclipse.class.getMethod("addFinalAndValAnnotationToVariableDeclarationStatement", + Object.class, + Class.forName(VARIABLEDECLARATIONSTATEMENT_SIG), + Class.forName(LOCALDECLARATION_SIG)); + } catch (Exception e) { + // That's problematic, but as long as no local classes are used we don't actually need it. + // Better fail on local classes than crash altogether. + } + copyInitializationOfForEachIterable = m; + copyInitializationOfLocalDeclaration = n; + addFinalAndValAnnotationToVariableDeclarationStatement = o; + + } + } +} diff --git a/src/installer/lombok/installer/eclipse/EclipseFinder.java b/src/installer/lombok/installer/eclipse/EclipseFinder.java index cf8c5149..e7716281 100644 --- a/src/installer/lombok/installer/eclipse/EclipseFinder.java +++ b/src/installer/lombok/installer/eclipse/EclipseFinder.java @@ -1,5 +1,5 @@ /* - * Copyright © 2009 Reinier Zwitserloot and Roel Spilker. + * Copyright © 2009-2011 Reinier Zwitserloot, Roel Spilker and Robbert Jan Grootjans. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal diff --git a/src/installer/lombok/installer/eclipse/EclipseLocationProvider.java b/src/installer/lombok/installer/eclipse/EclipseLocationProvider.java index 29c3b918..2108ea60 100644 --- a/src/installer/lombok/installer/eclipse/EclipseLocationProvider.java +++ b/src/installer/lombok/installer/eclipse/EclipseLocationProvider.java @@ -1,5 +1,5 @@ /* - * Copyright © 2009 Reinier Zwitserloot and Roel Spilker. + * Copyright © 2009-2011 Reinier Zwitserloot, Roel Spilker and Robbert Jan Grootjans. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal -- cgit