-
Notifications
You must be signed in to change notification settings - Fork 7
/
Processor.java
176 lines (152 loc) · 7.37 KB
/
Processor.java
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package com.hitanshudhawan.butterknife_compiler;
import com.hitanshudhawan.butterknife_annotations.BindView;
import com.hitanshudhawan.butterknife_annotations.OnClick;
import com.squareup.javapoet.ClassName;
import com.squareup.javapoet.JavaFile;
import com.squareup.javapoet.MethodSpec;
import com.squareup.javapoet.TypeSpec;
import java.io.IOException;
import java.util.HashSet;
import java.util.Set;
import javax.annotation.processing.AbstractProcessor;
import javax.annotation.processing.ProcessingEnvironment;
import javax.annotation.processing.RoundEnvironment;
import javax.lang.model.SourceVersion;
import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.Element;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.Modifier;
import javax.lang.model.element.TypeElement;
import javax.lang.model.element.VariableElement;
import javax.lang.model.util.ElementFilter;
import javax.tools.Diagnostic;
public class Processor extends AbstractProcessor {
private ProcessingEnvironment mProcessingEnvironment;
@Override
public synchronized void init(ProcessingEnvironment processingEnvironment) {
super.init(processingEnvironment);
mProcessingEnvironment = processingEnvironment;
}
@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnvironment) {
for (TypeElement typeElement : getTypeElementsToProcess(roundEnvironment.getRootElements(), annotations)) {
String packageName = mProcessingEnvironment.getElementUtils().getPackageOf(typeElement).getQualifiedName().toString();
String typeName = typeElement.getSimpleName().toString();
ClassName className = ClassName.get(packageName, typeName);
ClassName generatedClassName = ClassName
.get(packageName, typeName + "Binder");
// define the wrapper class
TypeSpec.Builder classBuilder = TypeSpec.classBuilder(generatedClassName)
.addModifiers(Modifier.PUBLIC);
//.addAnnotation(Keep.class);
// add constructor
classBuilder.addMethod(MethodSpec.constructorBuilder()
.addModifiers(Modifier.PUBLIC)
.addParameter(className, "activity")
.addStatement("$N($N)",
"bindViews",
"activity")
.addStatement("$N($N)",
"bindOnClicks",
"activity")
.build());
// add method that maps the views with id
MethodSpec.Builder bindViewsMethodBuilder = MethodSpec
.methodBuilder("bindViews")
.addModifiers(Modifier.PRIVATE)
.returns(void.class)
.addParameter(className, "activity");
for (VariableElement variableElement : ElementFilter.fieldsIn(typeElement.getEnclosedElements())) {
BindView bindView = variableElement.getAnnotation(BindView.class);
if (bindView != null) {
bindViewsMethodBuilder.addStatement("$N.$N = ($T) $N.findViewById($L)",
"activity",
variableElement.getSimpleName(),
variableElement,
"activity",
bindView.value());
}
}
classBuilder.addMethod(bindViewsMethodBuilder.build());
// add method that attaches the onClickListeners
ClassName androidOnClickListenerClassName = ClassName.get(
"android.view",
"View",
"OnClickListener");
ClassName androidViewClassName = ClassName.get(
"android.view",
"View");
MethodSpec.Builder bindOnClicksMethodBuilder = MethodSpec
.methodBuilder("bindOnClicks")
.addModifiers(Modifier.PRIVATE)
.returns(void.class)
.addParameter(className, "activity", Modifier.FINAL);
for (ExecutableElement executableElement : ElementFilter.methodsIn(typeElement.getEnclosedElements())) {
OnClick onClick = executableElement.getAnnotation(OnClick.class);
if (onClick != null) {
TypeSpec OnClickListenerClass = TypeSpec.anonymousClassBuilder("")
.addSuperinterface(androidOnClickListenerClassName)
.addMethod(MethodSpec.methodBuilder("onClick")
.addModifiers(Modifier.PUBLIC)
.addParameter(androidViewClassName, "view")
.addStatement("$N.$N($N)",
"activity",
executableElement.getSimpleName(),
"view")
.returns(void.class)
.build())
.build();
bindOnClicksMethodBuilder.addStatement("$N.findViewById($L).setOnClickListener($L)",
"activity",
onClick.value(),
OnClickListenerClass);
}
}
classBuilder.addMethod(bindOnClicksMethodBuilder.build());
// write the defines class to a java file
try {
JavaFile.builder(packageName,
classBuilder.build())
.build()
.writeTo(mProcessingEnvironment.getFiler());
} catch (IOException e) {
mProcessingEnvironment.getMessager().printMessage(Diagnostic.Kind.ERROR, e.toString(), typeElement);
}
}
return true;
}
private Set<TypeElement> getTypeElementsToProcess(Set<? extends Element> elements,
Set<? extends TypeElement> supportedAnnotations) {
Set<TypeElement> typeElements = new HashSet<>();
for (Element element : elements) {
if (element instanceof TypeElement) {
boolean found = false;
for (Element subElement : element.getEnclosedElements()) {
for (AnnotationMirror mirror : subElement.getAnnotationMirrors()) {
for (Element annotation : supportedAnnotations) {
if (mirror.getAnnotationType().asElement().equals(annotation)) {
typeElements.add((TypeElement) element);
found = true;
break;
}
}
if (found) break;
}
if (found) break;
}
}
}
return typeElements;
}
@Override
public Set<String> getSupportedAnnotationTypes() {
return new HashSet<String>() {{
add(BindView.class.getCanonicalName());
add(OnClick.class.getCanonicalName());
}};
}
@Override
public SourceVersion getSupportedSourceVersion() {
return SourceVersion.latestSupported();
}
}