chore: code quality

This commit is contained in:
zaaarf 2023-02-27 03:41:24 +01:00
parent 252272ac42
commit 4fcba147cb
No known key found for this signature in database
GPG key ID: AD8563472FD43386

View file

@ -58,28 +58,21 @@ public class LilleroProcessor extends AbstractProcessor {
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
for (TypeElement annotation : annotations) { for (TypeElement annotation : annotations) {
if(annotation.getQualifiedName().toString().equals(Patch.class.getName())) { if(annotation.getQualifiedName().toString().equals(Patch.class.getName())) {
Set<TypeElement> validInjectors = new HashSet<>(); Set<TypeElement> validInjectors =
Set<TypeElement> potentialInjectors =
roundEnv.getElementsAnnotatedWith(annotation) roundEnv.getElementsAnnotatedWith(annotation)
.stream() .stream()
.map(e -> (TypeElement) e) .map(e -> (TypeElement) e)
.filter(this::isValidInjector)
.collect(Collectors.toSet()); .collect(Collectors.toSet());
for(TypeElement p : potentialInjectors) if(!validInjectors.isEmpty()) {
if(isValidInjector(p))
validInjectors.add(p);
else processingEnv.getMessager().printMessage(
Diagnostic.Kind.WARNING,
"Missing valid @Injector method in @Patch class " + p + ", skipping."
);
if(validInjectors.isEmpty())
return false;
validInjectors.forEach(this::generateInjector); validInjectors.forEach(this::generateInjector);
if(this.generatedInjectors.isEmpty()) if (!this.generatedInjectors.isEmpty()) {
return false;
generateServiceProvider(); generateServiceProvider();
return true; return true;
} }
} }
}
}
return false; return false;
} }
@ -93,7 +86,7 @@ public class LilleroProcessor extends AbstractProcessor {
private boolean isValidInjector(TypeElement elem) { private boolean isValidInjector(TypeElement elem) {
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();
return 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().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
@ -103,7 +96,12 @@ public class LilleroProcessor extends AbstractProcessor {
&& params.size() == 2 && params.size() == 2
&& processingEnv.getTypeUtils().isSameType(params.get(0), classNodeType) && processingEnv.getTypeUtils().isSameType(params.get(0), classNodeType)
&& processingEnv.getTypeUtils().isSameType(params.get(1), methodNodeType); && processingEnv.getTypeUtils().isSameType(params.get(1), methodNodeType);
}); })) return true;
else {
processingEnv.getMessager().printMessage(Diagnostic.Kind.WARNING,
"Missing valid @Injector method in @Patch class " + elem + ", skipping.");
return false;
}
} }
/** /**
@ -124,6 +122,14 @@ public class LilleroProcessor extends AbstractProcessor {
.get(); //will never be null so can ignore warning .get(); //will never be null so can ignore warning
} }
private static MethodSpec buildStringReturnMethod(String name, String returnString) {
return MethodSpec.methodBuilder(name)
.addModifiers(Modifier.PUBLIC)
.returns(String.class)
.addStatement("return $S", returnString)
.build();
}
/** /**
* Builds a type descriptor from the given {@link TypeMirror} * Builds a type descriptor from the given {@link TypeMirror}
* @param t the {@link TypeMirror} representing the desired type * @param t the {@link TypeMirror} representing the desired type
@ -183,11 +189,7 @@ public class LilleroProcessor extends AbstractProcessor {
* @param cl the {@link TypeElement} for the given class * @param cl the {@link TypeElement} for the given class
*/ */
private void generateInjector(TypeElement cl) { private void generateInjector(TypeElement cl) {
Patch ann = cl.getAnnotation(Patch.class);
ExecutableElement targetMethod = findAnnotatedMethod(cl, Target.class);
ExecutableElement injectorMethod = findAnnotatedMethod(cl, Injector.class);
SrgMapper mapper; SrgMapper mapper;
try { //TODO: cant we get it from local? try { //TODO: cant we get it from local?
URL url = new URL("https://data.fantabos.co/output.tsrg"); URL url = new URL("https://data.fantabos.co/output.tsrg");
InputStream is = url.openStream(); InputStream is = url.openStream();
@ -198,49 +200,18 @@ public class LilleroProcessor extends AbstractProcessor {
throw new MappingsFileNotFoundException(); throw new MappingsFileNotFoundException();
} }
Element packageElement = cl.getEnclosingElement(); Patch ann = cl.getAnnotation(Patch.class);
while (packageElement.getKind() != ElementKind.PACKAGE)
packageElement = packageElement.getEnclosingElement();
String packageName = packageElement.toString();
String simpleClassName = cl.getSimpleName().toString();
String injectorSimpleClassName = simpleClassName + "Injector";
String injectorClassName = packageName + "." + injectorSimpleClassName;
MethodSpec name = MethodSpec.methodBuilder("name")
.addModifiers(Modifier.PUBLIC)
.returns(String.class)
.addStatement("return $S", injectorSimpleClassName)
.build();
MethodSpec reason = MethodSpec.methodBuilder("reason")
.addModifiers(Modifier.PUBLIC)
.returns(String.class)
.addStatement("return $S", ann.reason())
.build();
String targetClassCanonicalName; String targetClassCanonicalName;
try { try {
targetClassCanonicalName = ann.value().getCanonicalName(); targetClassCanonicalName = ann.value().getCanonicalName();
} catch(MirroredTypeException e) { } catch(MirroredTypeException e) {
targetClassCanonicalName = e.getTypeMirror().toString(); targetClassCanonicalName = e.getTypeMirror().toString();
} } //pretty sure class names de facto never change but better safe than sorry
String targetClassSrgName = mapper.getMcpClass(targetClassCanonicalName.replace('.', '/'));
//pretty sure class names de facto never change but better safe than sorry
String targetClassSrgName = mapper.getMcpClass(
targetClassCanonicalName.replace('.', '/')
);
if(targetClassSrgName == null) if(targetClassSrgName == null)
throw new MappingNotFoundException(targetClassCanonicalName); throw new MappingNotFoundException(targetClassCanonicalName);
MethodSpec targetClass = MethodSpec.methodBuilder("targetClass") ExecutableElement targetMethod = findAnnotatedMethod(cl, Target.class);
.addModifiers(Modifier.PUBLIC)
.returns(String.class)
.addStatement("return $S", targetClassSrgName.replace('/', '.'))
.build();
String targetMethodDescriptor = descriptorFromMethodSpec(targetMethod); String targetMethodDescriptor = descriptorFromMethodSpec(targetMethod);
String targetMethodSrgName = mapper.getSrgMember( String targetMethodSrgName = mapper.getSrgMember(
targetClassCanonicalName.replace('.', '/'), targetClassCanonicalName.replace('.', '/'),
@ -250,11 +221,15 @@ public class LilleroProcessor extends AbstractProcessor {
if(targetMethodSrgName == null) if(targetMethodSrgName == null)
throw new MappingNotFoundException(targetMethod.getSimpleName() + " " + targetMethodDescriptor); throw new MappingNotFoundException(targetMethod.getSimpleName() + " " + targetMethodDescriptor);
MethodSpec methodName = MethodSpec.methodBuilder("methodName") ExecutableElement injectorMethod = findAnnotatedMethod(cl, Injector.class);
.addModifiers(Modifier.PUBLIC)
.returns(String.class) Element packageElement = cl.getEnclosingElement();
.addStatement("return $S", targetMethodSrgName) while (packageElement.getKind() != ElementKind.PACKAGE)
.build(); packageElement = packageElement.getEnclosingElement();
String packageName = packageElement.toString();
String injectorSimpleClassName = cl.getSimpleName().toString() + "Injector";
String injectorClassName = packageName + "." + injectorSimpleClassName;
MethodSpec methodDesc = MethodSpec.methodBuilder("methodDesc") MethodSpec methodDesc = MethodSpec.methodBuilder("methodDesc")
.addModifiers(Modifier.PUBLIC) .addModifiers(Modifier.PUBLIC)
@ -279,24 +254,26 @@ public class LilleroProcessor extends AbstractProcessor {
TypeSpec injectorClass = TypeSpec.classBuilder(injectorSimpleClassName) TypeSpec injectorClass = TypeSpec.classBuilder(injectorSimpleClassName)
.addModifiers(Modifier.PUBLIC) .addModifiers(Modifier.PUBLIC)
.addSuperinterface(ClassName.get(IInjector.class)) .addSuperinterface(ClassName.get(IInjector.class))
.addMethod(name) .addMethod(buildStringReturnMethod("name", injectorSimpleClassName))
.addMethod(reason) .addMethod(buildStringReturnMethod("reason", ann.reason()))
.addMethod(targetClass) .addMethod(buildStringReturnMethod("targetClass", targetClassSrgName.replace('/', '.')))
.addMethod(methodName) .addMethod(buildStringReturnMethod("methodName", targetMethodSrgName))
.addMethod(methodDesc) .addMethod(methodDesc)
.addMethod(inject) .addMethod(inject)
.build(); .build();
JavaFile javaFile = JavaFile.builder(packageName, injectorClass).build();
try { try {
JavaFileObject injectorFile = processingEnv.getFiler().createSourceFile(injectorClassName); JavaFileObject injectorFile = processingEnv.getFiler().createSourceFile(injectorClassName);
PrintWriter out = new PrintWriter(injectorFile.openWriter()); PrintWriter out = new PrintWriter(injectorFile.openWriter());
JavaFile javaFile = JavaFile.builder(packageName, injectorClass).build();
javaFile.writeTo(out); javaFile.writeTo(out);
out.close(); out.close();
this.generatedInjectors.add(injectorClassName);
} catch(IOException e) { } catch(IOException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
this.generatedInjectors.add(injectorClassName);
} }
/** /**