aboutsummaryrefslogtreecommitdiff
path: root/src/lombok/javac/apt/Processor.java
blob: 012161fd08ff1b3dd712e976f7e4ad29b5f47a46 (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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package lombok.javac.apt;

import java.util.Set;

import javax.annotation.processing.AbstractProcessor;
import javax.annotation.processing.ProcessingEnvironment;
import javax.annotation.processing.RoundEnvironment;
import javax.annotation.processing.SupportedAnnotationTypes;
import javax.annotation.processing.SupportedSourceVersion;
import javax.lang.model.SourceVersion;
import javax.lang.model.element.Element;
import javax.lang.model.element.TypeElement;

import lombok.javac.HandlerLibrary;
import lombok.javac.JavacNode;

import com.sun.source.util.Trees;
import com.sun.tools.javac.processing.JavacProcessingEnvironment;


@SupportedAnnotationTypes("*")
@SupportedSourceVersion(SourceVersion.RELEASE_6)
public class Processor extends AbstractProcessor {
	private JavacProcessingEnvironment processingEnv;
	private HandlerLibrary handlers;
	private Trees trees;
	
	@Override public void init(ProcessingEnvironment processingEnv) {
		super.init(processingEnv);
		if ( !(processingEnv instanceof JavacProcessingEnvironment) ) this.processingEnv = null;
		else {
			this.processingEnv = (JavacProcessingEnvironment) processingEnv;
			handlers = HandlerLibrary.load(processingEnv.getMessager());
		}
	}
	
	@Override public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
		if ( processingEnv == null ) return false;
		
		trees = Trees.instance(processingEnv);
		
		for ( TypeElement annotationType : annotations ) {
			if ( !handlers.hasHandlerFor(annotationType) ) continue;
			for ( Element element : roundEnv.getElementsAnnotatedWith(annotationType) ) {
				handlers.handleAnnotation(createNode(element), annotationType);
			}
		}
		
		for ( Element element : roundEnv.getRootElements() ) {
			if ( element instanceof TypeElement ) {
				handlers.handleType((TypeElement)element);
			}
		}
		
		return false;
	}
	
	private JavacNode createNode(Element element) {
		return new JavacNode(trees, processingEnv, element);
	}
}