Major restructuring+adding tests

This commit is contained in:
Roel Spilker committed 2009-11-26 01:45:55 +01:00
1 parent 4c73407dc7
commit 6cd3b8a8ca
10 files changed
+274 -204

No files matched your search

+2
View File
@@ -5,12 +5,14 @@
<classpathentry kind="src" path="src/installer"/>
<classpathentry kind="src" path="src/delombok"/>
<classpathentry kind="src" path="experimental/src"/>
<classpathentry kind="src" path="test/delombok/src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="lib" path="deps/lombok/org.eclipse.jdt.core_3.5.0.v_963.jar"/>
<classpathentry kind="lib" path="deps/lombok/org.eclipse.jdt.ui_3.5.1.r351_v20090821-0800.jar"/>
<classpathentry kind="lib" path="deps/lombok/org.eclipse.core.runtime_3.5.0.v20090525.jar"/>
<classpathentry kind="lib" path="deps/lombok/org.eclipse.osgi_3.5.0.v20090520.jar"/>
<classpathentry kind="lib" path="deps/lombok/org.eclipse.equinox.common_3.5.0.v20090520-1800.jar"/>
<classpathentry kind="lib" path="deps/junit-4.7.jar"/>
<classpathentry combineaccessrules="false" kind="src" path="/lombok.patcher"/>
<classpathentry kind="lib" path="deps/lombok/tools.jar"/>
<classpathentry kind="lib" path="deps/lombok/spi-0.2.4.jar"/>
BIN
View File
Binary file not shown.
+2 -2
View File
@@ -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;
}
@@ -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())));
}
}
@@ -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));
}
}
}
@@ -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;
@@ -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())));
}
}
}
@@ -0,0 +1,5 @@
// Cool Comments
public class WithComments {
// Also inside the body
}
@@ -0,0 +1,4 @@
// Cool Comments
public class WithComments {
// Also inside the body
}
@@ -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();
}
}