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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
|
package ftbsc.geb.processor;
import com.squareup.javapoet.*;
import ftbsc.geb.api.IEvent;
import ftbsc.geb.api.IEventCancelable;
import ftbsc.geb.api.IEventDispatcher;
import ftbsc.geb.api.IListener;
import ftbsc.geb.api.annotations.Listen;
import ftbsc.geb.exceptions.BadListenerArgumentsException;
import ftbsc.geb.exceptions.MissingInterfaceException;
import javax.annotation.processing.*;
import javax.lang.model.SourceVersion;
import javax.lang.model.element.*;
import javax.lang.model.type.TypeKind;
import javax.lang.model.type.TypeMirror;
import javax.tools.Diagnostic;
import javax.tools.FileObject;
import javax.tools.JavaFileObject;
import javax.tools.StandardLocation;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.*;
import java.util.stream.Collectors;
/**
* GEB's {@link javax.annotation.processing.Processor annotation processor},
* which takes care of generating the {@link IEventDispatcher dispatchers}.
*/
@SupportedAnnotationTypes("ftbsc.geb.api.annotations.Listen")
public class GEBProcessor extends AbstractProcessor {
/**
* A {@link Map} tying each event class to a {@link Set} of listeners.
*/
private final Map<TypeMirror, Set<ListenerContainer>> listenerMap = new HashMap<>();
/**
* A {@link Set} containing the fully-qualified names of the generated classes.
*/
private final Set<String> generatedClasses = new HashSet<>();
/**
* A {@link TypeMirror} representing the {@link IListener} interface.
*/
private TypeMirror listenerInterface;
/**
* A {@link TypeMirror} representing the {@link IEvent} interface.
*/
private TypeMirror eventInterface;
/**
* A {@link TypeMirror} representing the {@link IEventCancelable} interface.
*/
private TypeMirror cancelableEventInterface;
/**
* A {@link TypeMirror} representing the {@link IEventDispatcher} interface.
*/
private TypeMirror dispatcherInterface;
/**
* Initializes the processor with the given environment.
* Also takes carae of initializing the TypeMirror "constants" for later use.
* @param env the environment
*/
@Override
public synchronized void init(ProcessingEnvironment env) {
super.init(env);
this.listenerInterface = env.getElementUtils()
.getTypeElement("ftbsc.geb.api.IListener").asType();
this.eventInterface = env.getElementUtils()
.getTypeElement("ftbsc.geb.api.IEvent").asType();
this.dispatcherInterface = env.getElementUtils()
.getTypeElement("ftbsc.geb.api.IEventDispatcher").asType();
this.cancelableEventInterface = env.getElementUtils()
.getTypeElement("ftbsc.geb.api.IEventCancelable").asType();
}
/**
* The starting point of the processor.
* It calls {@link #processListener(Element)} on all elements annotated with
* the {@link Listen} annotation.
* @param annotations the annotation types requested to be processed
* @param env environment for information about the current and prior round
* @return whether the set of annotation types are claimed by this processor
*/
@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment env) {
boolean claimed = false;
for(TypeElement ann : annotations) {
if(ann.getQualifiedName().contentEquals(Listen.class.getName())) {
claimed = true;
for(Element e : env.getElementsAnnotatedWith(ann))
this.processListener(e);
if(!this.listenerMap.isEmpty()) {
this.generateClasses();
this.generateServiceProvider();
}
}
}
return claimed;
}
/**
* Sets the supported source version to the latest one.
* It's either that or constant warnings, and the processor is simple enough.
* @return the latest source version
*/
@Override
public SourceVersion getSupportedSourceVersion() {
return SourceVersion.latest();
}
/**
* Verifies that the annotated method is valid and, if it is, adds it to
* the list. See the annotation's javadoc for details on what's considered
* a valid listener.
* @see Listen
* @param target the {@link Element} that was annotated with {@link Listen}
*/
private void processListener(Element target) {
ExecutableElement listener = (ExecutableElement) target; //this cast will never fail
//ensure the parent is instance of IListener
TypeMirror parentType = listener.getEnclosingElement().asType();
if(!this.processingEnv.getTypeUtils().isAssignable(parentType, this.listenerInterface))
throw new MissingInterfaceException(
listener.getEnclosingElement().getSimpleName().toString(),
listener.getSimpleName().toString());
//ensure the listener method has only one parameter
List<? extends VariableElement> params = listener.getParameters();
if(listener.getParameters().size() != 1)
throw new BadListenerArgumentsException.Count(
listener.getEnclosingElement().getSimpleName().toString(),
listener.getSimpleName().toString(),
params.size());
//ensure said parameter implements IEvent
TypeMirror event = params.get(0).asType();
if(!this.processingEnv.getTypeUtils().isAssignable(event, this.eventInterface))
throw new BadListenerArgumentsException.Type(
listener.getEnclosingElement().getSimpleName().toString(),
listener.getSimpleName().toString(),
params.get(0).getSimpleName().toString());
//warn about return type
if(!listener.getReturnType().getKind().equals(TypeKind.VOID))
this.processingEnv.getMessager().printMessage(Diagnostic.Kind.WARNING, String.format(
"The method %s::%s has a return type: please note that it will be ignored.",
listener.getEnclosingElement().getSimpleName().toString(),
listener.getSimpleName().toString()));
this.listenerMap.computeIfAbsent(event, k -> new HashSet<>())
.add(new ListenerContainer(listener));
}
/**
* Uses JavaPoet to generate the classes dispatcher classes.
*/
private void generateClasses() {
this.listenerMap.forEach((event, listeners) -> {
TypeElement eventClass = (TypeElement) this.processingEnv.getTypeUtils().asElement(event);
boolean cancelable = this.processingEnv.getTypeUtils().isAssignable(event, this.cancelableEventInterface);
ParameterSpec eventParam = ParameterSpec.builder(TypeName.get(this.eventInterface), "event").build();
ParameterSpec listenersParam = ParameterSpec.builder(
ParameterizedTypeName.get(
ClassName.get("java.util", "Map"),
ParameterizedTypeName.get(
ClassName.get("java.lang", "Class"),
WildcardTypeName.subtypeOf(TypeName.get(this.listenerInterface))
),
ParameterizedTypeName.get(
ClassName.get("java.util", "Set"),
ClassName.get(this.listenerInterface)
)
),
"listeners"
).build();
MethodSpec.Builder callListenersBuilder = MethodSpec.methodBuilder("callListeners")
.addModifiers(Modifier.PUBLIC)
.addAnnotation(Override.class)
.addAnnotation(AnnotationSpec.builder(SuppressWarnings.class) // because why not
.addMember("value" , "{$S}", "unchecked").build())
.addParameter(eventParam)
.addParameter(listenersParam)
.returns(boolean.class);
// reorder the injectors to follow priority
Map<TypeMirror, Integer> done = new HashMap<>();
List<ListenerContainer> ordered = listeners.stream().sorted(Comparator.comparingInt(
container -> container.annotation.priority()
)).collect(Collectors.toList());
// get all the relevant injectors
for(int i = 0; i < ordered.size(); i++) {
ListenerContainer listener = ordered.get(i);
if(!done.containsKey(listener.parent)) {
done.put(listener.parent, i);
String varName = String.format("listener%d", i);
callListenersBuilder.addStatement(
"java.util.Set<$T> $L = $N.get($T.class)",
this.listenerInterface,
varName,
listenersParam,
listener.parent
);
}
}
for(ListenerContainer listener : ordered) {
String varName = String.format("listener%d", done.get(listener.parent));
callListenersBuilder
.addStatement("if($L != null) { for($T l : $L) {", varName, this.listenerInterface, varName)
.addStatement(
"if(l != null) (($T) l).$L(($T) $N); } }",
listener.parent,
listener.method.getSimpleName().toString(),
event,
eventParam
);
if(cancelable) callListenersBuilder
.addStatement("if((($T) $N).isCanceled()) return false", this.cancelableEventInterface, eventParam);
}
callListenersBuilder.addStatement("return true");
MethodSpec eventType = MethodSpec.methodBuilder("eventType")
.addModifiers(Modifier.PUBLIC)
.addAnnotation(Override.class)
.returns(Class.class)
.addStatement("return $T.class", event)
.build();
String clazzName = String.format("%sDispatcher", eventClass.getSimpleName());
TypeSpec clazz = TypeSpec.classBuilder(clazzName)
.addModifiers(Modifier.PUBLIC)
.addSuperinterface(this.dispatcherInterface)
.addMethod(callListenersBuilder.build())
.addMethod(eventType)
.build();
String packageName = "ftbsc.geb.generated";
JavaFile javaFile = JavaFile.builder(packageName, clazz).build();
String resultingClassName = String.format("%s.%s", packageName, clazzName);
try {
JavaFileObject injectorFile = this.processingEnv.getFiler().createSourceFile(resultingClassName);
PrintWriter out = new PrintWriter(injectorFile.openWriter());
javaFile.writeTo(out);
out.close();
} catch(IOException e) {
throw new RuntimeException(e);
}
this.generatedClasses.add(resultingClassName);
});
}
/**
* Generates the Service Provider file for the dispatchers.
*/
private void generateServiceProvider() {
try {
FileObject serviceProvider = processingEnv.getFiler().createResource(StandardLocation.CLASS_OUTPUT, "",
"META-INF/services/ftbsc.geb.api.IEventDispatcher");
PrintWriter out = new PrintWriter(serviceProvider.openWriter());
this.generatedClasses.forEach(out::println);
out.close();
} catch(IOException e) {
throw new RuntimeException(e);
}
}
/**
* A container class to carry information about a listener class.
*/
private static class ListenerContainer {
/**
* The actual listener, the annotated method.
*/
public final ExecutableElement method;
/**
* The parent which implements {@link IListener}.
*/
public final TypeMirror parent;
/**
* The {@link Listen} annotation on the method.
*/
public final Listen annotation;
/**
* The public constructor.
* @param method the annotated method, assumed to be valid
* and already checked
*/
public ListenerContainer(ExecutableElement method) {
this.method = method;
this.parent = method.getEnclosingElement().asType();
this.annotation = method.getAnnotation(Listen.class);
}
}
}
|