From 6cd3b8a8ca4809efd2001473f31c881c9f2381ea Mon Sep 17 00:00:00 2001 From: Roel Spilker Date: Thu, 26 Nov 2009 01:45:55 +0100 Subject: Major restructuring+adding tests --- test/delombok/resource/after/WithComments.java | 5 ++ test/delombok/resource/before/WithComments.java | 4 + .../src/lombok/delombok/TestSourceFiles.java | 97 ++++++++++++++++++++++ 3 files changed, 106 insertions(+) create mode 100644 test/delombok/resource/after/WithComments.java create mode 100644 test/delombok/resource/before/WithComments.java create mode 100644 test/delombok/src/lombok/delombok/TestSourceFiles.java (limited to 'test') diff --git a/test/delombok/resource/after/WithComments.java b/test/delombok/resource/after/WithComments.java new file mode 100644 index 00000000..0f9ddb9f --- /dev/null +++ b/test/delombok/resource/after/WithComments.java @@ -0,0 +1,5 @@ +// Cool Comments + +public class WithComments { + // Also inside the body +} \ No newline at end of file diff --git a/test/delombok/resource/before/WithComments.java b/test/delombok/resource/before/WithComments.java new file mode 100644 index 00000000..22d044b3 --- /dev/null +++ b/test/delombok/resource/before/WithComments.java @@ -0,0 +1,4 @@ +// Cool Comments +public class WithComments { + // Also inside the body +} diff --git a/test/delombok/src/lombok/delombok/TestSourceFiles.java b/test/delombok/src/lombok/delombok/TestSourceFiles.java new file mode 100644 index 00000000..90c9b543 --- /dev/null +++ b/test/delombok/src/lombok/delombok/TestSourceFiles.java @@ -0,0 +1,97 @@ +/* + * Copyright © 2009 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; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.io.StringWriter; + +import lombok.delombok.CommentPreservingParser.ParseResult; + +import org.junit.BeforeClass; +import org.junit.Test; +import static org.junit.Assert.*; + +import com.sun.tools.javac.main.OptionName; +import com.sun.tools.javac.util.Context; +import com.sun.tools.javac.util.Options; + +public class TestSourceFiles { + + private static CommentPreservingParser parser; + + private static final File BEFORE_FOLDER = new File("test/delombok/resource/before"); + private static final File AFTER_FOLDER = new File("test/delombok/resource/after"); + + private static final String LINE_SEPARATOR = System.getProperty("line.separator"); + + @BeforeClass + public static void init() { + Context c = new Context(); + Options.instance(c).put(OptionName.ENCODING, "utf-8"); + parser = new CommentPreservingParser(c); + } + + @Test + public void testSources() throws Exception { + File[] listFiles = BEFORE_FOLDER.listFiles(); + for (File file : listFiles) { + ParseResult parseResult = parser.parseFile(file.toString()); + StringWriter writer = new StringWriter(); + parseResult.print(writer); + compare(file.getName(), readAfter(file), writer.toString()); + } + } + + private void compare(String name, String expectedFile, String actualFile) { + String[] expectedLines = expectedFile.split("(\\r?\\n)"); + String[] actualLines = actualFile.split("(\\r?\\n)"); + int size = Math.min(expectedLines.length, actualLines.length); + for (int i = 0; i < size; i++) { + String expected = expectedLines[i]; + String actual = actualLines[i]; + if (!expected.equals(actual)) { + fail(String.format("Difference in line %s(%d):\n`%s`\n`%s`\n", name, i, expected, actual)); + } + } + if (expectedLines.length > actualLines.length) { + fail(String.format("Missing line %s(%d): %s\n", name, size, expectedLines[size])); + } + if (expectedLines.length < actualLines.length) { + fail(String.format("Extra line %s(%d): %s\n", name, size, actualLines[size])); + } + } + + private String readAfter(File file) throws IOException { + BufferedReader reader = new BufferedReader(new FileReader(new File(AFTER_FOLDER, file.getName()))); + StringBuilder result = new StringBuilder(); + String line; + while ((line = reader.readLine()) != null) { + result.append(line); + result.append(LINE_SEPARATOR); + } + reader.close(); + return result.toString(); + } +} -- cgit From f22021ca7808af2cd0ba03b7c34b8fd3758ff44b Mon Sep 17 00:00:00 2001 From: unknown Date: Thu, 26 Nov 2009 17:12:41 +0100 Subject: Minor refactoring + added testcase for for-loop --- .../lombok/delombok/CommentPreservingParser.java | 30 ++++++++++++++++------ .../lombok/delombok/PrettyCommentsPrinter.java | 10 +++----- test/delombok/resource/after/ForLoop.java | 13 ++++++++++ test/delombok/resource/after/WithComments.java | 2 +- test/delombok/resource/before/ForLoop.java | 12 +++++++++ .../src/lombok/delombok/TestSourceFiles.java | 11 +++----- 6 files changed, 54 insertions(+), 24 deletions(-) create mode 100644 test/delombok/resource/after/ForLoop.java create mode 100644 test/delombok/resource/before/ForLoop.java (limited to 'test') diff --git a/src/delombok/lombok/delombok/CommentPreservingParser.java b/src/delombok/lombok/delombok/CommentPreservingParser.java index c85124b5..19219d93 100644 --- a/src/delombok/lombok/delombok/CommentPreservingParser.java +++ b/src/delombok/lombok/delombok/CommentPreservingParser.java @@ -25,29 +25,43 @@ import java.io.IOException; import java.io.Writer; import com.sun.tools.javac.main.JavaCompiler; +import com.sun.tools.javac.main.OptionName; import com.sun.tools.javac.tree.JCTree.JCCompilationUnit; import com.sun.tools.javac.util.Context; import com.sun.tools.javac.util.List; +import com.sun.tools.javac.util.Options; public class CommentPreservingParser { - private final JavaCompiler compiler; - private final Context context; - public CommentPreservingParser(Context context) { - this.context = context; + private final String encoding; + + public CommentPreservingParser() { + this("utf-8"); + } + + public CommentPreservingParser(String encoding) { + this.encoding = encoding; + } + + public ParseResult parseFile(String fileName) throws IOException { + Context context = new Context(); + + Options.instance(context).put(OptionName.ENCODING, encoding); + CommentCollectingScanner.Factory.preRegister(context); - compiler = new JavaCompiler(context) { + + JavaCompiler compiler = new JavaCompiler(context) { @Override protected boolean keepComments() { return true; } }; compiler.genEndPos = true; - } - - public ParseResult parseFile(String fileName) throws IOException { + Comments comments = new Comments(); context.put(Comments.class, comments); + + comments.comments = List.nil(); @SuppressWarnings("deprecation") JCCompilationUnit cu = compiler.parse(fileName); return new ParseResult(comments.comments, cu); diff --git a/src/delombok/lombok/delombok/PrettyCommentsPrinter.java b/src/delombok/lombok/delombok/PrettyCommentsPrinter.java index c6b58a31..2fb62e53 100644 --- a/src/delombok/lombok/delombok/PrettyCommentsPrinter.java +++ b/src/delombok/lombok/delombok/PrettyCommentsPrinter.java @@ -184,10 +184,6 @@ public class PrettyCommentsPrinter extends JCTree.Visitor { */ Writer out; - /** Indentation width (can be reassigned from outside). - */ - public int width = 4; - /** The current left margin. */ int lmargin = 0; @@ -205,19 +201,19 @@ public class PrettyCommentsPrinter extends JCTree.Visitor { */ void align() throws IOException { newLine = false; - for (int i = 0; i < lmargin; i++) out.write(" "); + for (int i = 0; i < lmargin; i++) out.write("\t"); } /** Increase left margin by indentation width. */ void indent() { - lmargin = lmargin + width; + lmargin++; } /** Decrease left margin by indentation width. */ void undent() { - lmargin = lmargin - width; + lmargin--; } /** Enter a new precedence level. Emit a `(' if new precedence level diff --git a/test/delombok/resource/after/ForLoop.java b/test/delombok/resource/after/ForLoop.java new file mode 100644 index 00000000..609549b7 --- /dev/null +++ b/test/delombok/resource/after/ForLoop.java @@ -0,0 +1,13 @@ + +public class ForLoop { + + public static void main(String[] args) { + // before loop + for (int i = 0; i < 10; i++) { + // start of block + System.out.println(i); + // end of block + } + // after loop + } +} diff --git a/test/delombok/resource/after/WithComments.java b/test/delombok/resource/after/WithComments.java index 0f9ddb9f..59cc97c4 100644 --- a/test/delombok/resource/after/WithComments.java +++ b/test/delombok/resource/after/WithComments.java @@ -1,5 +1,5 @@ // Cool Comments public class WithComments { - // Also inside the body + // Also inside the body } \ No newline at end of file diff --git a/test/delombok/resource/before/ForLoop.java b/test/delombok/resource/before/ForLoop.java new file mode 100644 index 00000000..2bed7f9b --- /dev/null +++ b/test/delombok/resource/before/ForLoop.java @@ -0,0 +1,12 @@ +public class ForLoop { + + public static void main(String[] args) { + // before loop + for (int i = 0; i < 10; i++) { + // start of block + System.out.println(i); + // end of block + } + // after loop + } +} diff --git a/test/delombok/src/lombok/delombok/TestSourceFiles.java b/test/delombok/src/lombok/delombok/TestSourceFiles.java index 90c9b543..374eeb8b 100644 --- a/test/delombok/src/lombok/delombok/TestSourceFiles.java +++ b/test/delombok/src/lombok/delombok/TestSourceFiles.java @@ -21,6 +21,8 @@ */ package lombok.delombok; +import static org.junit.Assert.fail; + import java.io.BufferedReader; import java.io.File; import java.io.FileReader; @@ -31,11 +33,6 @@ import lombok.delombok.CommentPreservingParser.ParseResult; import org.junit.BeforeClass; import org.junit.Test; -import static org.junit.Assert.*; - -import com.sun.tools.javac.main.OptionName; -import com.sun.tools.javac.util.Context; -import com.sun.tools.javac.util.Options; public class TestSourceFiles { @@ -48,9 +45,7 @@ public class TestSourceFiles { @BeforeClass public static void init() { - Context c = new Context(); - Options.instance(c).put(OptionName.ENCODING, "utf-8"); - parser = new CommentPreservingParser(c); + parser = new CommentPreservingParser(); } @Test -- cgit From e5d248c3ccf64211fd8a301f584bde82dd426601 Mon Sep 17 00:00:00 2001 From: Reinier Zwitserloot Date: Fri, 27 Nov 2009 00:38:58 +0100 Subject: delombok now calls lombok. wahey! --- src/core/lombok/javac/JavacAST.java | 5 +- src/core/lombok/javac/JavacTransformer.java | 98 ++++++++++++++++++++++ src/core/lombok/javac/apt/Processor.java | 67 +-------------- .../lombok/delombok/CommentPreservingParser.java | 32 ++++++- .../src/lombok/delombok/TestLombokFiles.java | 95 +++++++++++++++++++++ .../src/lombok/delombok/TestSourceFiles.java | 21 +++-- .../resource/after/CommentsInterspersed.java | 26 ++++++ .../resource/before/CommentsInterspersed.java | 14 ++++ 8 files changed, 285 insertions(+), 73 deletions(-) create mode 100644 src/core/lombok/javac/JavacTransformer.java create mode 100644 test/delombok/src/lombok/delombok/TestLombokFiles.java create mode 100644 test/lombok/resource/after/CommentsInterspersed.java create mode 100644 test/lombok/resource/before/CommentsInterspersed.java (limited to 'test') diff --git a/src/core/lombok/javac/JavacAST.java b/src/core/lombok/javac/JavacAST.java index f2c83fb8..77c63cfe 100644 --- a/src/core/lombok/javac/JavacAST.java +++ b/src/core/lombok/javac/JavacAST.java @@ -32,7 +32,6 @@ import javax.tools.JavaFileObject; import lombok.core.AST; -import com.sun.source.util.Trees; import com.sun.tools.javac.code.Symtab; import com.sun.tools.javac.tree.JCTree; import com.sun.tools.javac.tree.TreeMaker; @@ -66,13 +65,11 @@ public class JavacAST extends AST { /** * Creates a new JavacAST of the provided Compilation Unit. * - * @param trees The trees instance to use to inspect the compilation unit. Generate via: - * {@code Trees.getInstance(env)} * @param messager A Messager for warning and error reporting. * @param context A Context object for interfacing with the compiler. * @param top The compilation unit, which serves as the top level node in the tree to be built. */ - public JavacAST(Trees trees, Messager messager, Context context, JCCompilationUnit top) { + public JavacAST(Messager messager, Context context, JCCompilationUnit top) { super(top.sourcefile == null ? null : top.sourcefile.toString()); setTop(buildCompilationUnit(top)); this.context = context; diff --git a/src/core/lombok/javac/JavacTransformer.java b/src/core/lombok/javac/JavacTransformer.java new file mode 100644 index 00000000..d004414d --- /dev/null +++ b/src/core/lombok/javac/JavacTransformer.java @@ -0,0 +1,98 @@ +/* + * Copyright © 2009 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.javac; + +import java.util.ArrayList; +import java.util.List; + +import javax.annotation.processing.Messager; + +import com.sun.tools.javac.tree.JCTree.JCAnnotation; +import com.sun.tools.javac.tree.JCTree.JCClassDecl; +import com.sun.tools.javac.tree.JCTree.JCCompilationUnit; +import com.sun.tools.javac.tree.JCTree.JCMethodDecl; +import com.sun.tools.javac.tree.JCTree.JCVariableDecl; +import com.sun.tools.javac.util.Context; + +public class JavacTransformer { + private final HandlerLibrary handlers; + private final Messager messager; + + public JavacTransformer(Messager messager) { + this.messager = messager; + this.handlers = HandlerLibrary.load(messager); + } + + public void transform(Context context, Iterable compilationUnits) { + List asts = new ArrayList(); + + for (JCCompilationUnit unit : compilationUnits) asts.add(new JavacAST(messager, context, unit)); + + handlers.skipPrintAST(); + for (JavacAST ast : asts) { + ast.traverse(new AnnotationVisitor()); + handlers.callASTVisitors(ast); + } + + handlers.skipAllButPrintAST(); + for (JavacAST ast : asts) { + ast.traverse(new AnnotationVisitor()); + } + } + + private class AnnotationVisitor extends JavacASTAdapter { + @Override public void visitAnnotationOnType(JCClassDecl type, JavacNode annotationNode, JCAnnotation annotation) { + if (annotationNode.isHandled()) return; + JCCompilationUnit top = (JCCompilationUnit) annotationNode.top().get(); + boolean handled = handlers.handleAnnotation(top, annotationNode, annotation); + if (handled) annotationNode.setHandled(); + } + + @Override public void visitAnnotationOnField(JCVariableDecl field, JavacNode annotationNode, JCAnnotation annotation) { + if (annotationNode.isHandled()) return; + JCCompilationUnit top = (JCCompilationUnit) annotationNode.top().get(); + boolean handled = handlers.handleAnnotation(top, annotationNode, annotation); + if (handled) annotationNode.setHandled(); + } + + @Override public void visitAnnotationOnMethod(JCMethodDecl method, JavacNode annotationNode, JCAnnotation annotation) { + if (annotationNode.isHandled()) return; + JCCompilationUnit top = (JCCompilationUnit) annotationNode.top().get(); + boolean handled = handlers.handleAnnotation(top, annotationNode, annotation); + if (handled) annotationNode.setHandled(); + } + + @Override public void visitAnnotationOnMethodArgument(JCVariableDecl argument, JCMethodDecl method, JavacNode annotationNode, JCAnnotation annotation) { + if (annotationNode.isHandled()) return; + JCCompilationUnit top = (JCCompilationUnit) annotationNode.top().get(); + boolean handled = handlers.handleAnnotation(top, annotationNode, annotation); + if (handled) annotationNode.setHandled(); + } + + @Override public void visitAnnotationOnLocal(JCVariableDecl local, JavacNode annotationNode, JCAnnotation annotation) { + if (annotationNode.isHandled()) return; + JCCompilationUnit top = (JCCompilationUnit) annotationNode.top().get(); + boolean handled = handlers.handleAnnotation(top, annotationNode, annotation); + if (handled) annotationNode.setHandled(); + } + } +} diff --git a/src/core/lombok/javac/apt/Processor.java b/src/core/lombok/javac/apt/Processor.java index 9c851762..b779a680 100644 --- a/src/core/lombok/javac/apt/Processor.java +++ b/src/core/lombok/javac/apt/Processor.java @@ -21,9 +21,7 @@ */ package lombok.javac.apt; -import java.util.ArrayList; import java.util.IdentityHashMap; -import java.util.List; import java.util.Set; import javax.annotation.processing.AbstractProcessor; @@ -36,19 +34,12 @@ import javax.lang.model.element.Element; import javax.lang.model.element.TypeElement; import javax.tools.Diagnostic.Kind; -import lombok.javac.HandlerLibrary; -import lombok.javac.JavacAST; -import lombok.javac.JavacASTAdapter; -import lombok.javac.JavacNode; +import lombok.javac.JavacTransformer; import com.sun.source.util.TreePath; import com.sun.source.util.Trees; import com.sun.tools.javac.processing.JavacProcessingEnvironment; -import com.sun.tools.javac.tree.JCTree.JCAnnotation; -import com.sun.tools.javac.tree.JCTree.JCClassDecl; import com.sun.tools.javac.tree.JCTree.JCCompilationUnit; -import com.sun.tools.javac.tree.JCTree.JCMethodDecl; -import com.sun.tools.javac.tree.JCTree.JCVariableDecl; /** @@ -66,7 +57,7 @@ import com.sun.tools.javac.tree.JCTree.JCVariableDecl; public class Processor extends AbstractProcessor { private ProcessingEnvironment rawProcessingEnv; private JavacProcessingEnvironment processingEnv; - private HandlerLibrary handlers; + private JavacTransformer transformer; private Trees trees; private String errorToShow; @@ -86,7 +77,7 @@ public class Processor extends AbstractProcessor { this.errorToShow = null; } else { this.processingEnv = (JavacProcessingEnvironment) procEnv; - handlers = HandlerLibrary.load(procEnv.getMessager()); + transformer = new JavacTransformer(procEnv.getMessager()); trees = Trees.instance(procEnv); this.errorToShow = null; } @@ -111,61 +102,11 @@ public class Processor extends AbstractProcessor { if (unit != null) units.put(unit, null); } - List asts = new ArrayList(); + transformer.transform(processingEnv.getContext(), units.keySet()); - for (JCCompilationUnit unit : units.keySet()) asts.add( - new JavacAST(trees, processingEnv.getMessager(), processingEnv.getContext(), unit)); - - handlers.skipPrintAST(); - for (JavacAST ast : asts) { - ast.traverse(new AnnotationVisitor()); - handlers.callASTVisitors(ast); - } - - handlers.skipAllButPrintAST(); - for (JavacAST ast : asts) { - ast.traverse(new AnnotationVisitor()); - } return false; } - private class AnnotationVisitor extends JavacASTAdapter { - @Override public void visitAnnotationOnType(JCClassDecl type, JavacNode annotationNode, JCAnnotation annotation) { - if (annotationNode.isHandled()) return; - JCCompilationUnit top = (JCCompilationUnit) annotationNode.top().get(); - boolean handled = handlers.handleAnnotation(top, annotationNode, annotation); - if (handled) annotationNode.setHandled(); - } - - @Override public void visitAnnotationOnField(JCVariableDecl field, JavacNode annotationNode, JCAnnotation annotation) { - if (annotationNode.isHandled()) return; - JCCompilationUnit top = (JCCompilationUnit) annotationNode.top().get(); - boolean handled = handlers.handleAnnotation(top, annotationNode, annotation); - if (handled) annotationNode.setHandled(); - } - - @Override public void visitAnnotationOnMethod(JCMethodDecl method, JavacNode annotationNode, JCAnnotation annotation) { - if (annotationNode.isHandled()) return; - JCCompilationUnit top = (JCCompilationUnit) annotationNode.top().get(); - boolean handled = handlers.handleAnnotation(top, annotationNode, annotation); - if (handled) annotationNode.setHandled(); - } - - @Override public void visitAnnotationOnMethodArgument(JCVariableDecl argument, JCMethodDecl method, JavacNode annotationNode, JCAnnotation annotation) { - if (annotationNode.isHandled()) return; - JCCompilationUnit top = (JCCompilationUnit) annotationNode.top().get(); - boolean handled = handlers.handleAnnotation(top, annotationNode, annotation); - if (handled) annotationNode.setHandled(); - } - - @Override public void visitAnnotationOnLocal(JCVariableDecl local, JavacNode annotationNode, JCAnnotation annotation) { - if (annotationNode.isHandled()) return; - JCCompilationUnit top = (JCCompilationUnit) annotationNode.top().get(); - boolean handled = handlers.handleAnnotation(top, annotationNode, annotation); - if (handled) annotationNode.setHandled(); - } - } - private JCCompilationUnit toUnit(Element element) { TreePath path = trees == null ? null : trees.getPath(element); if (path == null) return null; diff --git a/src/delombok/lombok/delombok/CommentPreservingParser.java b/src/delombok/lombok/delombok/CommentPreservingParser.java index 19219d93..51df5ed1 100644 --- a/src/delombok/lombok/delombok/CommentPreservingParser.java +++ b/src/delombok/lombok/delombok/CommentPreservingParser.java @@ -23,6 +23,15 @@ package lombok.delombok; import java.io.IOException; import java.io.Writer; +import java.util.Collections; + +import javax.annotation.processing.Messager; +import javax.lang.model.element.AnnotationMirror; +import javax.lang.model.element.AnnotationValue; +import javax.lang.model.element.Element; +import javax.tools.Diagnostic.Kind; + +import lombok.javac.JavacTransformer; import com.sun.tools.javac.main.JavaCompiler; import com.sun.tools.javac.main.OptionName; @@ -64,9 +73,30 @@ public class CommentPreservingParser { comments.comments = List.nil(); @SuppressWarnings("deprecation") JCCompilationUnit cu = compiler.parse(fileName); + + new JavacTransformer(messager).transform(context, Collections.singleton(cu)); + return new ParseResult(comments.comments, cu); } - + + private static final Messager messager = new Messager() { + @Override public void printMessage(Kind kind, CharSequence msg) { + System.out.printf("M: %s: %s\n", kind, msg); + } + + @Override public void printMessage(Kind kind, CharSequence msg, Element e) { + System.out.printf("E: %s: %s\n", kind, msg); + } + + @Override public void printMessage(Kind kind, CharSequence msg, Element e, AnnotationMirror a) { + System.out.printf("A: %s: %s\n", kind, msg); + } + + @Override public void printMessage(Kind kind, CharSequence msg, Element e, AnnotationMirror a, AnnotationValue v) { + System.out.printf("V: %s: %s\n", kind, msg); + } + }; + static class Comments { List comments = List.nil(); diff --git a/test/delombok/src/lombok/delombok/TestLombokFiles.java b/test/delombok/src/lombok/delombok/TestLombokFiles.java new file mode 100644 index 00000000..10782830 --- /dev/null +++ b/test/delombok/src/lombok/delombok/TestLombokFiles.java @@ -0,0 +1,95 @@ +/* + * Copyright © 2009 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; + +import static org.junit.Assert.fail; +import static lombok.delombok.TestSourceFiles.removeBlanks; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.io.StringWriter; + +import lombok.delombok.CommentPreservingParser.ParseResult; + +import org.junit.BeforeClass; +import org.junit.Test; + +public class TestLombokFiles { + private static CommentPreservingParser parser; + + private static final File BEFORE_FOLDER = new File("test/lombok/resource/before"); + private static final File AFTER_FOLDER = new File("test/lombok/resource/after"); + + private static final String LINE_SEPARATOR = System.getProperty("line.separator"); + + @BeforeClass + public static void init() { + parser = new CommentPreservingParser(); + } + + @Test + public void testSources() throws Exception { + File[] listFiles = BEFORE_FOLDER.listFiles(); + for (File file : listFiles) { + ParseResult parseResult = parser.parseFile(file.toString()); + StringWriter writer = new StringWriter(); + parseResult.print(writer); + System.out.println(writer); + compare(file.getName(), readAfter(file), writer.toString()); + } + } + + private void compare(String name, String expectedFile, String actualFile) { + String[] expectedLines = expectedFile.split("(\\r?\\n)"); + String[] actualLines = actualFile.split("(\\r?\\n)"); + expectedLines = removeBlanks(expectedLines); + actualLines = removeBlanks(actualLines); + int size = Math.min(expectedLines.length, actualLines.length); + for (int i = 0; i < size; i++) { + String expected = expectedLines[i]; + String actual = actualLines[i]; + if (!expected.equals(actual)) { + fail(String.format("Difference in line %s(%d):\n`%s`\n`%s`\n", name, i, expected, actual)); + } + } + if (expectedLines.length > actualLines.length) { + fail(String.format("Missing line %s(%d): %s\n", name, size, expectedLines[size])); + } + if (expectedLines.length < actualLines.length) { + fail(String.format("Extra line %s(%d): %s\n", name, size, actualLines[size])); + } + } + + private String readAfter(File file) throws IOException { + BufferedReader reader = new BufferedReader(new FileReader(new File(AFTER_FOLDER, file.getName()))); + StringBuilder result = new StringBuilder(); + String line; + while ((line = reader.readLine()) != null) { + result.append(line); + result.append(LINE_SEPARATOR); + } + reader.close(); + return result.toString(); + } +} diff --git a/test/delombok/src/lombok/delombok/TestSourceFiles.java b/test/delombok/src/lombok/delombok/TestSourceFiles.java index 374eeb8b..b662651d 100644 --- a/test/delombok/src/lombok/delombok/TestSourceFiles.java +++ b/test/delombok/src/lombok/delombok/TestSourceFiles.java @@ -28,6 +28,8 @@ import java.io.File; import java.io.FileReader; import java.io.IOException; import java.io.StringWriter; +import java.util.ArrayList; +import java.util.List; import lombok.delombok.CommentPreservingParser.ParseResult; @@ -35,14 +37,13 @@ import org.junit.BeforeClass; import org.junit.Test; public class TestSourceFiles { - private static CommentPreservingParser parser; private static final File BEFORE_FOLDER = new File("test/delombok/resource/before"); private static final File AFTER_FOLDER = new File("test/delombok/resource/after"); - - private static final String LINE_SEPARATOR = System.getProperty("line.separator"); - + + private static final String LINE_SEPARATOR = System.getProperty("line.separator"); + @BeforeClass public static void init() { parser = new CommentPreservingParser(); @@ -59,9 +60,19 @@ public class TestSourceFiles { } } + static String[] removeBlanks(String[] in) { + List out = new ArrayList(); + for (String s : in) { + if (!s.trim().isEmpty()) out.add(s); + } + return out.toArray(new String[0]); + } + private void compare(String name, String expectedFile, String actualFile) { String[] expectedLines = expectedFile.split("(\\r?\\n)"); String[] actualLines = actualFile.split("(\\r?\\n)"); + expectedLines = removeBlanks(expectedLines); + actualLines = removeBlanks(actualLines); int size = Math.min(expectedLines.length, actualLines.length); for (int i = 0; i < size; i++) { String expected = expectedLines[i]; @@ -77,7 +88,7 @@ public class TestSourceFiles { fail(String.format("Extra line %s(%d): %s\n", name, size, actualLines[size])); } } - + private String readAfter(File file) throws IOException { BufferedReader reader = new BufferedReader(new FileReader(new File(AFTER_FOLDER, file.getName()))); StringBuilder result = new StringBuilder(); diff --git a/test/lombok/resource/after/CommentsInterspersed.java b/test/lombok/resource/after/CommentsInterspersed.java new file mode 100644 index 00000000..ec5374c0 --- /dev/null +++ b/test/lombok/resource/after/CommentsInterspersed.java @@ -0,0 +1,26 @@ +import lombok.Getter; + +/*bla */ +public class CommentsInterspersed { + /** javadoc for field */ + private int x; + + /* bla2 */ + @Getter() + private String test = "foo"; + //$NON-NLS-1$ + + /** Javadoc on method */ + public native void gwtTest(); + /*-{ + javascript; + }-*/ + + public CommentsInterspersed() { + } + + public String getTest() { + return this.test; + } +} +//haha! diff --git a/test/lombok/resource/before/CommentsInterspersed.java b/test/lombok/resource/before/CommentsInterspersed.java new file mode 100644 index 00000000..2a32d309 --- /dev/null +++ b/test/lombok/resource/before/CommentsInterspersed.java @@ -0,0 +1,14 @@ +import lombok.Getter; + +public /*bla */ class CommentsInterspersed { + /** javadoc for field */ + private int x; + + private /* bla2 */ @Getter String test = "foo"; //$NON-NLS-1$ + + /** Javadoc on method */ + public native void gwtTest(); /*-{ + javascript; + }-*/ +} //haha! + -- cgit From c91e3701b9f5cf85a09f998d305895d4d1617206 Mon Sep 17 00:00:00 2001 From: Roel Spilker Date: Fri, 27 Nov 2009 02:44:46 +0100 Subject: Added another test --- test/delombok/resource/after/Cast.java | 7 +++++++ test/delombok/resource/before/Cast.java | 6 ++++++ 2 files changed, 13 insertions(+) create mode 100644 test/delombok/resource/after/Cast.java create mode 100644 test/delombok/resource/before/Cast.java (limited to 'test') diff --git a/test/delombok/resource/after/Cast.java b/test/delombok/resource/after/Cast.java new file mode 100644 index 00000000..a1b455f0 --- /dev/null +++ b/test/delombok/resource/after/Cast.java @@ -0,0 +1,7 @@ +import java.util.*; +public class Cast { + public void test(List list) { + RandomAccess r = (/*before*/ + RandomAccess /*after*/)list; + } +} \ No newline at end of file diff --git a/test/delombok/resource/before/Cast.java b/test/delombok/resource/before/Cast.java new file mode 100644 index 00000000..95237b0f --- /dev/null +++ b/test/delombok/resource/before/Cast.java @@ -0,0 +1,6 @@ +import java.util.*; +public class Cast { + public void test(List list) { + RandomAccess r = (/*before*/ RandomAccess /*after*/)list; + } +} \ No newline at end of file -- cgit From 391db3dcecdd0d94eb76b656e655346891b02bb4 Mon Sep 17 00:00:00 2001 From: Roel Spilker Date: Fri, 27 Nov 2009 04:18:28 +0100 Subject: Thorough work on inserting comments in the proper place for delombok; should now work fine with GWT native methods! NON-NLS-1 is still theoretically problematic, but that'll be a fix for another day. Also added ability to recognize that nothing has changed, which will copy the original file instead of reparsing it. --- .classpath | 2 +- src/core/lombok/javac/JavacTransformer.java | 7 ++- src/core/lombok/javac/handlers/HandleCleanup.java | 2 + src/core/lombok/javac/handlers/HandleData.java | 1 + .../javac/handlers/HandleEqualsAndHashCode.java | 1 + src/core/lombok/javac/handlers/HandleGetter.java | 2 + src/core/lombok/javac/handlers/HandleSetter.java | 2 + .../lombok/javac/handlers/HandleSneakyThrows.java | 2 + .../lombok/javac/handlers/HandleSynchronized.java | 3 ++ src/core/lombok/javac/handlers/HandleToString.java | 2 + .../lombok/javac/handlers/JavacHandlerUtil.java | 51 ++++++++++++++++++++++ src/delombok/lombok/delombok/Comment.java | 12 ++++- .../lombok/delombok/CommentCollectingScanner.java | 16 ++++++- .../lombok/delombok/CommentPreservingParser.java | 29 ++++++++---- .../lombok/delombok/PrettyCommentsPrinter.java | 37 +++++++++++++--- .../src/lombok/delombok/TestLombokFiles.java | 6 ++- .../src/lombok/delombok/TestSourceFiles.java | 7 ++- .../resource/after/CommentsInterspersed.java | 20 +++------ .../resource/before/CommentsInterspersed.java | 2 +- 19 files changed, 169 insertions(+), 35 deletions(-) (limited to 'test') diff --git a/.classpath b/.classpath index 3390740d..0d702795 100644 --- a/.classpath +++ b/.classpath @@ -14,7 +14,7 @@ - + diff --git a/src/core/lombok/javac/JavacTransformer.java b/src/core/lombok/javac/JavacTransformer.java index d004414d..5f145460 100644 --- a/src/core/lombok/javac/JavacTransformer.java +++ b/src/core/lombok/javac/JavacTransformer.java @@ -42,7 +42,7 @@ public class JavacTransformer { this.handlers = HandlerLibrary.load(messager); } - public void transform(Context context, Iterable compilationUnits) { + public boolean transform(Context context, Iterable compilationUnits) { List asts = new ArrayList(); for (JCCompilationUnit unit : compilationUnits) asts.add(new JavacAST(messager, context, unit)); @@ -57,6 +57,11 @@ public class JavacTransformer { for (JavacAST ast : asts) { ast.traverse(new AnnotationVisitor()); } + + for (JavacAST ast : asts) { + if (ast.isChanged()) return true; + } + return false; } private class AnnotationVisitor extends JavacASTAdapter { diff --git a/src/core/lombok/javac/handlers/HandleCleanup.java b/src/core/lombok/javac/handlers/HandleCleanup.java index 88a8e1d7..2c89d9ad 100644 --- a/src/core/lombok/javac/handlers/HandleCleanup.java +++ b/src/core/lombok/javac/handlers/HandleCleanup.java @@ -21,6 +21,7 @@ */ package lombok.javac.handlers; +import static lombok.javac.handlers.JavacHandlerUtil.markAnnotationAsProcessed; import lombok.Cleanup; import lombok.core.AnnotationValues; import lombok.core.AST.Kind; @@ -53,6 +54,7 @@ import com.sun.tools.javac.util.Name; @ProviderFor(JavacAnnotationHandler.class) public class HandleCleanup implements JavacAnnotationHandler { @Override public boolean handle(AnnotationValues annotation, JCAnnotation ast, JavacNode annotationNode) { + markAnnotationAsProcessed(annotationNode, Cleanup.class); String cleanupName = annotation.getInstance().value(); if (cleanupName.length() == 0) { annotationNode.addError("cleanupName cannot be the empty string."); diff --git a/src/core/lombok/javac/handlers/HandleData.java b/src/core/lombok/javac/handlers/HandleData.java index eef7f78d..a0315d08 100644 --- a/src/core/lombok/javac/handlers/HandleData.java +++ b/src/core/lombok/javac/handlers/HandleData.java @@ -59,6 +59,7 @@ import com.sun.tools.javac.util.List; @ProviderFor(JavacAnnotationHandler.class) public class HandleData implements JavacAnnotationHandler { @Override public boolean handle(AnnotationValues annotation, JCAnnotation ast, JavacNode annotationNode) { + markAnnotationAsProcessed(annotationNode, Data.class); JavacNode typeNode = annotationNode.up(); JCClassDecl typeDecl = null; if (typeNode.get() instanceof JCClassDecl) typeDecl = (JCClassDecl)typeNode.get(); diff --git a/src/core/lombok/javac/handlers/HandleEqualsAndHashCode.java b/src/core/lombok/javac/handlers/HandleEqualsAndHashCode.java index 61a4ef63..f388336d 100644 --- a/src/core/lombok/javac/handlers/HandleEqualsAndHashCode.java +++ b/src/core/lombok/javac/handlers/HandleEqualsAndHashCode.java @@ -72,6 +72,7 @@ public class HandleEqualsAndHashCode implements JavacAnnotationHandler annotation, JCAnnotation ast, JavacNode annotationNode) { + markAnnotationAsProcessed(annotationNode, EqualsAndHashCode.class); EqualsAndHashCode ann = annotation.getInstance(); List excludes = List.from(ann.exclude()); List includes = List.from(ann.of()); diff --git a/src/core/lombok/javac/handlers/HandleGetter.java b/src/core/lombok/javac/handlers/HandleGetter.java index e60e426d..b0343eaf 100644 --- a/src/core/lombok/javac/handlers/HandleGetter.java +++ b/src/core/lombok/javac/handlers/HandleGetter.java @@ -80,8 +80,10 @@ public class HandleGetter implements JavacAnnotationHandler { } @Override public boolean handle(AnnotationValues annotation, JCAnnotation ast, JavacNode annotationNode) { + markAnnotationAsProcessed(annotationNode, Getter.class); JavacNode fieldNode = annotationNode.up(); AccessLevel level = annotation.getInstance().value(); + if (level == AccessLevel.NONE) return true; return createGetterForField(level, fieldNode, annotationNode, true); diff --git a/src/core/lombok/javac/handlers/HandleSetter.java b/src/core/lombok/javac/handlers/HandleSetter.java index 84032e9c..1d029f33 100644 --- a/src/core/lombok/javac/handlers/HandleSetter.java +++ b/src/core/lombok/javac/handlers/HandleSetter.java @@ -82,8 +82,10 @@ public class HandleSetter implements JavacAnnotationHandler { } @Override public boolean handle(AnnotationValues annotation, JCAnnotation ast, JavacNode annotationNode) { + markAnnotationAsProcessed(annotationNode, Setter.class); JavacNode fieldNode = annotationNode.up(); AccessLevel level = annotation.getInstance().value(); + if (level == AccessLevel.NONE) return true; return createSetterForField(level, fieldNode, annotationNode, true); diff --git a/src/core/lombok/javac/handlers/HandleSneakyThrows.java b/src/core/lombok/javac/handlers/HandleSneakyThrows.java index e7879dd1..8a185e87 100644 --- a/src/core/lombok/javac/handlers/HandleSneakyThrows.java +++ b/src/core/lombok/javac/handlers/HandleSneakyThrows.java @@ -22,6 +22,7 @@ package lombok.javac.handlers; import static lombok.javac.handlers.JavacHandlerUtil.chainDots; +import static lombok.javac.handlers.JavacHandlerUtil.markAnnotationAsProcessed; import java.util.ArrayList; import java.util.Collection; @@ -49,6 +50,7 @@ import com.sun.tools.javac.util.List; @ProviderFor(JavacAnnotationHandler.class) public class HandleSneakyThrows implements JavacAnnotationHandler { @Override public boolean handle(AnnotationValues annotation, JCAnnotation ast, JavacNode annotationNode) { + markAnnotationAsProcessed(annotationNode, SneakyThrows.class); Collection exceptionNames = annotation.getRawExpressions("value"); List memberValuePairs = ast.getArguments(); diff --git a/src/core/lombok/javac/handlers/HandleSynchronized.java b/src/core/lombok/javac/handlers/HandleSynchronized.java index c86d99c6..eaa0ad6d 100644 --- a/src/core/lombok/javac/handlers/HandleSynchronized.java +++ b/src/core/lombok/javac/handlers/HandleSynchronized.java @@ -51,10 +51,12 @@ public class HandleSynchronized implements JavacAnnotationHandler private static final String STATIC_LOCK_NAME = "$LOCK"; @Override public boolean handle(AnnotationValues annotation, JCAnnotation ast, JavacNode annotationNode) { + markAnnotationAsProcessed(annotationNode, Synchronized.class); JavacNode methodNode = annotationNode.up(); if (methodNode == null || methodNode.getKind() != Kind.METHOD || !(methodNode.get() instanceof JCMethodDecl)) { annotationNode.addError("@Synchronized is legal only on methods."); + return true; } @@ -62,6 +64,7 @@ public class HandleSynchronized implements JavacAnnotationHandler if ((method.mods.flags & Flags.ABSTRACT) != 0) { annotationNode.addError("@Synchronized is legal only on concrete methods."); + return true; } boolean isStatic = (method.mods.flags & Flags.STATIC) != 0; diff --git a/src/core/lombok/javac/handlers/HandleToString.java b/src/core/lombok/javac/handlers/HandleToString.java index f7251ab8..dd3df620 100644 --- a/src/core/lombok/javac/handlers/HandleToString.java +++ b/src/core/lombok/javac/handlers/HandleToString.java @@ -68,6 +68,8 @@ public class HandleToString implements JavacAnnotationHandler { } @Override public boolean handle(AnnotationValues annotation, JCAnnotation ast, JavacNode annotationNode) { + markAnnotationAsProcessed(annotationNode, ToString.class); + ToString ann = annotation.getInstance(); List excludes = List.from(ann.exclude()); List includes = List.from(ann.of()); diff --git a/src/core/lombok/javac/handlers/JavacHandlerUtil.java b/src/core/lombok/javac/handlers/JavacHandlerUtil.java index 34d8b849..caa8a998 100644 --- a/src/core/lombok/javac/handlers/JavacHandlerUtil.java +++ b/src/core/lombok/javac/handlers/JavacHandlerUtil.java @@ -21,6 +21,7 @@ */ package lombok.javac.handlers; +import java.lang.annotation.Annotation; import java.util.regex.Pattern; import lombok.AccessLevel; @@ -34,7 +35,9 @@ import com.sun.tools.javac.tree.JCTree; import com.sun.tools.javac.tree.TreeMaker; import com.sun.tools.javac.tree.JCTree.JCAnnotation; import com.sun.tools.javac.tree.JCTree.JCClassDecl; +import com.sun.tools.javac.tree.JCTree.JCCompilationUnit; import com.sun.tools.javac.tree.JCTree.JCExpression; +import com.sun.tools.javac.tree.JCTree.JCImport; import com.sun.tools.javac.tree.JCTree.JCMethodDecl; import com.sun.tools.javac.tree.JCTree.JCStatement; import com.sun.tools.javac.tree.JCTree.JCVariableDecl; @@ -57,6 +60,54 @@ public class JavacHandlerUtil { return TransformationsUtil.PRIMITIVE_TYPE_NAME_PATTERN.matcher(typeName).matches(); } + /** + * Removes the annotation from javac's AST, then removes it from lombok's AST, + * then removes any import statement that imports this exact annotation (not star imports). + */ + public static void markAnnotationAsProcessed(JavacNode annotation, Class annotationType) { + JavacNode parentNode = annotation.directUp(); + switch (parentNode.getKind()) { + case FIELD: + case ARGUMENT: + case LOCAL: + JCVariableDecl variable = (JCVariableDecl) parentNode.get(); + variable.mods.annotations = filterList(variable.mods.annotations, annotation.get()); + break; + case METHOD: + JCMethodDecl method = (JCMethodDecl) parentNode.get(); + method.mods.annotations = filterList(method.mods.annotations, annotation.get()); + break; + default: + throw new IllegalStateException("Don't know how to remove annotations from: " + parentNode.getKind()); + } + parentNode.removeChild(annotation); + + JCCompilationUnit unit = (JCCompilationUnit) annotation.top().get(); + deleteImportFromCompilationUnit(unit, annotationType.getName()); + } + + private static void deleteImportFromCompilationUnit(JCCompilationUnit unit, String name) { + List newDefs = List.nil(); + + for (JCTree def : unit.defs) { + boolean delete = false; + if (def instanceof JCImport) { + JCImport imp0rt = (JCImport)def; + delete = (!imp0rt.staticImport && imp0rt.qualid.toString().equals(name)); + } + if (!delete) newDefs = newDefs.append(def); + } + unit.defs = newDefs; + } + + private static List filterList(List annotations, JCTree jcTree) { + List newAnnotations = List.nil(); + for (JCAnnotation ann : annotations) { + if (jcTree != ann) newAnnotations = newAnnotations.append(ann); + } + return newAnnotations; + } + /** * Translates the given field into all possible getter names. * Convenient wrapper around {@link TransformationsUtil#toAllGetterNames(CharSequence, boolean)}. diff --git a/src/delombok/lombok/delombok/Comment.java b/src/delombok/lombok/delombok/Comment.java index c264733f..55a5c46d 100644 --- a/src/delombok/lombok/delombok/Comment.java +++ b/src/delombok/lombok/delombok/Comment.java @@ -23,11 +23,21 @@ package lombok.delombok; public final class Comment { final int pos; + final int prevEndPos; final String content; + final int endPos; + final boolean newLine; - public Comment(int pos, String content) { + public Comment(int prevEndPos, int pos, int endPos, String content, boolean newLine) { this.pos = pos; + this.prevEndPos = prevEndPos; + this.endPos = endPos; this.content = content; + this.newLine = newLine; + } + + public boolean isConnected() { + return !newLine && prevEndPos == pos; } public String summary() { diff --git a/src/delombok/lombok/delombok/CommentCollectingScanner.java b/src/delombok/lombok/delombok/CommentCollectingScanner.java index 6f593c7f..c38aef24 100644 --- a/src/delombok/lombok/delombok/CommentCollectingScanner.java +++ b/src/delombok/lombok/delombok/CommentCollectingScanner.java @@ -80,6 +80,20 @@ public class CommentCollectingScanner extends Scanner { @Override protected void processComment(CommentStyle style) { - comments.add(pos(), new String(getRawCharacters(pos(), endPos()))); + int prevEndPos = prevEndPos(); + int pos = pos(); + boolean newLine = containsNewLine(prevEndPos, pos); + String content = new String(getRawCharacters(pos, endPos())); + comments.add(prevEndPos, pos, endPos(), content, newLine); + } + + + private boolean containsNewLine(int from, int to) { + for (char c : getRawCharacters(from, to)) { + if (c == '\n' || c == '\r') { + return true; + } + } + return false; } } \ No newline at end of file diff --git a/src/delombok/lombok/delombok/CommentPreservingParser.java b/src/delombok/lombok/delombok/CommentPreservingParser.java index 51df5ed1..9fd8778c 100644 --- a/src/delombok/lombok/delombok/CommentPreservingParser.java +++ b/src/delombok/lombok/delombok/CommentPreservingParser.java @@ -24,11 +24,13 @@ package lombok.delombok; import java.io.IOException; import java.io.Writer; import java.util.Collections; +import java.util.Date; import javax.annotation.processing.Messager; import javax.lang.model.element.AnnotationMirror; import javax.lang.model.element.AnnotationValue; import javax.lang.model.element.Element; +import javax.tools.JavaFileObject; import javax.tools.Diagnostic.Kind; import lombok.javac.JavacTransformer; @@ -52,7 +54,7 @@ public class CommentPreservingParser { this.encoding = encoding; } - public ParseResult parseFile(String fileName) throws IOException { + public ParseResult parse(String fileName, boolean forceProcessing) throws IOException { Context context = new Context(); Options.instance(context).put(OptionName.ENCODING, encoding); @@ -74,9 +76,8 @@ public class CommentPreservingParser { @SuppressWarnings("deprecation") JCCompilationUnit cu = compiler.parse(fileName); - new JavacTransformer(messager).transform(context, Collections.singleton(cu)); - - return new ParseResult(comments.comments, cu); + boolean changed = new JavacTransformer(messager).transform(context, Collections.singleton(cu)); + return new ParseResult(comments.comments, cu, forceProcessing || changed); } private static final Messager messager = new Messager() { @@ -100,21 +101,33 @@ public class CommentPreservingParser { static class Comments { List comments = List.nil(); - void add(int pos, String content) { - comments = comments.append(new Comment(pos, content)); + void add(int prevEndPos, int pos, int endPos, String content, boolean newLine) { + comments = comments.append(new Comment(prevEndPos, pos, endPos, content, newLine)); } } public static class ParseResult { private final List comments; private final JCCompilationUnit compilationUnit; + private final boolean changed; - private ParseResult(List comments, JCCompilationUnit compilationUnit) { + private ParseResult(List comments, JCCompilationUnit compilationUnit, boolean changed) { this.comments = comments; this.compilationUnit = compilationUnit; + this.changed = changed; } - public void print(Writer out) { + public void print(Writer out) throws IOException { + if (!changed) { + JavaFileObject sourceFile = compilationUnit.getSourceFile(); + if (sourceFile != null) { + out.write(sourceFile.getCharContent(true).toString()); + System.out.println(out.toString()); + return; + } + } + + out.write("// Generated by delombok at " + new Date() + "\n"); compilationUnit.accept(new PrettyCommentsPrinter(out, compilationUnit, comments)); } } diff --git a/src/delombok/lombok/delombok/PrettyCommentsPrinter.java b/src/delombok/lombok/delombok/PrettyCommentsPrinter.java index 2fb62e53..611dd417 100644 --- a/src/delombok/lombok/delombok/PrettyCommentsPrinter.java +++ b/src/delombok/lombok/delombok/PrettyCommentsPrinter.java @@ -179,7 +179,32 @@ public class PrettyCommentsPrinter extends JCTree.Visitor { align(); } } - + + private void consumeTrailingComments(int from) throws IOException { + boolean shouldIndent = !newLine; + Comment head = comments.head; + while (comments.nonEmpty() && head.prevEndPos == from) { + from = head.endPos; + if (!head.isConnected()) { + if (head.newLine) { + if (!newLine) { + println(); + align(); + } + } + else { + print(" "); + } + } + print(head.content); + comments = comments.tail; + head = comments.head; + } + if (newLine && shouldIndent) { + align(); + } + } + /** The output stream on which trees are printed. */ Writer out; @@ -277,15 +302,17 @@ public class PrettyCommentsPrinter extends JCTree.Visitor { if (tree == null) print("/*missing*/"); else { consumeComments(tree.pos); - tree.accept(this); - consumeComments(endPos(tree)); + tree.accept(this); + int endPos = endPos(tree); + consumeTrailingComments(endPos); + consumeComments(endPos); } } catch (UncheckedIOException ex) { IOException e = new IOException(ex.getMessage()); e.initCause(ex); throw e; } finally { - this.prec = prevPrec; + this.prec = prevPrec; } } @@ -301,7 +328,7 @@ public class PrettyCommentsPrinter extends JCTree.Visitor { public void printStat(JCTree tree) throws IOException { printExpr(tree, TreeInfo.notExpression); } - + /** Derived visitor method: print list of expression trees, separated by given string. * @param sep the separator string */ diff --git a/test/delombok/src/lombok/delombok/TestLombokFiles.java b/test/delombok/src/lombok/delombok/TestLombokFiles.java index 10782830..f04a0a34 100644 --- a/test/delombok/src/lombok/delombok/TestLombokFiles.java +++ b/test/delombok/src/lombok/delombok/TestLombokFiles.java @@ -52,10 +52,9 @@ public class TestLombokFiles { public void testSources() throws Exception { File[] listFiles = BEFORE_FOLDER.listFiles(); for (File file : listFiles) { - ParseResult parseResult = parser.parseFile(file.toString()); + ParseResult parseResult = parser.parse(file.toString(), true); StringWriter writer = new StringWriter(); parseResult.print(writer); - System.out.println(writer); compare(file.getName(), readAfter(file), writer.toString()); } } @@ -63,6 +62,9 @@ public class TestLombokFiles { private void compare(String name, String expectedFile, String actualFile) { String[] expectedLines = expectedFile.split("(\\r?\\n)"); String[] actualLines = actualFile.split("(\\r?\\n)"); + if (actualLines[0].startsWith("// Generated by delombok at ")) { + actualLines[0] = ""; + } expectedLines = removeBlanks(expectedLines); actualLines = removeBlanks(actualLines); int size = Math.min(expectedLines.length, actualLines.length); diff --git a/test/delombok/src/lombok/delombok/TestSourceFiles.java b/test/delombok/src/lombok/delombok/TestSourceFiles.java index b662651d..91ace773 100644 --- a/test/delombok/src/lombok/delombok/TestSourceFiles.java +++ b/test/delombok/src/lombok/delombok/TestSourceFiles.java @@ -53,7 +53,7 @@ public class TestSourceFiles { public void testSources() throws Exception { File[] listFiles = BEFORE_FOLDER.listFiles(); for (File file : listFiles) { - ParseResult parseResult = parser.parseFile(file.toString()); + ParseResult parseResult = parser.parse(file.toString(), true); StringWriter writer = new StringWriter(); parseResult.print(writer); compare(file.getName(), readAfter(file), writer.toString()); @@ -71,6 +71,9 @@ public class TestSourceFiles { private void compare(String name, String expectedFile, String actualFile) { String[] expectedLines = expectedFile.split("(\\r?\\n)"); String[] actualLines = actualFile.split("(\\r?\\n)"); + if (actualLines[0].startsWith("// Generated by delombok at ")) { + actualLines[0] = ""; + } expectedLines = removeBlanks(expectedLines); actualLines = removeBlanks(actualLines); int size = Math.min(expectedLines.length, actualLines.length); @@ -78,7 +81,7 @@ public class TestSourceFiles { String expected = expectedLines[i]; String actual = actualLines[i]; if (!expected.equals(actual)) { - fail(String.format("Difference in line %s(%d):\n`%s`\n`%s`\n", name, i, expected, actual)); + fail(String.format("Difference in line %s(%d):\nExpected `%s`\nGot `%s`\n", name, i, expected, actual)); } } if (expectedLines.length > actualLines.length) { diff --git a/test/lombok/resource/after/CommentsInterspersed.java b/test/lombok/resource/after/CommentsInterspersed.java index ec5374c0..f3841606 100644 --- a/test/lombok/resource/after/CommentsInterspersed.java +++ b/test/lombok/resource/after/CommentsInterspersed.java @@ -1,26 +1,20 @@ -import lombok.Getter; - +/* cmt */ +/* cmt2 */ +/* cmt3 */ /*bla */ public class CommentsInterspersed { /** javadoc for field */ private int x; /* bla2 */ - @Getter() - private String test = "foo"; - //$NON-NLS-1$ + private String test = "foo"; //$NON-NLS-1$ /** Javadoc on method */ - public native void gwtTest(); - /*-{ + public native void gwtTest(); /*-{ javascript; }-*/ - public CommentsInterspersed() { - } - public String getTest() { - return this.test; + return test; } -} -//haha! +} //haha! diff --git a/test/lombok/resource/before/CommentsInterspersed.java b/test/lombok/resource/before/CommentsInterspersed.java index 2a32d309..455c680d 100644 --- a/test/lombok/resource/before/CommentsInterspersed.java +++ b/test/lombok/resource/before/CommentsInterspersed.java @@ -1,4 +1,4 @@ -import lombok.Getter; +import /* cmt */ lombok./* cmt2 */Getter /* cmt3 */ ; public /*bla */ class CommentsInterspersed { /** javadoc for field */ -- cgit From 4316e77e1eb6654eec18d04780a6da78c22fb644 Mon Sep 17 00:00:00 2001 From: Reinier Zwitserloot Date: Fri, 27 Nov 2009 07:18:48 +0100 Subject: Added a class that is easier to use than the parser itself for delombok, as well as code to process entire directories at a time. Also removed duplication from the testcases. --- .classpath | 1 + src/delombok/lombok/delombok/Delombok.java | 155 +++++++++++++++++++++ test/core/src/lombok/TestViaDelombok.java | 97 +++++++++++++ .../src/lombok/delombok/TestLombokFiles.java | 67 +-------- .../src/lombok/delombok/TestSourceFiles.java | 76 +--------- 5 files changed, 261 insertions(+), 135 deletions(-) create mode 100644 src/delombok/lombok/delombok/Delombok.java create mode 100644 test/core/src/lombok/TestViaDelombok.java (limited to 'test') diff --git a/.classpath b/.classpath index 0d702795..a33a68d9 100644 --- a/.classpath +++ b/.classpath @@ -6,6 +6,7 @@ + diff --git a/src/delombok/lombok/delombok/Delombok.java b/src/delombok/lombok/delombok/Delombok.java new file mode 100644 index 00000000..c6f9ecf9 --- /dev/null +++ b/src/delombok/lombok/delombok/Delombok.java @@ -0,0 +1,155 @@ +package lombok.delombok; + +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.OutputStreamWriter; +import java.io.PrintStream; +import java.io.Writer; +import java.nio.charset.Charset; +import java.nio.charset.UnsupportedCharsetException; + +import lombok.delombok.CommentPreservingParser.ParseResult; + +public class Delombok { + private Charset charset = Charset.defaultCharset(); + private CommentPreservingParser parser = new CommentPreservingParser(); + private PrintStream feedback = System.err; + private boolean verbose; + private boolean force = false; + + /** If null, output to standard out. */ + private File output = null; + + public void setCharset(String charsetName) throws UnsupportedCharsetException { + charset = Charset.forName(charsetName); + } + + public void setForceProcess(boolean force) { + this.force = force; + } + + public void setFeedback(PrintStream feedback) { + this.feedback = feedback; + } + + public void setVerbose(boolean verbose) { + this.verbose = verbose; + } + + public void setOutput(File dir) { + if (dir.isFile()) throw new IllegalArgumentException( + "DELOMBOK: delombok will only write to a directory. " + + "If you want to delombok a single file, use -p to output to standard output, then redirect this to a file:\n" + + "delombok MyJavaFile.java -p >MyJavaFileDelombok.java"); + output = dir; + } + + public void setOutputToStandardOut() { + this.output = null; + } + + public void delombok(File base) throws IOException { + delombok0(base, "", 0); + } + + private void delombok0(File base, String suffix, int loop) throws IOException { + File dir = suffix.isEmpty() ? base : new File(base, suffix); + String name = suffix + File.separator + dir.getName(); + + if (dir.isDirectory()) { + if (loop >= 100) { + feedback.printf("Over 100 subdirectories? I'm guessing there's a loop in your directory structure. Skipping: %s\n", suffix); + } else { + dir.mkdir(); + delombok0(base, name, loop + 1); + } + } else if (dir.isFile()) { + String extension = getExtension(dir); + if (extension.equals(".java")) delombok(base, name); + else if (extension.equals(".class")) skipClass(name); + else copy(base, name); + } else { + feedback.printf("Skipping %s because it is a special file type.\n", canonical(dir)); + } + } + + private void skipClass(String fileName) { + if (verbose) feedback.printf("Skipping class file: %s\n", fileName); + } + + private void copy(File base, String fileName) throws IOException { + if (output == null) { + feedback.printf("Skipping resource file: %s\n", fileName); + return; + } + if (verbose) feedback.printf("Copying resource file: %s\n", fileName); + byte[] b = new byte[65536]; + FileInputStream in = new FileInputStream(new File(base, fileName)); + try { + FileOutputStream out = new FileOutputStream(new File(output, fileName)); + try { + while (true) { + int r = in.read(b); + if (r == -1) break; + out.write(b, 0, r); + } + } finally { + out.close(); + } + } finally { + in.close(); + } + } + + public void delombok(String file, Writer writer) throws IOException { + ParseResult result = parser.parse(file, force); + + result.print(writer); + } + + public void delombok(File base, String fileName) throws IOException { + if (output != null && canonical(base).equals(canonical(output))) throw new IOException( + "DELOMBOK: Output file and input file refer to the same filesystem location. Specify a separate path for output."); + + ParseResult result = parser.parse(new File(base, fileName).getAbsolutePath(), force); + + if (verbose) feedback.printf("File: %s [%s]\n", fileName, result.isChanged() ? "delombok-ed" : "unchanged"); + + Writer rawWriter = output == null ? createStandardOutWriter() : createFileWriter(output, fileName); + BufferedWriter writer = new BufferedWriter(rawWriter); + + try { + result.print(writer); + } finally { + writer.close(); + } + } + + private static String canonical(File dir) { + try { + return dir.getCanonicalPath(); + } catch (Exception e) { + return dir.getAbsolutePath(); + } + } + + private static String getExtension(File dir) { + String name = dir.getName(); + int idx = name.lastIndexOf('.'); + return idx == -1 ? "" : name.substring(idx+1); + } + + private Writer createFileWriter(File base, String fileName) throws IOException { + File outFile = new File(base, fileName); + outFile.getParentFile().mkdirs(); + FileOutputStream out = new FileOutputStream(outFile); + return new OutputStreamWriter(out, charset); + } + + private Writer createStandardOutWriter() { + return new OutputStreamWriter(System.out, charset); + } +} diff --git a/test/core/src/lombok/TestViaDelombok.java b/test/core/src/lombok/TestViaDelombok.java new file mode 100644 index 00000000..e8070723 --- /dev/null +++ b/test/core/src/lombok/TestViaDelombok.java @@ -0,0 +1,97 @@ +/* + * Copyright © 2009 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; + +import static org.junit.Assert.fail; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.io.StringWriter; +import java.util.ArrayList; +import java.util.List; + +import lombok.delombok.Delombok; + +public class TestViaDelombok { + private static Delombok delombok = new Delombok(); + + private static final String LINE_SEPARATOR = System.getProperty("line.separator"); + + public static void runComparison(File beforeDir, File afterDir) throws IOException { + File[] listFiles = beforeDir.listFiles(); + + for (File file : listFiles) { + delombok.setVerbose(false); + delombok.setForceProcess(true); + delombok.setCharset("UTF-8"); + StringWriter writer = new StringWriter(); + delombok.delombok(file.getAbsolutePath(), writer); + compare(file.getName(), readAfter(afterDir, file), writer.toString()); + } + } + + private static void compare(String name, String expectedFile, String actualFile) { + String[] expectedLines = expectedFile.split("(\\r?\\n)"); + String[] actualLines = actualFile.split("(\\r?\\n)"); + if (actualLines[0].startsWith("// Generated by delombok at ")) { + actualLines[0] = ""; + } + expectedLines = removeBlanks(expectedLines); + actualLines = removeBlanks(actualLines); + int size = Math.min(expectedLines.length, actualLines.length); + for (int i = 0; i < size; i++) { + String expected = expectedLines[i]; + String actual = actualLines[i]; + if (!expected.equals(actual)) { + fail(String.format("Difference in line %s(%d):\n`%s`\n`%s`\n", name, i, expected, actual)); + } + } + if (expectedLines.length > actualLines.length) { + fail(String.format("Missing line %s(%d): %s\n", name, size, expectedLines[size])); + } + if (expectedLines.length < actualLines.length) { + fail(String.format("Extra line %s(%d): %s\n", name, size, actualLines[size])); + } + } + + private static String readAfter(File afterDir, File file) throws IOException { + BufferedReader reader = new BufferedReader(new FileReader(new File(afterDir, file.getName()))); + StringBuilder result = new StringBuilder(); + String line; + while ((line = reader.readLine()) != null) { + result.append(line); + result.append(LINE_SEPARATOR); + } + reader.close(); + return result.toString(); + } + + private static String[] removeBlanks(String[] in) { + List out = new ArrayList(); + for (String s : in) { + if (!s.trim().isEmpty()) out.add(s); + } + return out.toArray(new String[0]); + } +} diff --git a/test/delombok/src/lombok/delombok/TestLombokFiles.java b/test/delombok/src/lombok/delombok/TestLombokFiles.java index f04a0a34..1d93a4b2 100644 --- a/test/delombok/src/lombok/delombok/TestLombokFiles.java +++ b/test/delombok/src/lombok/delombok/TestLombokFiles.java @@ -21,77 +21,18 @@ */ package lombok.delombok; -import static org.junit.Assert.fail; -import static lombok.delombok.TestSourceFiles.removeBlanks; - -import java.io.BufferedReader; import java.io.File; -import java.io.FileReader; -import java.io.IOException; -import java.io.StringWriter; -import lombok.delombok.CommentPreservingParser.ParseResult; +import lombok.TestViaDelombok; -import org.junit.BeforeClass; import org.junit.Test; public class TestLombokFiles { - private static CommentPreservingParser parser; - - private static final File BEFORE_FOLDER = new File("test/lombok/resource/before"); - private static final File AFTER_FOLDER = new File("test/lombok/resource/after"); - - private static final String LINE_SEPARATOR = System.getProperty("line.separator"); - - @BeforeClass - public static void init() { - parser = new CommentPreservingParser(); - } + private static final File BEFORE_DIR = new File("test/lombok/resource/before"); + private static final File AFTER_DIR = new File("test/lombok/resource/after"); @Test public void testSources() throws Exception { - File[] listFiles = BEFORE_FOLDER.listFiles(); - for (File file : listFiles) { - ParseResult parseResult = parser.parse(file.toString(), true); - StringWriter writer = new StringWriter(); - parseResult.print(writer); - compare(file.getName(), readAfter(file), writer.toString()); - } - } - - private void compare(String name, String expectedFile, String actualFile) { - String[] expectedLines = expectedFile.split("(\\r?\\n)"); - String[] actualLines = actualFile.split("(\\r?\\n)"); - if (actualLines[0].startsWith("// Generated by delombok at ")) { - actualLines[0] = ""; - } - expectedLines = removeBlanks(expectedLines); - actualLines = removeBlanks(actualLines); - int size = Math.min(expectedLines.length, actualLines.length); - for (int i = 0; i < size; i++) { - String expected = expectedLines[i]; - String actual = actualLines[i]; - if (!expected.equals(actual)) { - fail(String.format("Difference in line %s(%d):\n`%s`\n`%s`\n", name, i, expected, actual)); - } - } - if (expectedLines.length > actualLines.length) { - fail(String.format("Missing line %s(%d): %s\n", name, size, expectedLines[size])); - } - if (expectedLines.length < actualLines.length) { - fail(String.format("Extra line %s(%d): %s\n", name, size, actualLines[size])); - } - } - - private String readAfter(File file) throws IOException { - BufferedReader reader = new BufferedReader(new FileReader(new File(AFTER_FOLDER, file.getName()))); - StringBuilder result = new StringBuilder(); - String line; - while ((line = reader.readLine()) != null) { - result.append(line); - result.append(LINE_SEPARATOR); - } - reader.close(); - return result.toString(); + TestViaDelombok.runComparison(BEFORE_DIR, AFTER_DIR); } } diff --git a/test/delombok/src/lombok/delombok/TestSourceFiles.java b/test/delombok/src/lombok/delombok/TestSourceFiles.java index 91ace773..a5df5197 100644 --- a/test/delombok/src/lombok/delombok/TestSourceFiles.java +++ b/test/delombok/src/lombok/delombok/TestSourceFiles.java @@ -21,86 +21,18 @@ */ package lombok.delombok; -import static org.junit.Assert.fail; - -import java.io.BufferedReader; import java.io.File; -import java.io.FileReader; -import java.io.IOException; -import java.io.StringWriter; -import java.util.ArrayList; -import java.util.List; -import lombok.delombok.CommentPreservingParser.ParseResult; +import lombok.TestViaDelombok; -import org.junit.BeforeClass; import org.junit.Test; public class TestSourceFiles { - private static CommentPreservingParser parser; - - private static final File BEFORE_FOLDER = new File("test/delombok/resource/before"); - private static final File AFTER_FOLDER = new File("test/delombok/resource/after"); - - private static final String LINE_SEPARATOR = System.getProperty("line.separator"); - - @BeforeClass - public static void init() { - parser = new CommentPreservingParser(); - } + private static final File BEFORE_DIR = new File("test/delombok/resource/before"); + private static final File AFTER_DIR = new File("test/delombok/resource/after"); @Test public void testSources() throws Exception { - File[] listFiles = BEFORE_FOLDER.listFiles(); - for (File file : listFiles) { - ParseResult parseResult = parser.parse(file.toString(), true); - StringWriter writer = new StringWriter(); - parseResult.print(writer); - compare(file.getName(), readAfter(file), writer.toString()); - } - } - - static String[] removeBlanks(String[] in) { - List out = new ArrayList(); - for (String s : in) { - if (!s.trim().isEmpty()) out.add(s); - } - return out.toArray(new String[0]); - } - - private void compare(String name, String expectedFile, String actualFile) { - String[] expectedLines = expectedFile.split("(\\r?\\n)"); - String[] actualLines = actualFile.split("(\\r?\\n)"); - if (actualLines[0].startsWith("// Generated by delombok at ")) { - actualLines[0] = ""; - } - expectedLines = removeBlanks(expectedLines); - actualLines = removeBlanks(actualLines); - int size = Math.min(expectedLines.length, actualLines.length); - for (int i = 0; i < size; i++) { - String expected = expectedLines[i]; - String actual = actualLines[i]; - if (!expected.equals(actual)) { - fail(String.format("Difference in line %s(%d):\nExpected `%s`\nGot `%s`\n", name, i, expected, actual)); - } - } - if (expectedLines.length > actualLines.length) { - fail(String.format("Missing line %s(%d): %s\n", name, size, expectedLines[size])); - } - if (expectedLines.length < actualLines.length) { - fail(String.format("Extra line %s(%d): %s\n", name, size, actualLines[size])); - } - } - - private String readAfter(File file) throws IOException { - BufferedReader reader = new BufferedReader(new FileReader(new File(AFTER_FOLDER, file.getName()))); - StringBuilder result = new StringBuilder(); - String line; - while ((line = reader.readLine()) != null) { - result.append(line); - result.append(LINE_SEPARATOR); - } - reader.close(); - return result.toString(); + TestViaDelombok.runComparison(BEFORE_DIR, AFTER_DIR); } } -- cgit