aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/core/lombok/javac/JavacAST.java5
-rw-r--r--src/core/lombok/javac/JavacTransformer.java98
-rw-r--r--src/core/lombok/javac/apt/Processor.java67
-rw-r--r--src/delombok/lombok/delombok/CommentPreservingParser.java32
-rw-r--r--test/delombok/src/lombok/delombok/TestLombokFiles.java95
-rw-r--r--test/delombok/src/lombok/delombok/TestSourceFiles.java21
-rw-r--r--test/lombok/resource/after/CommentsInterspersed.java26
-rw-r--r--test/lombok/resource/before/CommentsInterspersed.java14
8 files changed, 285 insertions, 73 deletions
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<JavacAST, JavacNode, JCTree> {
/**
* 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<JCCompilationUnit> compilationUnits) {
+ List<JavacAST> asts = new ArrayList<JavacAST>();
+
+ 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<JavacAST> asts = new ArrayList<JavacAST>();
+ 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<Comment> 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<String> out = new ArrayList<String>();
+ 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!
+