|
| 1 | +package com.segementfalut.deep.in.java.annotation.processing; |
| 2 | + |
| 3 | +import javax.annotation.processing.*; |
| 4 | +import javax.lang.model.SourceVersion; |
| 5 | +import javax.lang.model.element.Element; |
| 6 | +import javax.lang.model.element.ElementKind; |
| 7 | +import javax.lang.model.element.Modifier; |
| 8 | +import javax.lang.model.element.TypeElement; |
| 9 | +import javax.lang.model.type.DeclaredType; |
| 10 | +import javax.lang.model.type.TypeMirror; |
| 11 | +import javax.lang.model.util.Types; |
| 12 | +import javax.tools.FileObject; |
| 13 | +import javax.tools.StandardLocation; |
| 14 | +import java.io.IOException; |
| 15 | +import java.io.Writer; |
| 16 | +import java.sql.Wrapper; |
| 17 | +import java.util.*; |
| 18 | + |
| 19 | +/** |
| 20 | + * 1. 扩展 javax.annotation.processing.AbstractProcessor 抽象类 |
| 21 | + * 2. 指定需要处理的注解类名(集合) |
| 22 | + * 3. 指定支持的 Java 源代码版本 |
| 23 | + * 4. 指定支持的 Processorcom.segementfalut.deep.in.java.reflection.Repository 参数 Options(可选) |
| 24 | + * 5. 利用 Java SPI 配置 javax.annotation.processing.Processor 的实现类 |
| 25 | + */ |
| 26 | +@SupportedAnnotationTypes(RepositoryAnnotationProcessor.REPOSITORY_ANNOTATION_CLASS_NAME) |
| 27 | +@SupportedSourceVersion(SourceVersion.RELEASE_8) |
| 28 | +public class RepositoryAnnotationProcessor extends AbstractProcessor { |
| 29 | + |
| 30 | + public static final String REPOSITORY_ANNOTATION_CLASS_NAME = "com.segementfalut.deep.in.java.reflection.Repository"; |
| 31 | + |
| 32 | + private static final String CRUD_REPOSITORY_INTERFACE_CLASS_NAME = "com.segementfalut.deep.in.java.reflection.CrudRepository"; |
| 33 | + |
| 34 | + /** |
| 35 | + * Key 为 CrudRepository 接口实现类,Value 为 CrudRepository 接口首个参数类型 |
| 36 | + */ |
| 37 | + private Map<String, String> crudRepositoryParameterizedTypesMapping = new HashMap<>(); |
| 38 | + |
| 39 | + @Override |
| 40 | + public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { |
| 41 | + // 输出处理的注解类名称 |
| 42 | + |
| 43 | + // 第一个阶段:处理阶段 |
| 44 | + // 获取所有的编译类 |
| 45 | + roundEnv.getRootElements() |
| 46 | + .stream() |
| 47 | + .filter(this::isRepositoryAnnotationPresent) // 过滤标注 @com.segementfalut.deep.in.java.reflection.Repository 的元素 |
| 48 | + .forEach(this::processRepositoryAnnotatedElement); // 处理标注 @com.segementfalut.deep.in.java.reflection.Repository 的元素 |
| 49 | + |
| 50 | + |
| 51 | + // 第二个阶段:完成阶段 |
| 52 | + if (roundEnv.processingOver()) { |
| 53 | + // 将 crudRepositoryParameterizedTypesMapping 输出到新生成的文件 |
| 54 | + try { |
| 55 | + generateCrudRepositoryParameterizedTypesMetadata(); |
| 56 | + } catch (IOException e) { |
| 57 | + throw new RuntimeException(e); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + return false; |
| 62 | + } |
| 63 | + |
| 64 | + private void generateCrudRepositoryParameterizedTypesMetadata() throws IOException { |
| 65 | + // 找到 ClassPath |
| 66 | + Filer filer = processingEnv.getFiler(); |
| 67 | + String resourceName = "META-INF/crud-repos-mappings.properties"; |
| 68 | + FileObject fileObject = filer.createResource(StandardLocation.CLASS_OUTPUT, "", resourceName); |
| 69 | + |
| 70 | + try (Writer writer = fileObject.openWriter()) { |
| 71 | + Properties properties = new Properties(); |
| 72 | + properties.putAll(crudRepositoryParameterizedTypesMapping); |
| 73 | + properties.store(writer, "Generated by RepositoryAnnotationProcessor"); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + |
| 78 | + private void processRepositoryAnnotatedElement(Element element) { |
| 79 | + |
| 80 | + if (!isConcreteClass(element) || !isCrudRepositoryType(element)) { |
| 81 | + return; |
| 82 | + } |
| 83 | + |
| 84 | + System.out.println("CrudRepository 实现类为 :" + element.toString()); |
| 85 | + |
| 86 | + // CrudRepository 接口类型 |
| 87 | + TypeMirror crudRepositoryGenericInterfaceType = getGenericInterfaceType(element, CRUD_REPOSITORY_INTERFACE_CLASS_NAME); |
| 88 | + |
| 89 | + System.out.println("CrudRepository 实现泛型接口定义为 :" + crudRepositoryGenericInterfaceType.toString()); |
| 90 | + |
| 91 | + // 由于 CrudRepository 是接口类型,可以强制转化为 DeclaredType |
| 92 | + DeclaredType declaredType = DeclaredType.class.cast(crudRepositoryGenericInterfaceType); |
| 93 | + // 获取泛型参数类型列表 |
| 94 | + List<? extends TypeMirror> parameterizedTypes = declaredType.getTypeArguments(); |
| 95 | + // 获取第一个参数类型 |
| 96 | + TypeMirror firstArgumentType = parameterizedTypes.get(0); |
| 97 | + // 必然等于 User 对象 |
| 98 | + System.out.println("CrudRepository 实现接口的首个泛型参数为 :" + firstArgumentType.toString()); |
| 99 | + // Key 为 CrudRepository 接口实现类,Value 为 CrudRepository 接口首个参数类型 |
| 100 | + crudRepositoryParameterizedTypesMapping.put(crudRepositoryGenericInterfaceType.toString(), firstArgumentType.toString()); |
| 101 | + } |
| 102 | + |
| 103 | + private boolean isConcreteClass(Element element) { |
| 104 | + return !element.getModifiers().contains(Modifier.ABSTRACT); |
| 105 | + } |
| 106 | + |
| 107 | + private boolean isCrudRepositoryType(Element element) { |
| 108 | + return getGenericInterfaceType(element, CRUD_REPOSITORY_INTERFACE_CLASS_NAME) != null; |
| 109 | + } |
| 110 | + |
| 111 | + private TypeMirror getGenericInterfaceType(Element element, String interfaceTypeName) { |
| 112 | + ElementKind kind = element.getKind(); |
| 113 | + if (kind.isClass() && element instanceof TypeElement) { |
| 114 | + TypeElement typeElement = (TypeElement) element; |
| 115 | + return typeElement.getInterfaces().stream() // 发现当前参数类型的所有接口 |
| 116 | + .filter(interfaceType -> typeEquals(interfaceType, interfaceTypeName)) |
| 117 | + .findFirst() // 找到第一个符合条件的 CrudRepository 接口 |
| 118 | + .orElse(null); |
| 119 | + } |
| 120 | + return null; |
| 121 | + } |
| 122 | + |
| 123 | + private boolean typeEquals(TypeMirror type, String typeName) { |
| 124 | + Types types = processingEnv.getTypeUtils(); |
| 125 | + TypeMirror erasedType = types.erasure(type); // 擦写泛型参数 |
| 126 | + return Objects.equals(typeName, erasedType.toString()); |
| 127 | + } |
| 128 | + |
| 129 | + private boolean isRepositoryAnnotationPresent(Element element) { |
| 130 | + return isAnnotationPresent(element, REPOSITORY_ANNOTATION_CLASS_NAME); |
| 131 | + } |
| 132 | + |
| 133 | + private boolean isAnnotationPresent(Element element, String annotationClassName) { |
| 134 | + return element.getAnnotationMirrors() // 返回当前元素的所有注解集合 |
| 135 | + .stream() |
| 136 | + .filter(annotation -> Objects.equals(annotationClassName, annotation.getAnnotationType().toString())) |
| 137 | + .count() > 0; |
| 138 | + } |
| 139 | + |
| 140 | +// @Override |
| 141 | +// public SourceVersion getSupportedSourceVersion() { |
| 142 | +// return SourceVersion.latest(); |
| 143 | +// } |
| 144 | +// |
| 145 | +// @Override |
| 146 | +// public Set<String> getSupportedAnnotationTypes() { |
| 147 | +// return singleton("com.segementfalut.deep.in.java.reflection.Repository"); |
| 148 | +// } |
| 149 | +} |
0 commit comments