From 77ff3ca2c3ad9544e242cc0c29817831485eeb0b Mon Sep 17 00:00:00 2001 From: Reinier Zwitserloot Date: Tue, 20 Jul 2010 07:03:07 +0200 Subject: import lombok.AccessLevel is now also removed during delomboking. Also, when NOT running delombok, the javac processors no longer delete the lombok annotations as they process. This is particularly relevant for netbeans. This fixes issue #100 and #103. --- doc/changelog.markdown | 1 + src/core/lombok/javac/JavacNode.java | 6 ++++ .../lombok/javac/handlers/HandleConstructor.java | 3 ++ src/core/lombok/javac/handlers/HandleGetter.java | 1 + src/core/lombok/javac/handlers/HandleSetter.java | 1 + .../lombok/javac/handlers/JavacHandlerUtil.java | 8 +++-- .../lombok/delombok/CommentPreservingParser.java | 10 +++++-- .../lombok/delombok/DeleteLombokAnnotations.java | 35 ++++++++++++++++++++++ src/delombok/lombok/delombok/Delombok.java | 7 ++++- 9 files changed, 66 insertions(+), 6 deletions(-) create mode 100644 src/delombok/lombok/delombok/DeleteLombokAnnotations.java diff --git a/doc/changelog.markdown b/doc/changelog.markdown index 2acfd7fd..5aa39970 100644 --- a/doc/changelog.markdown +++ b/doc/changelog.markdown @@ -14,6 +14,7 @@ Lombok Changelog * ENHANCEMENT: All fields and methods generated by lombok now get `@SuppressWarnings("all")` attached to avoid such warnings as missing javadoc, for those of you who have that warning enabled. [Issue #47](http://code.google.com/p/projectlombok/issues/detail?id=47) * FEATURE: Three new annotations, `@NoArgsConstructor`, `@RequiredArgsConstructor` and `@AllArgsConstructor` have been added. These split off `@Data`'s ability to generate constructors, and also allow you to finetune what kind of constructor you want. In addition, by using these annotations, you can force generation of constructors even if you have your own. [Issue #79](http://code.google.com/p/projectlombok/issues/detail?id=79) * FEATURE: Constructors generated by lombok now include a `@java.beans.ConstructorProperties` annotation. This does mean these constructors no longer work in java 1.5, as this is a java 1.6 feature. The annotation can be suppressed by setting `suppressConstructorProperties` to `true` in a `@RequiredArgsConstructor` or `@AllArgsConstructor` annotation. [Issue #122](http://code.google.com/p/projectlombok/issues/detail?id=122) +* BUGFIX: delombok now no longer forgets to remove `import lombok.AccessLevel;`. In netbeans, that import will no longer be flagged erroneously as being unused. [Issue #100](http://code.google.com/p/projectlombok/issues/detail?id=100) and [Issue #103](http://code.google.com/p/projectlombok/issues/detail?id=103) ### v0.9.2 "Hailbunny" (December 15th, 2009) * preliminary support for lombok on NetBeans! - thanks go to Jan Lahoda from NetBeans. [Issue #20](http://code.google.com/p/projectlombok/issues/detail?id=20) diff --git a/src/core/lombok/javac/JavacNode.java b/src/core/lombok/javac/JavacNode.java index a0ee2789..3d7ce73f 100644 --- a/src/core/lombok/javac/JavacNode.java +++ b/src/core/lombok/javac/JavacNode.java @@ -26,6 +26,7 @@ import java.util.List; import javax.tools.Diagnostic; import lombok.core.AST.Kind; +import lombok.delombok.DeleteLombokAnnotations; import com.sun.tools.javac.code.Symtab; import com.sun.tools.javac.tree.JCTree; @@ -173,6 +174,11 @@ public class JavacNode extends lombok.core.LombokNode { @Override public boolean handle(AnnotationValues annotation, JCAnnotation ast, JavacNode annotationNode) { markAnnotationAsProcessed(annotationNode, NoArgsConstructor.class); + deleteImportFromCompilationUnit(annotationNode, "lombok.AccessLevel"); JavacNode typeNode = annotationNode.up(); NoArgsConstructor ann = annotation.getInstance(); AccessLevel level = ann.access(); @@ -73,6 +74,7 @@ public class HandleConstructor { public static class HandleRequiredArgsConstructor implements JavacAnnotationHandler { @Override public boolean handle(AnnotationValues annotation, JCAnnotation ast, JavacNode annotationNode) { markAnnotationAsProcessed(annotationNode, RequiredArgsConstructor.class); + deleteImportFromCompilationUnit(annotationNode, "lombok.AccessLevel"); JavacNode typeNode = annotationNode.up(); RequiredArgsConstructor ann = annotation.getInstance(); AccessLevel level = ann.access(); @@ -102,6 +104,7 @@ public class HandleConstructor { public static class HandleAllArgsConstructor implements JavacAnnotationHandler { @Override public boolean handle(AnnotationValues annotation, JCAnnotation ast, JavacNode annotationNode) { markAnnotationAsProcessed(annotationNode, AllArgsConstructor.class); + deleteImportFromCompilationUnit(annotationNode, "lombok.AccessLevel"); JavacNode typeNode = annotationNode.up(); AllArgsConstructor ann = annotation.getInstance(); AccessLevel level = ann.access(); diff --git a/src/core/lombok/javac/handlers/HandleGetter.java b/src/core/lombok/javac/handlers/HandleGetter.java index 16185e9c..9c0ce32f 100644 --- a/src/core/lombok/javac/handlers/HandleGetter.java +++ b/src/core/lombok/javac/handlers/HandleGetter.java @@ -95,6 +95,7 @@ public class HandleGetter implements JavacAnnotationHandler { @Override public boolean handle(AnnotationValues annotation, JCAnnotation ast, JavacNode annotationNode) { markAnnotationAsProcessed(annotationNode, Getter.class); + deleteImportFromCompilationUnit(annotationNode, "lombok.AccessLevel"); JavacNode node = annotationNode.up(); AccessLevel level = annotation.getInstance().value(); if (level == AccessLevel.NONE) return true; diff --git a/src/core/lombok/javac/handlers/HandleSetter.java b/src/core/lombok/javac/handlers/HandleSetter.java index a3822daa..dfc9c8ba 100644 --- a/src/core/lombok/javac/handlers/HandleSetter.java +++ b/src/core/lombok/javac/handlers/HandleSetter.java @@ -104,6 +104,7 @@ public class HandleSetter implements JavacAnnotationHandler { @Override public boolean handle(AnnotationValues annotation, JCAnnotation ast, JavacNode annotationNode) { markAnnotationAsProcessed(annotationNode, Setter.class); + deleteImportFromCompilationUnit(annotationNode, "lombok.AccessLevel"); JavacNode node = annotationNode.up(); AccessLevel level = annotation.getInstance().value(); diff --git a/src/core/lombok/javac/handlers/JavacHandlerUtil.java b/src/core/lombok/javac/handlers/JavacHandlerUtil.java index a4949a58..0df48358 100644 --- a/src/core/lombok/javac/handlers/JavacHandlerUtil.java +++ b/src/core/lombok/javac/handlers/JavacHandlerUtil.java @@ -92,13 +92,15 @@ public class JavacHandlerUtil { return; } - JCCompilationUnit unit = (JCCompilationUnit) annotation.top().get(); - deleteImportFromCompilationUnit(unit, annotationType.getName()); + deleteImportFromCompilationUnit(annotation, annotationType.getName()); } - private static void deleteImportFromCompilationUnit(JCCompilationUnit unit, String name) { + public static void deleteImportFromCompilationUnit(JavacNode node, String name) { + if (!node.shouldDeleteLombokAnnotations()) return; List newDefs = List.nil(); + JCCompilationUnit unit = (JCCompilationUnit) node.top().get(); + for (JCTree def : unit.defs) { boolean delete = false; if (def instanceof JCImport) { diff --git a/src/delombok/lombok/delombok/CommentPreservingParser.java b/src/delombok/lombok/delombok/CommentPreservingParser.java index 186d70b0..8536075d 100644 --- a/src/delombok/lombok/delombok/CommentPreservingParser.java +++ b/src/delombok/lombok/delombok/CommentPreservingParser.java @@ -1,5 +1,5 @@ /* - * Copyright © 2009 Reinier Zwitserloot and Roel Spilker. + * Copyright © 2009-2010 Reinier Zwitserloot and Roel Spilker. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -44,6 +44,7 @@ import com.sun.tools.javac.util.Options; public class CommentPreservingParser { private final String encoding; + private boolean deleteLombokAnnotations = false; public CommentPreservingParser() { this("utf-8"); @@ -53,6 +54,10 @@ public class CommentPreservingParser { this.encoding = encoding; } + public void setDeleteLombokAnnotations(boolean deleteLombokAnnotations) { + this.deleteLombokAnnotations = deleteLombokAnnotations; + } + public ParseResult parse(JavaFileObject source, boolean forceProcessing) throws IOException { return doParse(source, forceProcessing); } @@ -78,7 +83,8 @@ public class CommentPreservingParser { Comments comments = new Comments(); context.put(Comments.class, comments); - + if (deleteLombokAnnotations) context.put(DeleteLombokAnnotations.class, new DeleteLombokAnnotations(true)); + comments.comments = List.nil(); JCCompilationUnit cu; diff --git a/src/delombok/lombok/delombok/DeleteLombokAnnotations.java b/src/delombok/lombok/delombok/DeleteLombokAnnotations.java new file mode 100644 index 00000000..335cf6e9 --- /dev/null +++ b/src/delombok/lombok/delombok/DeleteLombokAnnotations.java @@ -0,0 +1,35 @@ +/* + * Copyright © 2010 Reinier Zwitserloot and Roel Spilker. + * + * 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. + */ +package lombok.delombok; + +/** Used as marker in javac's Context object when delombok is running to signal that all lombok annotations should be deleted as they are processed. */ +public class DeleteLombokAnnotations { + private final boolean deleteLombokAnnotations; + + public DeleteLombokAnnotations(boolean deleteLombokAnnotations) { + this.deleteLombokAnnotations = deleteLombokAnnotations; + } + + public boolean isDeleteLombokAnnotations() { + return deleteLombokAnnotations; + } +} diff --git a/src/delombok/lombok/delombok/Delombok.java b/src/delombok/lombok/delombok/Delombok.java index b38d3ef5..4ec5e270 100644 --- a/src/delombok/lombok/delombok/Delombok.java +++ b/src/delombok/lombok/delombok/Delombok.java @@ -1,5 +1,5 @@ /* - * Copyright © 2009 Reinier Zwitserloot and Roel Spilker. + * Copyright © 2009-2010 Reinier Zwitserloot and Roel Spilker. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -50,6 +50,11 @@ import com.zwitserloot.cmdreader.Shorthand; public class Delombok { private Charset charset = Charset.defaultCharset(); private CommentPreservingParser parser = new CommentPreservingParser(); + + { + parser.setDeleteLombokAnnotations(true); + } + private PrintStream feedback = System.err; private boolean verbose; private boolean noCopy; -- cgit