|
13 | 13 | import javax.lang.model.type.DeclaredType;
|
14 | 14 | import javax.lang.model.type.TypeKind;
|
15 | 15 | import javax.lang.model.type.TypeMirror;
|
| 16 | +import javax.tools.FileObject; |
16 | 17 | import javax.tools.JavaFileObject;
|
| 18 | +import javax.tools.StandardLocation; |
| 19 | +import java.io.IOException; |
17 | 20 | import java.io.PrintWriter;
|
18 | 21 | import java.util.ArrayList;
|
| 22 | +import java.util.HashSet; |
19 | 23 | import java.util.List;
|
20 | 24 | import java.util.Set;
|
21 | 25 | import java.util.stream.Collectors;
|
|
25 | 29 | @SupportedSourceVersion(SourceVersion.RELEASE_8)
|
26 | 30 | @AutoService(Processor.class)
|
27 | 31 | public class ControllerAnnotationProcessor extends AbstractProcessor {
|
| 32 | + private FileObject resource; |
| 33 | + PrintWriter printWriter = null; |
| 34 | + private Set<String> generatedDoneableClassFiles = new HashSet<>(); |
| 35 | + |
| 36 | + @Override |
| 37 | + public synchronized void init(ProcessingEnvironment processingEnv) { |
| 38 | + super.init(processingEnv); |
| 39 | + try { |
| 40 | + resource = processingEnv.getFiler().createResource(StandardLocation.CLASS_OUTPUT, "", "javaoperatorsdk-custom-resources"); |
| 41 | + } catch (IOException e) { |
| 42 | + throw new RuntimeException(e); |
| 43 | + } |
| 44 | + try { |
| 45 | + printWriter = new PrintWriter(resource.openOutputStream()); |
| 46 | + } catch (IOException e) { |
| 47 | + throw new RuntimeException(e); |
| 48 | + } |
| 49 | + } |
| 50 | + |
28 | 51 | @Override
|
29 | 52 | public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
|
30 |
| - for (TypeElement annotation : annotations) { |
31 |
| - Set<? extends Element> annotatedElements |
32 |
| - = roundEnv.getElementsAnnotatedWith(annotation); |
33 |
| - annotatedElements.stream().filter(element -> element.getKind().equals(ElementKind.CLASS)) |
34 |
| - .map(e -> (TypeElement) e) |
35 |
| - .forEach(this::generateDoneableClass); |
| 53 | + |
| 54 | + try { |
| 55 | + for (TypeElement annotation : annotations) { |
| 56 | + Set<? extends Element> annotatedElements |
| 57 | + = roundEnv.getElementsAnnotatedWith(annotation); |
| 58 | + annotatedElements.stream().filter(element -> element.getKind().equals(ElementKind.CLASS)) |
| 59 | + .map(e -> (TypeElement) e) |
| 60 | + .forEach(e -> this.generateDoneableClass(e, printWriter)); |
| 61 | + } |
| 62 | + } finally { |
| 63 | + printWriter.close(); |
36 | 64 | }
|
37 | 65 | return true;
|
38 | 66 | }
|
39 | 67 |
|
40 |
| - private void generateDoneableClass(TypeElement controllerClassSymbol) { |
| 68 | + private void generateDoneableClass(TypeElement controllerClassSymbol, PrintWriter printWriter) { |
41 | 69 | try {
|
42 | 70 | final TypeMirror resourceType = findResourceType(controllerClassSymbol);
|
| 71 | + |
43 | 72 | TypeElement customerResourceTypeElement = processingEnv
|
44 | 73 | .getElementUtils()
|
45 | 74 | .getTypeElement(resourceType.toString());
|
46 | 75 |
|
| 76 | + final String doneableClassName = customerResourceTypeElement.getSimpleName() + "Doneable"; |
47 | 77 | final String destinationClassFileName = customerResourceTypeElement.getQualifiedName() + "Doneable";
|
| 78 | + final TypeName customResourceType = TypeName.get(resourceType); |
| 79 | + if (!generatedDoneableClassFiles.add(destinationClassFileName)) { |
| 80 | + printWriter.println(controllerClassSymbol.getQualifiedName() + "," + customResourceType.toString()); |
| 81 | + return; |
| 82 | + } |
48 | 83 | JavaFileObject builderFile = processingEnv.getFiler()
|
49 | 84 | .createSourceFile(destinationClassFileName);
|
50 | 85 |
|
51 | 86 | try (PrintWriter out = new PrintWriter(builderFile.openWriter())) {
|
| 87 | + printWriter.println(controllerClassSymbol.getQualifiedName() + "," + customResourceType.toString()); |
52 | 88 | final MethodSpec constructor = MethodSpec.constructorBuilder()
|
53 | 89 | .addModifiers(Modifier.PUBLIC)
|
54 |
| - .addParameter(TypeName.get(resourceType), "resource") |
| 90 | + .addParameter(customResourceType, "resource") |
55 | 91 | .addParameter(Function.class, "function")
|
56 | 92 | .addStatement("super(resource,function)")
|
57 | 93 | .build();
|
58 | 94 |
|
59 |
| - final TypeSpec typeSpec = TypeSpec.classBuilder(customerResourceTypeElement.getSimpleName() + "Doneable") |
| 95 | + |
| 96 | + final TypeSpec typeSpec = TypeSpec.classBuilder(doneableClassName) |
60 | 97 | .addAnnotation(RegisterForReflection.class)
|
61 |
| - .superclass(ParameterizedTypeName.get(ClassName.get(CustomResourceDoneable.class), TypeName.get(resourceType))) |
| 98 | + .superclass(ParameterizedTypeName.get(ClassName.get(CustomResourceDoneable.class), customResourceType)) |
62 | 99 | .addModifiers(Modifier.PUBLIC)
|
63 | 100 | .addMethod(constructor)
|
64 | 101 | .build();
|
|
0 commit comments