feat: allow to register manually written IInjectors

This commit is contained in:
zaaarf 2023-04-12 14:04:25 +02:00
parent abd9954ed3
commit 3344fd766f
No known key found for this signature in database
GPG key ID: 82240E075E31FA4C
2 changed files with 31 additions and 12 deletions

View file

@ -5,10 +5,7 @@ import ftbsc.lll.IInjector;
import ftbsc.lll.exceptions.AmbiguousDefinitionException;
import ftbsc.lll.exceptions.InvalidResourceException;
import ftbsc.lll.exceptions.OrphanElementException;
import ftbsc.lll.processor.annotations.Find;
import ftbsc.lll.processor.annotations.Injector;
import ftbsc.lll.processor.annotations.Patch;
import ftbsc.lll.processor.annotations.Target;
import ftbsc.lll.processor.annotations.*;
import ftbsc.lll.processor.tools.containers.ClassContainer;
import ftbsc.lll.processor.tools.containers.MethodContainer;
import ftbsc.lll.processor.tools.obfuscation.ObfuscationMapper;
@ -38,7 +35,7 @@ import static ftbsc.lll.processor.tools.JavaPoetUtils.*;
* The actual annotation processor behind the magic.
* It (implicitly) implements the {@link Processor} interface by extending {@link AbstractProcessor}.
*/
@SupportedAnnotationTypes("ftbsc.lll.processor.annotations.Patch")
@SupportedAnnotationTypes({"ftbsc.lll.processor.annotations.Patch", "ftbsc.lll.processor.annotations.RegisterBareInjector"})
@SupportedSourceVersion(SourceVersion.RELEASE_8)
@SupportedOptions({"mappingsFile", "anonymousClassWarning", "obfuscateInjectorMetadata"})
public class LilleroProcessor extends AbstractProcessor {
@ -144,15 +141,19 @@ public class LilleroProcessor extends AbstractProcessor {
.map(e -> (TypeElement) e)
.filter(this::isValidInjector)
.collect(Collectors.toSet());
if(!validInjectors.isEmpty()) {
if(!validInjectors.isEmpty())
validInjectors.forEach(this::generateClasses);
if (!this.generatedInjectors.isEmpty()) {
generateServiceProvider();
return true;
}
}
} else if(annotation.getQualifiedName().contentEquals(RegisterBareInjector.class.getName())) {
for(Element e : roundEnv.getElementsAnnotatedWith(annotation))
this.generatedInjectors.add(((TypeElement) e).getQualifiedName().toString());
}
}
if (!this.generatedInjectors.isEmpty()) {
generateServiceProvider();
return true;
}
return false;
}
@ -437,4 +438,4 @@ public class LilleroProcessor extends AbstractProcessor {
this.target = MethodContainer.from(targetStub, targetAnn, null, processingEnv, mapper);
}
}
}
}

View file

@ -0,0 +1,18 @@
package ftbsc.lll.processor.annotations;
import ftbsc.lll.IInjector;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
/**
* Marks the class in question as a class to be added to the service provider file
* (META-INF/services/ftbsc.lll.IInjector) without actually processing it. This can
* be used to mix in a same project regular {@link IInjector}s and those generated
* by the processor.
* @since 0.6.0
*/
@Retention(RetentionPolicy.CLASS)
@java.lang.annotation.Target(ElementType.TYPE)
public @interface RegisterBareInjector {}