diff options
-rw-r--r-- | src/lombok/core/PrintAST.java | 6 | ||||
-rw-r--r-- | src/lombok/eclipse/EclipseASTVisitor.java | 43 | ||||
-rw-r--r-- | src/lombok/eclipse/HandlerLibrary.java | 14 | ||||
-rw-r--r-- | src/lombok/eclipse/TransformEclipseAST.java | 3 | ||||
-rw-r--r-- | src/lombok/eclipse/handlers/HandlePrintAST.java | 2 | ||||
-rw-r--r-- | src/lombok/javac/JavacASTVisitor.java | 45 | ||||
-rw-r--r-- | src/lombok/javac/handlers/HandlePrintAST.java | 2 |
7 files changed, 92 insertions, 23 deletions
diff --git a/src/lombok/core/PrintAST.java b/src/lombok/core/PrintAST.java index 9fb12b2c..6d1eaafd 100644 --- a/src/lombok/core/PrintAST.java +++ b/src/lombok/core/PrintAST.java @@ -9,4 +9,10 @@ import java.lang.annotation.Target; @Retention(RetentionPolicy.SOURCE) public @interface PrintAST { String outfile() default ""; + + /** + * Normally, the printer will print each node focusing on the node (E.g. classname, and such). By setting printContent to true, + * methods, initializers, and other statement-containing elements actually print their java code instead of element class names. + */ + boolean printContent() default false; } diff --git a/src/lombok/eclipse/EclipseASTVisitor.java b/src/lombok/eclipse/EclipseASTVisitor.java index df7466d5..bebabc6b 100644 --- a/src/lombok/eclipse/EclipseASTVisitor.java +++ b/src/lombok/eclipse/EclipseASTVisitor.java @@ -84,26 +84,34 @@ public interface EclipseASTVisitor { public static class Printer implements EclipseASTVisitor { private final PrintStream out; - public Printer() { - this(System.out); + private final boolean printContent; + private int disablePrinting = 0; + private int indent = 0; + + public Printer(boolean printContent) { + this(printContent, System.out); } - public Printer(File file) throws FileNotFoundException { - this(new PrintStream(file)); + public Printer(boolean printContent, File file) throws FileNotFoundException { + this(printContent, new PrintStream(file)); } - public Printer(PrintStream out) { + public Printer(boolean printContent, PrintStream out) { + this.printContent = printContent; this.out = out; } - int indent = 0; - private void print(String text, Object... params) { + private void forcePrint(String text, Object... params) { StringBuilder sb = new StringBuilder(); for ( int i = 0 ; i < indent ; i++ ) sb.append(" "); out.printf(sb.append(text).append('\n').toString(), params); out.flush(); } + private void print(String text, Object... params) { + if ( disablePrinting == 0 ) forcePrint(text, params); + } + private String str(char[] c) { if ( c == null ) return "(NULL)"; else return new String(c); @@ -140,7 +148,7 @@ public interface EclipseASTVisitor { } @Override public void visitAnnotationOnType(TypeDeclaration type, Node node, Annotation annotation) { - print("<ANNOTATION: %s />", annotation); + forcePrint("<ANNOTATION: %s />", annotation); } @Override public void endVisitType(Node node, TypeDeclaration type) { @@ -155,9 +163,14 @@ public interface EclipseASTVisitor { (initializer.modifiers & Modifier.STATIC) != 0 ? "static" : "instance", s ? "filled" : "blank"); indent++; + if ( printContent ) { + if ( initializer.block != null ) print("%s", initializer.block); + disablePrinting++; + } } @Override public void endVisitInitializer(Node node, Initializer initializer) { + if ( printContent ) disablePrinting--; indent--; print("</%s INITIALIZER>", (initializer.modifiers & Modifier.STATIC) != 0 ? "static" : "instance"); } @@ -165,13 +178,18 @@ public interface EclipseASTVisitor { @Override public void visitField(Node node, FieldDeclaration field) { print("<FIELD %s %s = %s>", str(field.type), str(field.name), field.initialization); indent++; + if ( printContent ) { + if ( field.initialization != null ) print("%s", field.initialization); + disablePrinting++; + } } @Override public void visitAnnotationOnField(FieldDeclaration field, Node node, Annotation annotation) { - print("<ANNOTATION: %s />", annotation); + forcePrint("<ANNOTATION: %s />", annotation); } @Override public void endVisitField(Node node, FieldDeclaration field) { + if ( printContent ) disablePrinting--; indent--; print("</FIELD %s %s>", str(field.type), str(field.name)); } @@ -180,13 +198,18 @@ public interface EclipseASTVisitor { String type = method instanceof ConstructorDeclaration ? "CONSTRUCTOR" : "METHOD"; print("<%s %s: %s>", type, str(method.selector), method.statements != null ? "filled" : "blank"); indent++; + if ( printContent ) { + if ( method.statements != null ) print("%s", method); + disablePrinting++; + } } @Override public void visitAnnotationOnMethod(AbstractMethodDeclaration method, Node node, Annotation annotation) { - print("<ANNOTATION: %s />", annotation); + forcePrint("<ANNOTATION: %s />", annotation); } @Override public void endVisitMethod(Node node, AbstractMethodDeclaration method) { + if ( printContent ) disablePrinting--; String type = method instanceof ConstructorDeclaration ? "CONSTRUCTOR" : "METHOD"; indent--; print("</%s %s>", type, str(method.selector)); diff --git a/src/lombok/eclipse/HandlerLibrary.java b/src/lombok/eclipse/HandlerLibrary.java index 10180963..37610a33 100644 --- a/src/lombok/eclipse/HandlerLibrary.java +++ b/src/lombok/eclipse/HandlerLibrary.java @@ -12,6 +12,7 @@ import java.util.ServiceConfigurationError; import java.util.ServiceLoader; import lombok.core.AnnotationValues; +import lombok.core.PrintAST; import lombok.core.SpiLoadUtil; import lombok.core.TypeLibrary; import lombok.core.TypeResolver; @@ -44,6 +45,8 @@ public class HandlerLibrary { new HashMap<String, AnnotationHandlerContainer<?>>(); private Collection<EclipseASTVisitor> visitorHandlers = new ArrayList<EclipseASTVisitor>(); + + private boolean skipPrintAST; public static HandlerLibrary load() { HandlerLibrary lib = new HandlerLibrary(); @@ -93,6 +96,8 @@ public class HandlerLibrary { if ( rawType == null ) return false; boolean handled = false; for ( String fqn : resolver.findTypeMatches(annotationNode, toQualifiedName(annotation.type.getTypeName())) ) { + boolean isPrintAST = fqn.equals(PrintAST.class.getName()); + if ( isPrintAST == skipPrintAST ) continue; AnnotationHandlerContainer<?> container = annotationHandlers.get(fqn); if ( container == null ) continue; @@ -100,6 +105,7 @@ public class HandlerLibrary { try { handled |= container.handle(annotation, annotationNode); } catch ( AnnotationValueDecodeFail fail ) { + fail.printStackTrace(); //TODO debug! fail.owner.setError(fail.getMessage(), fail.idx); } catch ( Throwable t ) { Eclipse.error(ast, String.format("Lombok annotation handler %s failed", container.handler.getClass()), t); @@ -117,4 +123,12 @@ public class HandlerLibrary { String.format("Lombok visitor handler %s failed", visitor.getClass()), t); } } + + public void skipPrintAST() { + skipPrintAST = true; + } + + public void skipAllButPrintAST() { + skipPrintAST = false; + } } diff --git a/src/lombok/eclipse/TransformEclipseAST.java b/src/lombok/eclipse/TransformEclipseAST.java index a43be699..66d663f4 100644 --- a/src/lombok/eclipse/TransformEclipseAST.java +++ b/src/lombok/eclipse/TransformEclipseAST.java @@ -103,8 +103,11 @@ public class TransformEclipseAST { } public void go() { + handlers.skipPrintAST(); ast.traverse(new AnnotationVisitor()); handlers.callASTVisitors(ast); + handlers.skipAllButPrintAST(); + ast.traverse(new AnnotationVisitor()); } private static class AnnotationVisitor extends EclipseASTAdapter { diff --git a/src/lombok/eclipse/handlers/HandlePrintAST.java b/src/lombok/eclipse/handlers/HandlePrintAST.java index ae34c5c2..cbd2cae1 100644 --- a/src/lombok/eclipse/handlers/HandlePrintAST.java +++ b/src/lombok/eclipse/handlers/HandlePrintAST.java @@ -27,7 +27,7 @@ public class HandlePrintAST implements EclipseAnnotationHandler<PrintAST> { Lombok.sneakyThrow(e); } - annotationNode.up().traverse(new EclipseASTVisitor.Printer(stream)); + annotationNode.up().traverse(new EclipseASTVisitor.Printer(annotation.getInstance().printContent(), stream)); return true; } } diff --git a/src/lombok/javac/JavacASTVisitor.java b/src/lombok/javac/JavacASTVisitor.java index 714ae294..15d848dd 100644 --- a/src/lombok/javac/JavacASTVisitor.java +++ b/src/lombok/javac/JavacASTVisitor.java @@ -73,27 +73,34 @@ public interface JavacASTVisitor { public static class Printer implements JavacASTVisitor { private final PrintStream out; + private final boolean printContent; + private int disablePrinting = 0; + private int indent = 0; - public Printer() { - this(System.out); + public Printer(boolean printContent) { + this(printContent, System.out); } - public Printer(File file) throws FileNotFoundException { - this(new PrintStream(file)); + public Printer(boolean printContent, File file) throws FileNotFoundException { + this(printContent, new PrintStream(file)); } - public Printer(PrintStream out) { + public Printer(boolean printContent, PrintStream out) { + this.printContent = printContent; this.out = out; } - int indent = 0; - private void print(String text, Object... params) { + private void forcePrint(String text, Object... params) { StringBuilder sb = new StringBuilder(); for ( int i = 0 ; i < indent ; i++ ) sb.append(" "); out.printf(sb.append(text).append('\n').toString(), params); out.flush(); } + private void print(String text, Object... params) { + if ( disablePrinting == 0 ) forcePrint(text, params); + } + @Override public void visitCompilationUnit(Node Node, JCCompilationUnit unit) { out.println("---------------------------------------------------------"); @@ -112,7 +119,7 @@ public interface JavacASTVisitor { } @Override public void visitAnnotationOnType(JCClassDecl type, Node node, JCAnnotation annotation) { - print("<ANNOTATION: %s />", annotation); + forcePrint("<ANNOTATION: %s />", annotation); } @Override public void endVisitType(Node node, JCClassDecl type) { @@ -124,9 +131,14 @@ public interface JavacASTVisitor { print("<%s INITIALIZER>", initializer.isStatic() ? "static" : "instance"); indent++; + if ( printContent ) { + print("%s", initializer); + disablePrinting++; + } } @Override public void endVisitInitializer(Node node, JCBlock initializer) { + if ( printContent ) disablePrinting--; indent--; print("</%s INITIALIZER>", initializer.isStatic() ? "static" : "instance"); } @@ -134,13 +146,18 @@ public interface JavacASTVisitor { @Override public void visitField(Node node, JCVariableDecl field) { print("<FIELD %s %s>", field.vartype, field.name); indent++; + if ( printContent ) { + if ( field.init != null ) print("%s", field.init); + disablePrinting++; + } } @Override public void visitAnnotationOnField(JCVariableDecl field, Node node, JCAnnotation annotation) { - print("<ANNOTATION: %s />", annotation); + forcePrint("<ANNOTATION: %s />", annotation); } @Override public void endVisitField(Node node, JCVariableDecl field) { + if ( printContent ) disablePrinting--; indent--; print("</FIELD %s %s>", field.vartype, field.name); } @@ -154,13 +171,19 @@ public interface JavacASTVisitor { } else type = "METHOD"; print("<%s %s> returns: %s", type, method.name, method.restype); indent++; + if ( printContent ) { + if ( method.body == null ) print("(ABSTRACT)"); + else print("%s", method.body); + disablePrinting++; + } } @Override public void visitAnnotationOnMethod(JCMethodDecl method, Node node, JCAnnotation annotation) { - print("<ANNOTATION: %s />", annotation); + forcePrint("<ANNOTATION: %s />", annotation); } @Override public void endVisitMethod(Node node, JCMethodDecl method) { + if ( printContent ) disablePrinting--; indent--; print("</%s %s>", "XMETHOD", method.name); } @@ -171,7 +194,7 @@ public interface JavacASTVisitor { } @Override public void visitAnnotationOnMethodArgument(JCVariableDecl arg, JCMethodDecl method, Node nodeAnnotation, JCAnnotation annotation) { - print("<ANNOTATION: %s />", annotation); + forcePrint("<ANNOTATION: %s />", annotation); } @Override public void endVisitMethodArgument(Node node, JCVariableDecl arg, JCMethodDecl method) { diff --git a/src/lombok/javac/handlers/HandlePrintAST.java b/src/lombok/javac/handlers/HandlePrintAST.java index 6becb7b8..f2009c71 100644 --- a/src/lombok/javac/handlers/HandlePrintAST.java +++ b/src/lombok/javac/handlers/HandlePrintAST.java @@ -26,7 +26,7 @@ public class HandlePrintAST implements JavacAnnotationHandler<PrintAST> { Lombok.sneakyThrow(e); } - annotationNode.up().traverse(new JavacASTVisitor.Printer(stream)); + annotationNode.up().traverse(new JavacASTVisitor.Printer(annotation.getInstance().printContent(), stream)); return true; } |