aboutsummaryrefslogtreecommitdiff
path: root/src/delombok
diff options
context:
space:
mode:
authorRoel Spilker <r.spilker@gmail.com>2009-11-26 01:45:55 +0100
committerRoel Spilker <r.spilker@gmail.com>2009-11-26 01:45:55 +0100
commit6cd3b8a8ca4809efd2001473f31c881c9f2381ea (patch)
tree2e7924edcb3af6f3826b582bdbf8254dcc238bb1 /src/delombok
parent4c73407dc7f68fffd7d3166f6e01118d331d3a68 (diff)
downloadlombok-6cd3b8a8ca4809efd2001473f31c881c9f2381ea.tar.gz
lombok-6cd3b8a8ca4809efd2001473f31c881c9f2381ea.tar.bz2
lombok-6cd3b8a8ca4809efd2001473f31c881c9f2381ea.zip
Major restructuring+adding tests
Diffstat (limited to 'src/delombok')
-rw-r--r--src/delombok/lombok/delombok/Comment.java4
-rw-r--r--src/delombok/lombok/delombok/CommentCollectingScanner.java85
-rw-r--r--src/delombok/lombok/delombok/CommentPreservingParser.java77
-rw-r--r--src/delombok/lombok/delombok/PrettyCommentsPrinter.java6
-rw-r--r--src/delombok/lombok/delombok/PrettyPrinter.java198
5 files changed, 166 insertions, 204 deletions
diff --git a/src/delombok/lombok/delombok/Comment.java b/src/delombok/lombok/delombok/Comment.java
index 6f6910b4..c264733f 100644
--- a/src/delombok/lombok/delombok/Comment.java
+++ b/src/delombok/lombok/delombok/Comment.java
@@ -21,11 +21,11 @@
*/
package lombok.delombok;
-class Comment {
+public final class Comment {
final int pos;
final String content;
- Comment(int pos, String content) {
+ public Comment(int pos, String content) {
this.pos = pos;
this.content = content;
}
diff --git a/src/delombok/lombok/delombok/CommentCollectingScanner.java b/src/delombok/lombok/delombok/CommentCollectingScanner.java
new file mode 100644
index 00000000..6f593c7f
--- /dev/null
+++ b/src/delombok/lombok/delombok/CommentCollectingScanner.java
@@ -0,0 +1,85 @@
+/*
+ * 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.nio.CharBuffer;
+
+import lombok.delombok.CommentPreservingParser.Comments;
+
+import com.sun.tools.javac.parser.Scanner;
+import com.sun.tools.javac.util.Context;
+
+public class CommentCollectingScanner extends Scanner {
+
+ private final Comments comments;
+
+ /** A factory for creating scanners. */
+ public static class Factory extends Scanner.Factory {
+
+ private final Context context;
+
+ public static void preRegister(final Context context) {
+ context.put(scannerFactoryKey, new Context.Factory<Scanner.Factory>() {
+ public CommentCollectingScanner.Factory make() {
+ return new Factory(context);
+ }
+ });
+ }
+
+ /** Create a new scanner factory. */
+ protected Factory(Context context) {
+ super(context);
+ this.context = context;
+ }
+
+ @Override
+ public Scanner newScanner(CharSequence input) {
+ if (input instanceof CharBuffer) {
+ return new CommentCollectingScanner(this, (CharBuffer)input, context.get(Comments.class));
+ }
+ char[] array = input.toString().toCharArray();
+ return newScanner(array, array.length);
+ }
+
+ @Override
+ public Scanner newScanner(char[] input, int inputLength) {
+ return new CommentCollectingScanner(this, input, inputLength, context.get(Comments.class));
+ }
+ }
+
+
+ public CommentCollectingScanner(CommentCollectingScanner.Factory factory, CharBuffer charBuffer, Comments comments) {
+ super(factory, charBuffer);
+ this.comments = comments;
+ }
+
+
+ public CommentCollectingScanner(CommentCollectingScanner.Factory factory, char[] input, int inputLength, Comments comments) {
+ super(factory, input, inputLength);
+ this.comments = comments;
+ }
+
+ @Override
+ protected void processComment(CommentStyle style) {
+ comments.add(pos(), new String(getRawCharacters(pos(), endPos())));
+ }
+} \ No newline at end of file
diff --git a/src/delombok/lombok/delombok/CommentPreservingParser.java b/src/delombok/lombok/delombok/CommentPreservingParser.java
new file mode 100644
index 00000000..c85124b5
--- /dev/null
+++ b/src/delombok/lombok/delombok/CommentPreservingParser.java
@@ -0,0 +1,77 @@
+/*
+ * 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.IOException;
+import java.io.Writer;
+
+import com.sun.tools.javac.main.JavaCompiler;
+import com.sun.tools.javac.tree.JCTree.JCCompilationUnit;
+import com.sun.tools.javac.util.Context;
+import com.sun.tools.javac.util.List;
+
+public class CommentPreservingParser {
+ private final JavaCompiler compiler;
+ private final Context context;
+
+ public CommentPreservingParser(Context context) {
+ this.context = context;
+ CommentCollectingScanner.Factory.preRegister(context);
+ 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);
+ @SuppressWarnings("deprecation")
+ JCCompilationUnit cu = compiler.parse(fileName);
+ return new ParseResult(comments.comments, cu);
+ }
+
+ static class Comments {
+ List<Comment> comments = List.nil();
+
+ void add(int pos, String content) {
+ comments = comments.append(new Comment(pos, content));
+ }
+ }
+
+ public static class ParseResult {
+ private final List<Comment> comments;
+ private final JCCompilationUnit compilationUnit;
+
+ private ParseResult(List<Comment> comments, JCCompilationUnit compilationUnit) {
+ this.comments = comments;
+ this.compilationUnit = compilationUnit;
+ }
+
+ public void print(Writer out) {
+ compilationUnit.accept(new PrettyCommentsPrinter(out, compilationUnit, comments));
+ }
+ }
+} \ No newline at end of file
diff --git a/src/delombok/lombok/delombok/PrettyCommentsPrinter.java b/src/delombok/lombok/delombok/PrettyCommentsPrinter.java
index aa42f2b5..c6b58a31 100644
--- a/src/delombok/lombok/delombok/PrettyCommentsPrinter.java
+++ b/src/delombok/lombok/delombok/PrettyCommentsPrinter.java
@@ -112,17 +112,16 @@ public class PrettyCommentsPrinter extends JCTree.Visitor {
private static final Method GET_TAG_METHOD;
private static final Field TAG_FIELD;
+
static {
Method m = null;
Field f = null;
try {
m = JCTree.class.getDeclaredMethod("getTag");
- System.out.println("Using getTag method");
}
catch (NoSuchMethodException e) {
try {
f = JCTree.class.getDeclaredField("tag");
- System.out.println("Using tag field");
}
catch (NoSuchFieldException e1) {
e1.printStackTrace();
@@ -152,12 +151,11 @@ public class PrettyCommentsPrinter extends JCTree.Visitor {
}
}
-
private List<Comment> comments;
private final JCCompilationUnit cu;
private boolean newLine = true;
- public PrettyCommentsPrinter(Writer out, List<Comment> comments, JCCompilationUnit cu) {
+ public PrettyCommentsPrinter(Writer out, JCCompilationUnit cu, List<Comment> comments) {
this.out = out;
this.comments = comments;
this.cu = cu;
diff --git a/src/delombok/lombok/delombok/PrettyPrinter.java b/src/delombok/lombok/delombok/PrettyPrinter.java
deleted file mode 100644
index caea38c9..00000000
--- a/src/delombok/lombok/delombok/PrettyPrinter.java
+++ /dev/null
@@ -1,198 +0,0 @@
-/*
- * 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.FileReader;
-import java.io.StringWriter;
-import java.nio.CharBuffer;
-
-import com.sun.source.tree.Tree;
-import com.sun.source.tree.Tree.Kind;
-import com.sun.source.util.SimpleTreeVisitor;
-import com.sun.source.util.TreeScanner;
-import com.sun.tools.javac.main.JavaCompiler;
-import com.sun.tools.javac.main.OptionName;
-import com.sun.tools.javac.parser.Scanner;
-import com.sun.tools.javac.tree.JCTree;
-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 PrettyPrinter {
-
- private final Context context = new Context();
- private final JavaCompiler compiler;
-
- public static void main(String[] args) throws Exception {
- if (args.length == 0) {
-// System.out.println("Usage: java %s <file1> <file2>...");
- args = new String[] {"tests/foo/WithComments.java"};
- }
- PrettyPrinter prettyPrinter = new PrettyPrinter();
- for (String fileName : args) {
- prettyPrinter.print(fileName);
- }
- }
-
- public PrettyPrinter() {
- Options.instance(context).put(OptionName.ENCODING, "utf-8");
- PrettyDocCommentScanner.Factory.preRegister(context);
- compiler = new JavaCompiler(context) {
- @Override
- protected boolean keepComments() {
- return true;
- }
- };
- compiler.genEndPos = true;
- }
-
- public static class Comments {
-// private static Key<Comments> commentsKey = new Key<Comments>();
-
- private List<Comment> comments = List.nil();
-
- void add(int pos, String content) {
- comments = comments.append(new Comment(pos, content));
- }
- }
-
- private void print(String fileName) throws Exception {
-
- Comments comments = new Comments();
- context.put(Comments.class, comments);
-
- @SuppressWarnings("deprecation")
- final JCCompilationUnit cu = compiler.parse(fileName);
-
- StringWriter writer = new StringWriter();
- cu.accept(new PrettyCommentsPrinter(writer, comments.comments, cu));
- System.out.printf("####### Original source of %s #######\n", fileName);
- BufferedReader reader = new BufferedReader(new FileReader(fileName));
- String line = null;
- while ((line = reader.readLine()) != null) {
- System.out.println(line);
- }
- reader.close();
-
- System.out.printf("####### Generated source of %s #######\n", fileName);
- System.out.println(writer);
-
- printNodes(fileName, cu);
- System.out.printf("####### Comments of %s #######\n", fileName);
- for (Comment comment : comments.comments) {
- System.out.println(comment.summary());
- }
- System.out.printf("####### End of %s #######\n\n", fileName);
- }
-
- private void printNodes(String fileName, final JCCompilationUnit cu) {
- System.out.printf("####### Nodes of %s #######\n", fileName);
- cu.accept(new TreeScanner<Void, Void>(){
- @Override
- public Void scan(Tree node, Void p) {
- if (node == null) {
- return null;
- }
- node.accept(new SimpleTreeVisitor<Void, Void>(){
- @SuppressWarnings("incomplete-switch")
- @Override
- protected Void defaultAction(Tree treeNode, Void r) {
- if (treeNode == null) {
- return null;
- }
- JCTree tree = (JCTree)treeNode;
- Kind kind = tree.getKind();
- System.out.print(kind.toString() + " " + tree.pos + " - " + tree.getEndPosition(cu.endPositions));
- switch (kind) {
- case IDENTIFIER:
- case PRIMITIVE_TYPE:
- case MODIFIERS:
- System.out.print("[" + tree.toString() + "]");
- break;
- }
- System.out.println();
- return null;
- }
- }, null);
- return super.scan(node, p);
- }
- }, null);
- }
-
- public static class PrettyDocCommentScanner extends Scanner {
-
- private final Comments comments;
-
- /** A factory for creating scanners. */
- public static class Factory extends Scanner.Factory {
-
- private final Context context;
-
- public static void preRegister(final Context context) {
- context.put(scannerFactoryKey, new Context.Factory<Scanner.Factory>() {
- public Factory make() {
- return new Factory(context);
- }
- });
- }
-
- /** Create a new scanner factory. */
- protected Factory(Context context) {
- super(context);
- this.context = context;
- }
-
- @Override
- public Scanner newScanner(CharSequence input) {
- if (input instanceof CharBuffer) {
- return new PrettyDocCommentScanner(this, (CharBuffer)input, context.get(Comments.class));
- }
- char[] array = input.toString().toCharArray();
- return newScanner(array, array.length);
- }
-
- @Override
- public Scanner newScanner(char[] input, int inputLength) {
- return new PrettyDocCommentScanner(this, input, inputLength, context.get(Comments.class));
- }
- }
-
-
- public PrettyDocCommentScanner(PrettyPrinter.PrettyDocCommentScanner.Factory factory, CharBuffer charBuffer, Comments comments) {
- super(factory, charBuffer);
- this.comments = comments;
- }
-
-
- public PrettyDocCommentScanner(PrettyPrinter.PrettyDocCommentScanner.Factory factory, char[] input, int inputLength, Comments comments) {
- super(factory, input, inputLength);
- this.comments = comments;
- }
-
- @Override
- protected void processComment(CommentStyle style) {
- comments.add(pos(), new String(getRawCharacters(pos(), endPos())));
- }
- }
-}