diff options
7 files changed, 89 insertions, 20 deletions
diff --git a/src/core/lombok/javac/apt/InterceptingJavaFileManager.java b/src/core/lombok/javac/apt/InterceptingJavaFileManager.java index 87aeb927..979c1279 100644 --- a/src/core/lombok/javac/apt/InterceptingJavaFileManager.java +++ b/src/core/lombok/javac/apt/InterceptingJavaFileManager.java @@ -43,12 +43,12 @@ final class InterceptingJavaFileManager extends ForwardingJavaFileManager<JavaFi @Override public JavaFileObject getJavaFileForOutput(Location location, String className, final Kind kind, FileObject sibling) throws IOException { if (className.startsWith("lombok.dummy.ForceNewRound")) { final String name = className.replace(".", "/") + kind.extension; - return LombokFileObjects.createEmpty(fileManager, name, kind); + return LombokFileObjects.createEmpty(name, kind); } JavaFileObject fileObject = fileManager.getJavaFileForOutput(location, className, kind, sibling); if (kind != Kind.CLASS) { return fileObject; } - return LombokFileObjects.createIntercepting(fileManager, fileObject, className, diagnostics); + return LombokFileObjects.createIntercepting(fileObject, className, diagnostics); } }
\ No newline at end of file diff --git a/src/core/lombok/javac/apt/Javac7BaseFileObjectWrapper.java b/src/core/lombok/javac/apt/Javac7BaseFileObjectWrapper.java index 81cf834a..354e2e01 100644 --- a/src/core/lombok/javac/apt/Javac7BaseFileObjectWrapper.java +++ b/src/core/lombok/javac/apt/Javac7BaseFileObjectWrapper.java @@ -36,8 +36,8 @@ import javax.lang.model.element.NestingKind; class Javac7BaseFileObjectWrapper extends com.sun.tools.javac.file.BaseFileObject { private final LombokFileObject delegate; - public Javac7BaseFileObjectWrapper(com.sun.tools.javac.file.JavacFileManager jfm, LombokFileObject delegate) { - super(jfm); + public Javac7BaseFileObjectWrapper(LombokFileObject delegate) { + super(null); this.delegate = delegate; } diff --git a/src/core/lombok/javac/apt/LombokFileObjects.java b/src/core/lombok/javac/apt/LombokFileObjects.java index b868c847..ef1a6b56 100644 --- a/src/core/lombok/javac/apt/LombokFileObjects.java +++ b/src/core/lombok/javac/apt/LombokFileObjects.java @@ -24,7 +24,6 @@ package lombok.javac.apt; import java.lang.reflect.Method; -import javax.tools.JavaFileManager; import javax.tools.JavaFileObject; import javax.tools.JavaFileObject.Kind; @@ -36,17 +35,17 @@ final class LombokFileObjects { private enum Compiler { JAVAC6 { - @Override public JavaFileObject wrap(JavaFileManager fileManager, LombokFileObject fileObject) { + @Override public JavaFileObject wrap(LombokFileObject fileObject) { return new Javac6BaseFileObjectWrapper(fileObject); } }, JAVAC7 { - @Override public JavaFileObject wrap(JavaFileManager fileManager, LombokFileObject fileObject) { - return new Javac7BaseFileObjectWrapper((com.sun.tools.javac.file.JavacFileManager)fileManager, fileObject); + @Override public JavaFileObject wrap(LombokFileObject fileObject) { + return new Javac7BaseFileObjectWrapper(fileObject); } }; - abstract JavaFileObject wrap(JavaFileManager fileManager, LombokFileObject fileObject); + abstract JavaFileObject wrap(LombokFileObject fileObject); } private static final Compiler compiler; @@ -85,11 +84,11 @@ final class LombokFileObjects { private LombokFileObjects() {} - static JavaFileObject createEmpty(JavaFileManager fileManager, String name, Kind kind) { - return compiler.wrap(fileManager, new EmptyLombokFileObject(name, kind)); + static JavaFileObject createEmpty(String name, Kind kind) { + return compiler.wrap(new EmptyLombokFileObject(name, kind)); } - static JavaFileObject createIntercepting(JavaFileManager fileManager, JavaFileObject delegate, String fileName, DiagnosticsReceiver diagnostics) { - return compiler.wrap(fileManager, new InterceptingJavaFileObject(delegate, fileName, diagnostics, decoderMethod)); + static JavaFileObject createIntercepting(JavaFileObject delegate, String fileName, DiagnosticsReceiver diagnostics) { + return compiler.wrap(new InterceptingJavaFileObject(delegate, fileName, diagnostics, decoderMethod)); } } diff --git a/src/core/lombok/javac/apt/Processor.java b/src/core/lombok/javac/apt/Processor.java index b5d5c81c..ee9a9704 100644 --- a/src/core/lombok/javac/apt/Processor.java +++ b/src/core/lombok/javac/apt/Processor.java @@ -87,8 +87,9 @@ public class Processor extends AbstractProcessor { private void placePostCompileAndDontMakeForceRoundDummiesHook() { stopJavacProcessingEnvironmentFromClosingOurClassloader(); + forceMultipleRoundsInNetBeansEditor(); Context context = processingEnv.getContext(); - + disablePartialReparseInNetBeansEditor(context); try { Method keyMethod = Context.class.getDeclaredMethod("key", Class.class); keyMethod.setAccessible(true); @@ -114,6 +115,39 @@ public class Processor extends AbstractProcessor { } } + private void forceMultipleRoundsInNetBeansEditor() { + try { + Field f = JavacProcessingEnvironment.class.getDeclaredField("isBackgroundCompilation"); + f.setAccessible(true); + f.set(processingEnv, true); + } catch (NoSuchFieldException e) { + // only NetBeans has it + } catch (Throwable t) { + throw Lombok.sneakyThrow(t); + } + } + + private void disablePartialReparseInNetBeansEditor(Context context) { + try { + Class<?> cancelServiceClass = Class.forName("com.sun.tools.javac.util.CancelService"); + Method cancelServiceInstace = cancelServiceClass.getDeclaredMethod("instance", Context.class); + Object cancelService = cancelServiceInstace.invoke(null, context); + if (cancelService == null) return; + Field parserField = cancelService.getClass().getDeclaredField("parser"); + parserField.setAccessible(true); + Object parser = parserField.get(cancelService); + Field supportsReparseField = parser.getClass().getDeclaredField("supportsReparse"); + supportsReparseField.setAccessible(true); + supportsReparseField.set(parser, false); + } catch (ClassNotFoundException e) { + // only NetBeans has it + } catch (NoSuchFieldException e) { + // only NetBeans has it + } catch (Throwable t) { + throw Lombok.sneakyThrow(t); + } + } + private static class WrappingClassLoader extends ClassLoader { private final ClassLoader parent; diff --git a/src/eclipseAgent/lombok/eclipse/agent/EclipsePatcher.java b/src/eclipseAgent/lombok/eclipse/agent/EclipsePatcher.java index 257137ae..f67bd4e3 100644 --- a/src/eclipseAgent/lombok/eclipse/agent/EclipsePatcher.java +++ b/src/eclipseAgent/lombok/eclipse/agent/EclipsePatcher.java @@ -328,6 +328,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() @@ -363,6 +364,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; } } |