|
| 1 | +package com.baeldung.annotation.processor; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.io.PrintWriter; |
| 5 | +import java.util.List; |
| 6 | +import java.util.Map; |
| 7 | +import java.util.Set; |
| 8 | +import java.util.stream.Collectors; |
| 9 | +import javax.annotation.processing.*; |
| 10 | +import javax.lang.model.SourceVersion; |
| 11 | +import javax.lang.model.element.Element; |
| 12 | +import javax.lang.model.element.TypeElement; |
| 13 | +import javax.lang.model.type.ExecutableType; |
| 14 | +import javax.tools.Diagnostic; |
| 15 | +import javax.tools.JavaFileObject; |
| 16 | + |
| 17 | +import com.google.auto.service.AutoService; |
| 18 | + |
| 19 | +@SupportedAnnotationTypes("com.baeldung.annotation.processor.BuilderProperty") |
| 20 | +@SupportedSourceVersion(SourceVersion.RELEASE_8) |
| 21 | +@AutoService(Processor.class) |
| 22 | +public class BuilderProcessor extends AbstractProcessor { |
| 23 | + |
| 24 | + @Override |
| 25 | + public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { |
| 26 | + for (TypeElement annotation : annotations) { |
| 27 | + |
| 28 | + Set<? extends Element> annotatedElements = roundEnv.getElementsAnnotatedWith(annotation); |
| 29 | + |
| 30 | + Map<Boolean, List<Element>> annotatedMethods = annotatedElements.stream() |
| 31 | + .collect(Collectors.partitioningBy(element -> |
| 32 | + ((ExecutableType) element.asType()).getParameterTypes().size() == 1 |
| 33 | + && element.getSimpleName().toString().startsWith("set"))); |
| 34 | + |
| 35 | + List<Element> setters = annotatedMethods.get(true); |
| 36 | + List<Element> otherMethods = annotatedMethods.get(false); |
| 37 | + |
| 38 | + otherMethods.forEach(element -> |
| 39 | + processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, |
| 40 | + "@BuilderProperty must be applied to a setXxx method with a single argument", element)); |
| 41 | + |
| 42 | + if (setters.isEmpty()) { |
| 43 | + continue; |
| 44 | + } |
| 45 | + |
| 46 | + String className = ((TypeElement) setters.get(0).getEnclosingElement()).getQualifiedName().toString(); |
| 47 | + |
| 48 | + Map<String, String> setterMap = setters.stream().collect(Collectors.toMap( |
| 49 | + setter -> setter.getSimpleName().toString(), |
| 50 | + setter -> ((ExecutableType) setter.asType()) |
| 51 | + .getParameterTypes().get(0).toString() |
| 52 | + )); |
| 53 | + |
| 54 | + try { |
| 55 | + writeBuilderFile(className, setterMap); |
| 56 | + } catch (IOException e) { |
| 57 | + e.printStackTrace(); |
| 58 | + } |
| 59 | + |
| 60 | + } |
| 61 | + |
| 62 | + return true; |
| 63 | + } |
| 64 | + |
| 65 | + private void writeBuilderFile(String className, Map<String, String> setterMap) throws IOException { |
| 66 | + |
| 67 | + String packageName = null; |
| 68 | + int lastDot = className.lastIndexOf('.'); |
| 69 | + if (lastDot > 0) { |
| 70 | + packageName = className.substring(0, lastDot); |
| 71 | + } |
| 72 | + |
| 73 | + String simpleClassName = className.substring(lastDot + 1); |
| 74 | + String builderClassName = className + "Builder"; |
| 75 | + String builderSimpleClassName = builderClassName.substring(lastDot + 1); |
| 76 | + |
| 77 | + JavaFileObject builderFile = processingEnv.getFiler().createSourceFile(builderClassName); |
| 78 | + try (PrintWriter out = new PrintWriter(builderFile.openWriter())) { |
| 79 | + |
| 80 | + if (packageName != null) { |
| 81 | + out.print("package "); |
| 82 | + out.print(packageName); |
| 83 | + out.println(";"); |
| 84 | + out.println(); |
| 85 | + } |
| 86 | + |
| 87 | + out.print("public class "); |
| 88 | + out.print(builderSimpleClassName); |
| 89 | + out.println(" {"); |
| 90 | + out.println(); |
| 91 | + |
| 92 | + out.print(" private "); |
| 93 | + out.print(simpleClassName); |
| 94 | + out.print(" object = new "); |
| 95 | + out.print(simpleClassName); |
| 96 | + out.println("();"); |
| 97 | + out.println(); |
| 98 | + |
| 99 | + out.print(" public "); |
| 100 | + out.print(simpleClassName); |
| 101 | + out.println(" build() {"); |
| 102 | + out.println(" return object;"); |
| 103 | + out.println(" }"); |
| 104 | + out.println(); |
| 105 | + |
| 106 | + setterMap.entrySet().forEach(setter -> { |
| 107 | + String methodName = setter.getKey(); |
| 108 | + String argumentType = setter.getValue(); |
| 109 | + |
| 110 | + out.print(" public "); |
| 111 | + out.print(builderSimpleClassName); |
| 112 | + out.print(" "); |
| 113 | + out.print(methodName); |
| 114 | + |
| 115 | + out.print("("); |
| 116 | + |
| 117 | + out.print(argumentType); |
| 118 | + out.println(" value) {"); |
| 119 | + out.print(" object."); |
| 120 | + out.print(methodName); |
| 121 | + out.println("(value);"); |
| 122 | + out.println(" return this;"); |
| 123 | + out.println(" }"); |
| 124 | + out.println(); |
| 125 | + }); |
| 126 | + |
| 127 | + out.println("}"); |
| 128 | + |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | +} |
0 commit comments