fix: attempting to remove from list while iterating on it

This commit is contained in:
zaaarf 2023-03-25 18:44:58 +01:00
parent e2c4ccdce3
commit a51cbceebc
No known key found for this signature in database
GPG key ID: 82240E075E31FA4C

View file

@ -152,7 +152,7 @@ public class LilleroProcessor extends AbstractProcessor {
TypeMirror classNodeType = processingEnv.getElementUtils().getTypeElement("org.objectweb.asm.tree.ClassNode").asType(); TypeMirror classNodeType = processingEnv.getElementUtils().getTypeElement("org.objectweb.asm.tree.ClassNode").asType();
TypeMirror methodNodeType = processingEnv.getElementUtils().getTypeElement("org.objectweb.asm.tree.MethodNode").asType(); TypeMirror methodNodeType = processingEnv.getElementUtils().getTypeElement("org.objectweb.asm.tree.MethodNode").asType();
if (elem.getEnclosedElements().stream().anyMatch(e -> e.getAnnotation(Target.class) != null) if (elem.getEnclosedElements().stream().anyMatch(e -> e.getAnnotation(Target.class) != null)
&& elem.getEnclosedElements().stream().anyMatch(e -> { && elem.getEnclosedElements().stream().filter(e -> e instanceof ExecutableElement).anyMatch(e -> {
List<? extends TypeMirror> params = ((ExecutableType) e.asType()).getParameterTypes(); List<? extends TypeMirror> params = ((ExecutableType) e.asType()).getParameterTypes();
return e.getAnnotation(Injector.class) != null return e.getAnnotation(Injector.class) != null
&& e.getAnnotation(Target.class) == null && e.getAnnotation(Target.class) == null
@ -197,11 +197,16 @@ public class LilleroProcessor extends AbstractProcessor {
//initialize the constructor builder //initialize the constructor builder
MethodSpec.Builder constructorBuilder = MethodSpec.constructorBuilder(); MethodSpec.Builder constructorBuilder = MethodSpec.constructorBuilder();
List<VariableElement> methodFinders = new ArrayList<>();
//take care of TypeProxies and FieldProxies first //take care of TypeProxies and FieldProxies first
for(VariableElement proxyVar : finders) { for(VariableElement proxyVar : finders) {
ProxyType type = getProxyType(proxyVar); ProxyType type = getProxyType(proxyVar);
if(type == ProxyType.METHOD && !proxyVar.getAnnotation(Find.class).name().equals("")) //methods will be handled later if(type == ProxyType.METHOD && !proxyVar.getAnnotation(Find.class).name().equals("")) {
//methods without a specified name will be handled later
methodFinders.add(proxyVar);
continue; continue;
}
//case-specific handling //case-specific handling
if(type == ProxyType.TYPE) { if(type == ProxyType.TYPE) {
//find and validate //find and validate
@ -216,7 +221,6 @@ public class LilleroProcessor extends AbstractProcessor {
); );
} else if(type == ProxyType.FIELD || type == ProxyType.METHOD) } else if(type == ProxyType.FIELD || type == ProxyType.METHOD)
appendMemberFinderDefinition(targetClass, proxyVar, null, constructorBuilder, this.processingEnv, this.mapper); appendMemberFinderDefinition(targetClass, proxyVar, null, constructorBuilder, this.processingEnv, this.mapper);
finders.remove(proxyVar); //remove finders that have already been processed
} }
//declare it once for efficiency //declare it once for efficiency
@ -227,7 +231,7 @@ public class LilleroProcessor extends AbstractProcessor {
.collect(Collectors.toList()); .collect(Collectors.toList());
//targets that have matched at least once are put in here //targets that have matched at least once are put in here
Set<ExecutableElement> matchedTargets; Set<ExecutableElement> matchedTargets = new HashSet<>();
//this will contain the classes to generate: the key is the class name //this will contain the classes to generate: the key is the class name
HashMap<String, InjectorInfo> toGenerate = new HashMap<>(); HashMap<String, InjectorInfo> toGenerate = new HashMap<>();
@ -237,7 +241,7 @@ public class LilleroProcessor extends AbstractProcessor {
int iterationNumber = 1; int iterationNumber = 1;
for(Target targetAnn : mtgAnn) { for(Target targetAnn : mtgAnn) {
List<ExecutableElement> injectorCandidates = injectors; List<ExecutableElement> injectorCandidates = injectors;
List<VariableElement> finderCandidates = finders; List<VariableElement> finderCandidates = methodFinders;
if(!targetAnn.of().equals("") && injectorNames.contains(targetAnn.of())) { if(!targetAnn.of().equals("") && injectorNames.contains(targetAnn.of())) {
//case 1: find target by name //case 1: find target by name
@ -290,10 +294,14 @@ public class LilleroProcessor extends AbstractProcessor {
iterationNumber++; //increment is only used by injectors iterationNumber++; //increment is only used by injectors
} else { } else {
//matched a finder! //matched a finder!
VariableElement finder = finders.get(0); appendMemberFinderDefinition(
Find f = finder.getAnnotation(Find.class); targetClass,
appendMemberFinderDefinition(targetClass, finder, tg, constructorBuilder, this.processingEnv, this.mapper); finderCandidates.get(0),
finders.remove(finder); //unlike injectors, finders can't apply to multiple targets tg,
constructorBuilder,
this.processingEnv,
this.mapper
);
} }
} }
} }