diff options
author | grootjans <grootjans@gmail.com> | 2011-07-18 22:45:29 +0200 |
---|---|---|
committer | grootjans <grootjans@gmail.com> | 2011-07-18 22:45:48 +0200 |
commit | 931959c89addb8213594033ed43da734c426f3a4 (patch) | |
tree | 2c6033fbd78f5d4989653fe5014f45656f70f97e /src/eclipseAgent/lombok | |
parent | 43d938a895323e3bae807ea534de10a13b66ad24 (diff) | |
download | lombok-931959c89addb8213594033ed43da734c426f3a4.tar.gz lombok-931959c89addb8213594033ed43da734c426f3a4.tar.bz2 lombok-931959c89addb8213594033ed43da734c426f3a4.zip |
Fixes issue 232, now we also patch dom.SingleVariableDecls. Fixes val import disappearing when organizing imports in Eclipse, when using val in a foreach.
Diffstat (limited to 'src/eclipseAgent/lombok')
3 files changed, 42 insertions, 6 deletions
diff --git a/src/eclipseAgent/lombok/eclipse/agent/EclipsePatcher.java b/src/eclipseAgent/lombok/eclipse/agent/EclipsePatcher.java index ba8e6813..c49346bc 100644 --- a/src/eclipseAgent/lombok/eclipse/agent/EclipsePatcher.java +++ b/src/eclipseAgent/lombok/eclipse/agent/EclipsePatcher.java @@ -315,6 +315,7 @@ public class EclipsePatcher extends Agent { final String LOCALDECLARATION_SIG = "org.eclipse.jdt.internal.compiler.ast.LocalDeclaration"; final String PARSER_SIG = "org.eclipse.jdt.internal.compiler.parser.Parser"; final String VARIABLEDECLARATIONSTATEMENT_SIG = "org.eclipse.jdt.core.dom.VariableDeclarationStatement"; + final String SINGLEVARIABLEDECLARATION_SIG = "org.eclipse.jdt.core.dom.SingleVariableDeclaration"; final String ASTCONVERTER_SIG = "org.eclipse.jdt.core.dom.ASTConverter"; sm.addScript(ScriptBuilder.addField() @@ -350,6 +351,12 @@ public class EclipsePatcher extends Agent { .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()); + + sm.addScript(ScriptBuilder.wrapReturnValue() + .target(new MethodTarget(ASTCONVERTER_SIG, "setModifiers", "void", SINGLEVARIABLEDECLARATION_SIG, LOCALDECLARATION_SIG)) + .wrapMethod(new Hook("lombok.eclipse.agent.PatchValEclipsePortal", "addFinalAndValAnnotationToSingleVariableDeclaration", + "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/PatchValEclipse.java b/src/eclipseAgent/lombok/eclipse/agent/PatchValEclipse.java index 3fb407b9..ac3b58a0 100644 --- a/src/eclipseAgent/lombok/eclipse/agent/PatchValEclipse.java +++ b/src/eclipseAgent/lombok/eclipse/agent/PatchValEclipse.java @@ -35,6 +35,7 @@ import org.eclipse.jdt.core.dom.AST; import org.eclipse.jdt.core.dom.IExtendedModifier; import org.eclipse.jdt.core.dom.MarkerAnnotation; import org.eclipse.jdt.core.dom.Modifier; +import org.eclipse.jdt.core.dom.SingleVariableDeclaration; import org.eclipse.jdt.core.dom.Modifier.ModifierKeyword; import org.eclipse.jdt.core.dom.Name; import org.eclipse.jdt.core.dom.VariableDeclarationStatement; @@ -101,7 +102,17 @@ public class PatchValEclipse { } } + public static void addFinalAndValAnnotationToSingleVariableDeclaration(Object converter, SingleVariableDeclaration out, LocalDeclaration in) { + @SuppressWarnings("unchecked") List<IExtendedModifier> modifiers = out.modifiers(); + addFinalAndValAnnotationToModifierList(converter, modifiers, out.getAST(), in); + } + public static void addFinalAndValAnnotationToVariableDeclarationStatement(Object converter, VariableDeclarationStatement out, LocalDeclaration in) { + @SuppressWarnings("unchecked") List<IExtendedModifier> modifiers = out.modifiers(); + addFinalAndValAnnotationToModifierList(converter, modifiers, out.getAST(), in); + } + + public static void addFinalAndValAnnotationToModifierList(Object converter, List<IExtendedModifier> modifiers, AST ast, LocalDeclaration in) { // First check that 'in' has the final flag on, and a @val / @lombok.val annotation. if ((in.modifiers & ClassFileConstants.AccFinal) == 0) return; if (in.annotations == null) return; @@ -130,8 +141,6 @@ public class PatchValEclipse { // Now check that 'out' is missing either of these. - @SuppressWarnings("unchecked") List<IExtendedModifier> modifiers = out.modifiers(); - if (modifiers == null) return; // This is null only if the project is 1.4 or less. Lombok doesn't work in that. boolean finalIsPresent = false; boolean valIsPresent = false; @@ -156,11 +165,11 @@ public class PatchValEclipse { if (!finalIsPresent) { modifiers.add( - createModifier(out.getAST(), ModifierKeyword.FINAL_KEYWORD, valAnnotation.sourceStart, valAnnotation.sourceEnd)); + createModifier(ast, ModifierKeyword.FINAL_KEYWORD, valAnnotation.sourceStart, valAnnotation.sourceEnd)); } if (!valIsPresent) { - MarkerAnnotation newAnnotation = createValAnnotation(out.getAST(), valAnnotation, valAnnotation.sourceStart, valAnnotation.sourceEnd); + MarkerAnnotation newAnnotation = createValAnnotation(ast, valAnnotation, valAnnotation.sourceStart, valAnnotation.sourceEnd); try { Reflection.astConverterRecordNodes.invoke(converter, newAnnotation, valAnnotation); Reflection.astConverterRecordNodes.invoke(converter, newAnnotation.getTypeName(), valAnnotation.type); diff --git a/src/eclipseAgent/lombok/eclipse/agent/PatchValEclipsePortal.java b/src/eclipseAgent/lombok/eclipse/agent/PatchValEclipsePortal.java index cca1d4d0..8c308912 100644 --- a/src/eclipseAgent/lombok/eclipse/agent/PatchValEclipsePortal.java +++ b/src/eclipseAgent/lombok/eclipse/agent/PatchValEclipsePortal.java @@ -30,6 +30,7 @@ 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 SINGLEVARIABLEDECLARATION_SIG = "org.eclipse.jdt.core.dom.SingleVariableDeclaration"; static final String ASTCONVERTER_SIG = "org.eclipse.jdt.core.dom.ASTConverter"; public static void copyInitializationOfForEachIterable(Object parser) { @@ -72,13 +73,27 @@ public class PatchValEclipsePortal { } } + public static void addFinalAndValAnnotationToSingleVariableDeclaration(Object converter, Object out, Object in) { + try { + Reflection.addFinalAndValAnnotationToSingleVariableDeclaration.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; + public static final Method addFinalAndValAnnotationToVariableDeclarationStatement; + public static final Method addFinalAndValAnnotationToSingleVariableDeclaration; static { - Method m = null, n = null, o = null; + Method m = null, n = null, o = null, p = null; try { m = PatchValEclipse.class.getMethod("copyInitializationOfForEachIterable", Class.forName(PARSER_SIG)); n = PatchValEclipse.class.getMethod("copyInitializationOfLocalDeclaration", Class.forName(PARSER_SIG)); @@ -86,6 +101,10 @@ public class PatchValEclipsePortal { Object.class, Class.forName(VARIABLEDECLARATIONSTATEMENT_SIG), Class.forName(LOCALDECLARATION_SIG)); + p = PatchValEclipse.class.getMethod("addFinalAndValAnnotationToSingleVariableDeclaration", + Object.class, + Class.forName(SINGLEVARIABLEDECLARATION_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. @@ -93,6 +112,7 @@ public class PatchValEclipsePortal { copyInitializationOfForEachIterable = m; copyInitializationOfLocalDeclaration = n; addFinalAndValAnnotationToVariableDeclarationStatement = o; + addFinalAndValAnnotationToSingleVariableDeclaration = p; } } |