aboutsummaryrefslogtreecommitdiff
path: root/src/lombok/javac/handlers/HandlePrintAST.java
blob: f2009c7153ce129c294b1a7f9802cc184821d319 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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;
import lombok.javac.JavacAnnotationHandler;
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) {
		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(annotation.getInstance().printContent(), stream));
		
		return true;
	}
}