|
| 1 | +package io.javaoperatorsdk.operator.processing; |
| 2 | + |
| 3 | +import com.google.auto.service.AutoService; |
| 4 | +import com.sun.tools.javac.code.Symbol; |
| 5 | + |
| 6 | +import javax.annotation.processing.*; |
| 7 | +import javax.lang.model.SourceVersion; |
| 8 | +import javax.lang.model.element.Element; |
| 9 | +import javax.lang.model.element.TypeElement; |
| 10 | +import javax.lang.model.type.DeclaredType; |
| 11 | +import javax.lang.model.type.TypeMirror; |
| 12 | +import javax.tools.JavaFileObject; |
| 13 | +import java.io.IOException; |
| 14 | +import java.io.PrintWriter; |
| 15 | +import java.util.Set; |
| 16 | + |
| 17 | +@SupportedAnnotationTypes( |
| 18 | + "io.javaoperatorsdk.operator.api.Controller") |
| 19 | +@SupportedSourceVersion(SourceVersion.RELEASE_8) |
| 20 | +@AutoService(Processor.class) |
| 21 | +public class ControllerAnnotationProcessor extends AbstractProcessor { |
| 22 | + @Override |
| 23 | + public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { |
| 24 | + for (TypeElement annotation : annotations) { |
| 25 | + Set<? extends Element> annotatedElements |
| 26 | + = roundEnv.getElementsAnnotatedWith(annotation); |
| 27 | + annotatedElements.stream().filter(element -> element instanceof Symbol.ClassSymbol) |
| 28 | + .map(e -> (Symbol.ClassSymbol) e) |
| 29 | + .forEach(e -> { |
| 30 | + JavaFileObject builderFile = null; |
| 31 | + try { |
| 32 | + final TypeMirror resourceType = ((DeclaredType) e.getInterfaces().head).getTypeArguments().get(0); |
| 33 | + Symbol.ClassSymbol ee = (Symbol.ClassSymbol) processingEnv.getElementUtils().getTypeElement(resourceType.toString()); |
| 34 | + builderFile = processingEnv.getFiler() |
| 35 | + .createSourceFile(ee.className() + "Doneable"); |
| 36 | + try (PrintWriter out = new PrintWriter(builderFile.openWriter())) { |
| 37 | + out.println("package " + ee.packge().fullname + ";"); |
| 38 | + out.println("import io.quarkus.runtime.annotations.RegisterForReflection;"); |
| 39 | + out.println("import io.fabric8.kubernetes.api.builder.Function;"); |
| 40 | + out.println("import io.fabric8.kubernetes.client.CustomResourceDoneable;"); |
| 41 | + out.println(); |
| 42 | + out.println("@RegisterForReflection"); |
| 43 | + out.println("public class " + ee.name + "Doneable " + " extends CustomResourceDoneable<" + ee.name + "> {"); |
| 44 | + out.println("public " + ee.name + "Doneable(" + ee.name + " resource, Function function){ super(resource,function);}"); |
| 45 | + out.println("}"); |
| 46 | + } |
| 47 | + } catch (IOException ioException) { |
| 48 | + ioException.printStackTrace(); |
| 49 | + } catch (Exception ex) { |
| 50 | + ex.printStackTrace(); |
| 51 | + } |
| 52 | + |
| 53 | + }); |
| 54 | + } |
| 55 | + return false; |
| 56 | + } |
| 57 | +} |
0 commit comments