mirror of
https://github.com/tiennm99/lombok.git
synced 2026-10-15 10:15:19 +00:00
Expanded the AST printers to support a target PrintStream, and expanded the @PrintAST annotation to let you supply an optional filename. Useful particularly for IDEs, which don't usually have a viewable console.
Also renamed the printers to just 'Printer', as they are already inner classes of a specifically named type (JavacASTVisitor & co).
This commit is contained in:
1 parent
eca3cd7ccc
commit
6254337948
5 files changed
+74
-8
No files matched your search
@@ -7,4 +7,6 @@ import java.lang.annotation.Target;
|
||||
|
||||
@Target({ElementType.CONSTRUCTOR, ElementType.FIELD, ElementType.LOCAL_VARIABLE, ElementType.METHOD, ElementType.TYPE})
|
||||
@Retention(RetentionPolicy.SOURCE)
|
||||
public @interface PrintAST {}
|
||||
public @interface PrintAST {
|
||||
String outfile() default "";
|
||||
}
|
||||
@@ -1,5 +1,8 @@
|
||||
package lombok.eclipse;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.PrintStream;
|
||||
import java.lang.reflect.Modifier;
|
||||
|
||||
import lombok.eclipse.EclipseAST.Node;
|
||||
@@ -79,12 +82,26 @@ public interface EclipseASTVisitor {
|
||||
void visitStatement(Node statementNode, Statement statement);
|
||||
void endVisitStatement(Node statementNode, Statement statement);
|
||||
|
||||
public static class EclipseASTPrinter implements EclipseASTVisitor {
|
||||
public static class Printer implements EclipseASTVisitor {
|
||||
private final PrintStream out;
|
||||
public Printer() {
|
||||
this(System.out);
|
||||
}
|
||||
|
||||
public Printer(File file) throws FileNotFoundException {
|
||||
this(new PrintStream(file));
|
||||
}
|
||||
|
||||
public Printer(PrintStream out) {
|
||||
this.out = out;
|
||||
}
|
||||
|
||||
int indent = 0;
|
||||
private void print(String text, Object... params) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for ( int i = 0 ; i < indent ; i++ ) sb.append(" ");
|
||||
System.out.printf(sb.append(text).append('\n').toString(), params);
|
||||
out.printf(sb.append(text).append('\n').toString(), params);
|
||||
out.flush();
|
||||
}
|
||||
|
||||
private String str(char[] c) {
|
||||
|
||||
@@ -1,8 +1,13 @@
|
||||
package lombok.eclipse.handlers;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.PrintStream;
|
||||
|
||||
import org.eclipse.jdt.internal.compiler.ast.Annotation;
|
||||
import org.mangosdk.spi.ProviderFor;
|
||||
|
||||
import lombok.Lombok;
|
||||
import lombok.core.AnnotationValues;
|
||||
import lombok.core.PrintAST;
|
||||
import lombok.eclipse.EclipseASTVisitor;
|
||||
@@ -13,7 +18,16 @@ import lombok.eclipse.EclipseAST.Node;
|
||||
public class HandlePrintAST implements EclipseAnnotationHandler<PrintAST> {
|
||||
@Override public boolean handle(AnnotationValues<PrintAST> annotation, Annotation ast, Node annotationNode) {
|
||||
if ( !annotationNode.isCompleteParse() ) return false;
|
||||
annotationNode.up().traverse(new EclipseASTVisitor.EclipseASTPrinter());
|
||||
|
||||
PrintStream stream = System.out;
|
||||
String fileName = annotation.getInstance().outfile();
|
||||
if ( fileName.length() > 0 ) try {
|
||||
stream = new PrintStream(new File(fileName));
|
||||
} catch ( FileNotFoundException e ) {
|
||||
Lombok.sneakyThrow(e);
|
||||
}
|
||||
|
||||
annotationNode.up().traverse(new EclipseASTVisitor.Printer(stream));
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,9 @@
|
||||
package lombok.javac;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.PrintStream;
|
||||
|
||||
import lombok.javac.JavacAST.Node;
|
||||
|
||||
import com.sun.tools.javac.tree.JCTree;
|
||||
@@ -66,16 +70,31 @@ public interface JavacASTVisitor {
|
||||
void visitStatement(Node statementNode, JCTree statement);
|
||||
void endVisitStatement(Node statementNode, JCTree statement);
|
||||
|
||||
public static class JavacASTPrinter implements JavacASTVisitor {
|
||||
public static class Printer implements JavacASTVisitor {
|
||||
private final PrintStream out;
|
||||
|
||||
public Printer() {
|
||||
this(System.out);
|
||||
}
|
||||
|
||||
public Printer(File file) throws FileNotFoundException {
|
||||
this(new PrintStream(file));
|
||||
}
|
||||
|
||||
public Printer(PrintStream out) {
|
||||
this.out = out;
|
||||
}
|
||||
|
||||
int indent = 0;
|
||||
private void print(String text, Object... params) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for ( int i = 0 ; i < indent ; i++ ) sb.append(" ");
|
||||
System.out.printf(sb.append(text).append('\n').toString(), params);
|
||||
out.printf(sb.append(text).append('\n').toString(), params);
|
||||
out.flush();
|
||||
}
|
||||
|
||||
@Override public void visitCompilationUnit(Node Node, JCCompilationUnit unit) {
|
||||
System.out.println("---------------------------------------------------------");
|
||||
out.println("---------------------------------------------------------");
|
||||
|
||||
print("<CU %s>", Node.getFileName());
|
||||
indent++;
|
||||
|
||||
@@ -1,9 +1,14 @@
|
||||
package lombok.javac.handlers;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.PrintStream;
|
||||
|
||||
import org.mangosdk.spi.ProviderFor;
|
||||
|
||||
import com.sun.tools.javac.tree.JCTree.JCAnnotation;
|
||||
|
||||
import lombok.Lombok;
|
||||
import lombok.core.AnnotationValues;
|
||||
import lombok.core.PrintAST;
|
||||
import lombok.javac.JavacASTVisitor;
|
||||
@@ -13,7 +18,16 @@ import lombok.javac.JavacAST.Node;
|
||||
@ProviderFor(JavacAnnotationHandler.class)
|
||||
public class HandlePrintAST implements JavacAnnotationHandler<PrintAST> {
|
||||
@Override public boolean handle(AnnotationValues<PrintAST> annotation, JCAnnotation ast, Node annotationNode) {
|
||||
annotationNode.up().traverse(new JavacASTVisitor.JavacASTPrinter());
|
||||
PrintStream stream = System.out;
|
||||
String fileName = annotation.getInstance().outfile();
|
||||
if ( fileName.length() > 0 ) try {
|
||||
stream = new PrintStream(new File(fileName));
|
||||
} catch ( FileNotFoundException e ) {
|
||||
Lombok.sneakyThrow(e);
|
||||
}
|
||||
|
||||
annotationNode.up().traverse(new JavacASTVisitor.Printer(stream));
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user