mirror of
https://github.com/zaaarf/geb-processor.git
synced 2024-11-14 05:29:21 +01:00
feat: update to new API, slight generated code improvements
This commit is contained in:
parent
1cac528f23
commit
ee55975f5d
1 changed files with 50 additions and 23 deletions
|
@ -153,9 +153,8 @@ public class GEBProcessor extends AbstractProcessor {
|
||||||
listener.getEnclosingElement().getSimpleName().toString(),
|
listener.getEnclosingElement().getSimpleName().toString(),
|
||||||
listener.getSimpleName().toString()));
|
listener.getSimpleName().toString()));
|
||||||
|
|
||||||
if(!this.listenerMap.containsKey(event))
|
this.listenerMap.computeIfAbsent(event, k -> new HashSet<>())
|
||||||
this.listenerMap.put(event, new HashSet<>());
|
.add(new ListenerContainer(listener));
|
||||||
this.listenerMap.get(event).add(new ListenerContainer(listener));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -166,38 +165,66 @@ public class GEBProcessor extends AbstractProcessor {
|
||||||
TypeElement eventClass = (TypeElement) this.processingEnv.getTypeUtils().asElement(event);
|
TypeElement eventClass = (TypeElement) this.processingEnv.getTypeUtils().asElement(event);
|
||||||
boolean cancelable = this.processingEnv.getTypeUtils().isAssignable(event, this.cancelableEventInterface);
|
boolean cancelable = this.processingEnv.getTypeUtils().isAssignable(event, this.cancelableEventInterface);
|
||||||
|
|
||||||
//reorder the injectors to follow priority
|
|
||||||
List<ListenerContainer> ordered = listeners.stream().sorted(Comparator.comparingInt(
|
|
||||||
container -> container.annotation.priority()
|
|
||||||
)).collect(Collectors.toList());
|
|
||||||
|
|
||||||
ParameterSpec eventParam = ParameterSpec.builder(TypeName.get(this.eventInterface), "event").build();
|
ParameterSpec eventParam = ParameterSpec.builder(TypeName.get(this.eventInterface), "event").build();
|
||||||
ParameterSpec listenersParam = ParameterSpec.builder(ParameterizedTypeName.get(
|
ParameterSpec listenersParam = ParameterSpec.builder(
|
||||||
ClassName.get("java.util", "Map"), ParameterizedTypeName.get(
|
ParameterizedTypeName.get(
|
||||||
ClassName.get("java.lang", "Class"),
|
ClassName.get("java.util", "Map"),
|
||||||
WildcardTypeName.subtypeOf(TypeName.get(this.listenerInterface))),
|
ParameterizedTypeName.get(
|
||||||
ClassName.get(this.listenerInterface)), "listeners")
|
ClassName.get("java.lang", "Class"),
|
||||||
.build();
|
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")
|
MethodSpec.Builder callListenersBuilder = MethodSpec.methodBuilder("callListeners")
|
||||||
.addModifiers(Modifier.PUBLIC)
|
.addModifiers(Modifier.PUBLIC)
|
||||||
.addAnnotation(Override.class)
|
.addAnnotation(Override.class)
|
||||||
.addAnnotation(AnnotationSpec.builder(SuppressWarnings.class) //because why not
|
.addAnnotation(AnnotationSpec.builder(SuppressWarnings.class) // because why not
|
||||||
.addMember("value" , "{$S}", "unchecked").build())
|
.addMember("value" , "{$S}", "unchecked").build())
|
||||||
.addParameter(eventParam)
|
.addParameter(eventParam)
|
||||||
.addParameter(listenersParam)
|
.addParameter(listenersParam)
|
||||||
.returns(boolean.class);
|
.returns(boolean.class);
|
||||||
|
|
||||||
int counter = 0;
|
// 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) {
|
for(ListenerContainer listener : ordered) {
|
||||||
String varName = String.format("listener%d", counter);
|
String varName = String.format("listener%d", done.get(listener.parent));
|
||||||
callListenersBuilder
|
callListenersBuilder
|
||||||
.addStatement("$T $L = $N.get($T.class)", this.listenerInterface, varName, listenersParam, listener.parent)
|
.addStatement("for($T l : $L) {", this.listenerInterface, varName)
|
||||||
.addStatement("if($L.isActive()) (($T) $L).$L(($T) $N)", varName, listener.parent, varName,
|
.addStatement(
|
||||||
listener.method.getSimpleName().toString(), event, eventParam);
|
"if(l != null) (($T) l).$L(($T) $N); }",
|
||||||
|
listener.parent,
|
||||||
|
listener.method.getSimpleName().toString(),
|
||||||
|
event,
|
||||||
|
eventParam
|
||||||
|
);
|
||||||
if(cancelable) callListenersBuilder
|
if(cancelable) callListenersBuilder
|
||||||
.addStatement("if((($T) $N).isCanceled()) return true", this.cancelableEventInterface, eventParam);
|
.addStatement("if((($T) $N).isCanceled()) return false", this.cancelableEventInterface, eventParam);
|
||||||
counter++;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
callListenersBuilder.addStatement("return false");
|
callListenersBuilder.addStatement("return false");
|
||||||
|
@ -250,7 +277,7 @@ public class GEBProcessor extends AbstractProcessor {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A container class to carry information about a listener method.
|
* A container class to carry information about a listener class.
|
||||||
*/
|
*/
|
||||||
private static class ListenerContainer {
|
private static class ListenerContainer {
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in a new issue