Compare commits

..

No commits in common. "d6c1976261a7d2b17f910dd6b76804f7401a1858" and "1cac528f2336dbcfdc5e2e656c8a64bc04e63795" have entirely different histories.

2 changed files with 25 additions and 52 deletions

View file

@ -4,7 +4,7 @@ plugins {
} }
archivesBaseName = 'processor' archivesBaseName = 'processor'
version = versionDetails().lastTag version = gitVersion().split('-').getAt(0).replace('dirty', '')
java { java {
sourceCompatibility = targetCompatibility = JavaVersion.VERSION_1_8 sourceCompatibility = targetCompatibility = JavaVersion.VERSION_1_8
@ -25,6 +25,6 @@ repositories {
} }
dependencies { dependencies {
implementation 'ftbsc:geb:0.3.0' implementation 'ftbsc:geb:0.2.0'
implementation 'com.squareup:javapoet:1.13.0' implementation 'com.squareup:javapoet:1.13.0'
} }

View file

@ -153,8 +153,9 @@ public class GEBProcessor extends AbstractProcessor {
listener.getEnclosingElement().getSimpleName().toString(), listener.getEnclosingElement().getSimpleName().toString(),
listener.getSimpleName().toString())); listener.getSimpleName().toString()));
this.listenerMap.computeIfAbsent(event, k -> new HashSet<>()) if(!this.listenerMap.containsKey(event))
.add(new ListenerContainer(listener)); this.listenerMap.put(event, new HashSet<>());
this.listenerMap.get(event).add(new ListenerContainer(listener));
} }
/** /**
@ -165,21 +166,18 @@ 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( ParameterSpec listenersParam = ParameterSpec.builder(ParameterizedTypeName.get(
ParameterizedTypeName.get( ClassName.get("java.util", "Map"), ParameterizedTypeName.get(
ClassName.get("java.util", "Map"),
ParameterizedTypeName.get(
ClassName.get("java.lang", "Class"), ClassName.get("java.lang", "Class"),
WildcardTypeName.subtypeOf(TypeName.get(this.listenerInterface)) WildcardTypeName.subtypeOf(TypeName.get(this.listenerInterface))),
), ClassName.get(this.listenerInterface)), "listeners")
ParameterizedTypeName.get( .build();
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)
@ -190,41 +188,16 @@ public class GEBProcessor extends AbstractProcessor {
.addParameter(listenersParam) .addParameter(listenersParam)
.returns(boolean.class); .returns(boolean.class);
// reorder the injectors to follow priority int counter = 0;
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", done.get(listener.parent)); String varName = String.format("listener%d", counter);
callListenersBuilder callListenersBuilder
.addStatement("for($T l : $L) {", this.listenerInterface, varName) .addStatement("$T $L = $N.get($T.class)", this.listenerInterface, varName, listenersParam, listener.parent)
.addStatement( .addStatement("if($L.isActive()) (($T) $L).$L(($T) $N)", varName, listener.parent, varName,
"if(l != null) (($T) l).$L(($T) $N); }", listener.method.getSimpleName().toString(), event, eventParam);
listener.parent,
listener.method.getSimpleName().toString(),
event,
eventParam
);
if(cancelable) callListenersBuilder if(cancelable) callListenersBuilder
.addStatement("if((($T) $N).isCanceled()) return false", this.cancelableEventInterface, eventParam); .addStatement("if((($T) $N).isCanceled()) return true", this.cancelableEventInterface, eventParam);
counter++;
} }
callListenersBuilder.addStatement("return false"); callListenersBuilder.addStatement("return false");
@ -277,7 +250,7 @@ public class GEBProcessor extends AbstractProcessor {
} }
/** /**
* A container class to carry information about a listener class. * A container class to carry information about a listener method.
*/ */
private static class ListenerContainer { private static class ListenerContainer {
/** /**