|
| 1 | +package mekanism; |
| 2 | + |
| 3 | +import com.squareup.javapoet.ClassName; |
| 4 | +import com.squareup.javapoet.CodeBlock; |
| 5 | +import com.squareup.javapoet.JavaFile; |
| 6 | +import com.squareup.javapoet.MethodSpec; |
| 7 | +import com.squareup.javapoet.TypeSpec; |
| 8 | +import java.io.Writer; |
| 9 | +import java.util.ArrayList; |
| 10 | +import java.util.List; |
| 11 | +import java.util.Set; |
| 12 | +import javax.annotation.processing.AbstractProcessor; |
| 13 | +import javax.annotation.processing.ProcessingEnvironment; |
| 14 | +import javax.annotation.processing.RoundEnvironment; |
| 15 | +import javax.annotation.processing.SupportedAnnotationTypes; |
| 16 | +import javax.annotation.processing.SupportedOptions; |
| 17 | +import javax.annotation.processing.SupportedSourceVersion; |
| 18 | +import javax.lang.model.element.AnnotationMirror; |
| 19 | +import javax.lang.model.element.Element; |
| 20 | +import javax.lang.model.element.ElementKind; |
| 21 | +import javax.lang.model.element.Modifier; |
| 22 | +import javax.lang.model.element.TypeElement; |
| 23 | +import javax.tools.StandardLocation; |
| 24 | +import mekanism.visitors.AnnotationHelper; |
| 25 | + |
| 26 | +import javax.lang.model.SourceVersion; |
| 27 | +import javax.lang.model.type.TypeKind; |
| 28 | +import javax.lang.model.type.TypeMirror; |
| 29 | +import javax.lang.model.util.Types; |
| 30 | +import javax.tools.Diagnostic; |
| 31 | +import java.io.IOException; |
| 32 | + |
| 33 | +/** |
| 34 | + * Gathering (Gradle) annotation processor which generates a ComputerMethodRegistry for the Factories generated |
| 35 | + * by the {@link ComputerMethodProcessor} processor. |
| 36 | + */ |
| 37 | +@SupportedSourceVersion(SourceVersion.RELEASE_17) |
| 38 | +@SupportedAnnotationTypes(MekAnnotationProcessors.COMPUTER_METHOD_FACTORY_ANNOTATION_CLASSNAME) |
| 39 | +@SupportedOptions(MekAnnotationProcessors.MODULE_OPTION) |
| 40 | +public class MethodFactoryProcessor extends AbstractProcessor { |
| 41 | + private String mekModule; |
| 42 | + private final ClassName factoryRegistry = ClassName.get(MekAnnotationProcessors.COMPUTER_INTEGRATION_PACKAGE, "FactoryRegistry"); |
| 43 | + private final ClassName methodRegistryInterface = ClassName.get(MekAnnotationProcessors.COMPUTER_INTEGRATION_PACKAGE, "IComputerMethodRegistry"); |
| 44 | + private final MethodSpec.Builder registryInit = MethodSpec.methodBuilder("register") |
| 45 | + .addModifiers(Modifier.PUBLIC) |
| 46 | + .addAnnotation(Override.class); |
| 47 | + |
| 48 | + @Override |
| 49 | + public synchronized void init(ProcessingEnvironment processingEnv) { |
| 50 | + super.init(processingEnv); |
| 51 | + mekModule = processingEnv.getOptions().getOrDefault(MekAnnotationProcessors.MODULE_OPTION, "value_not_supplied"); |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + public boolean process(Set<? extends TypeElement> annotatedTypes, RoundEnvironment roundEnvironment) { |
| 56 | + TypeMirror methodFactoryType = processingEnv.getElementUtils().getTypeElement(MekAnnotationProcessors.COMPUTER_METHOD_FACTORY_ANNOTATION_CLASSNAME).asType(); |
| 57 | + TypeSpec.Builder registryType = TypeSpec.classBuilder("ComputerMethodRegistry_" + mekModule) |
| 58 | + .addModifiers(Modifier.PUBLIC) |
| 59 | + .addSuperinterface(methodRegistryInterface); |
| 60 | + |
| 61 | + //this should only ever be 1 annotation |
| 62 | + for (Element element : roundEnvironment.getElementsAnnotatedWithAny(annotatedTypes.toArray(new TypeElement[0]))) { |
| 63 | + if (element instanceof TypeElement factoryTypeEl) { |
| 64 | + //get the annotation mirror for @MethodFactory |
| 65 | + AnnotationMirror annotationMirror = null; |
| 66 | + for (AnnotationMirror am : factoryTypeEl.getAnnotationMirrors()) { |
| 67 | + if (typeUtils().isSameType(am.getAnnotationType(), methodFactoryType)) { |
| 68 | + annotationMirror = am; |
| 69 | + break; |
| 70 | + } |
| 71 | + } |
| 72 | + if (annotationMirror == null) { |
| 73 | + processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "Couldn't find annotation mirror", factoryTypeEl); |
| 74 | + continue; |
| 75 | + } |
| 76 | + registryType.addOriginatingElement(factoryTypeEl); |
| 77 | + AnnotationHelper helper = new AnnotationHelper(processingEnv.getElementUtils(), annotationMirror); |
| 78 | + addHandlerToRegistry((TypeElement) typeUtils().asElement(helper.getClassValue("target")), ClassName.get(factoryTypeEl)); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + if (!registryType.originatingElements.isEmpty()) { |
| 83 | + registryType.addMethod(registryInit.build()); |
| 84 | + TypeSpec registrySpec = registryType.build(); |
| 85 | + String packageName = "mekanism.generated." + mekModule; |
| 86 | + try { |
| 87 | + JavaFile.builder(packageName, registrySpec).build().writeTo(processingEnv.getFiler()); |
| 88 | + } catch (IOException e) { |
| 89 | + throw new RuntimeException(e); |
| 90 | + } |
| 91 | + try(Writer serviceWriter = processingEnv.getFiler().createResource(StandardLocation.CLASS_OUTPUT, "","META-INF/services/"+methodRegistryInterface.canonicalName()).openWriter()) { |
| 92 | + serviceWriter.write(packageName+"."+registrySpec.name); |
| 93 | + } catch (IOException e) { |
| 94 | + throw new RuntimeException(e); |
| 95 | + } |
| 96 | + } |
| 97 | + return false; |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * Gather superclasses for handledType and add a register call |
| 102 | + * @param handledType the subject type of the handler |
| 103 | + * @param factoryClassName the factory's class name |
| 104 | + */ |
| 105 | + private void addHandlerToRegistry(TypeElement handledType, ClassName factoryClassName) { |
| 106 | + //gather all superclasses (in mekanism package) |
| 107 | + List<ClassName> superClasses = new ArrayList<>(); |
| 108 | + TypeMirror superClass = handledType.getSuperclass(); |
| 109 | + TypeElement superTypeElement; |
| 110 | + do { |
| 111 | + superTypeElement = (TypeElement) typeUtils().asElement(superClass); |
| 112 | + if (superTypeElement == null) { |
| 113 | + break; |
| 114 | + } |
| 115 | + ClassName clazz = ClassName.get(superTypeElement); |
| 116 | + if (clazz.canonicalName().startsWith("mekanism")) { |
| 117 | + superClasses.add(0, clazz); |
| 118 | + } |
| 119 | + } while ((superClass = superTypeElement.getSuperclass()).getKind() != TypeKind.NONE); |
| 120 | + |
| 121 | + //add register call to the factory |
| 122 | + String registerName = handledType.getKind() == ElementKind.INTERFACE ? "registerInterface" : "register"; |
| 123 | + CodeBlock.Builder registerStatement = CodeBlock.builder() |
| 124 | + .add("$T.$L($T.class, $T::new", factoryRegistry, registerName, typeUtils().erasure(handledType.asType()), factoryClassName); |
| 125 | + //add all super classes, so we don't have to calculate at runtime |
| 126 | + for (ClassName cls : superClasses) { |
| 127 | + registerStatement.add(", $T.class", cls); |
| 128 | + } |
| 129 | + registerStatement.add(")"); |
| 130 | + registryInit.addStatement(registerStatement.build()); |
| 131 | + } |
| 132 | + |
| 133 | + private Types typeUtils() { |
| 134 | + return processingEnv.getTypeUtils(); |
| 135 | + } |
| 136 | +} |
0 commit comments