java.lang.Object
cn.wjybxx.apt.AptUtils
- 作者:
- wjybxx date 2023/4/6
-
字段概要
字段修饰符和类型字段说明static final com.squareup.javapoet.AnnotationSpecstatic final com.squareup.javapoet.AnnotationSpecstatic final com.squareup.javapoet.ClassNamestatic final com.squareup.javapoet.ClassNamestatic final com.squareup.javapoet.TypeNamestatic final com.squareup.javapoet.ClassNamestatic final com.squareup.javapoet.ClassNamestatic final com.squareup.javapoet.ClassNamestatic final com.squareup.javapoet.ClassNamestatic final com.squareup.javapoet.ClassNamestatic final com.squareup.javapoet.ClassNamestatic final com.squareup.javapoet.ClassNamestatic final com.squareup.javapoet.ClassNamestatic final com.squareup.javapoet.ClassNamestatic final com.squareup.javapoet.ClassNamestatic final com.squareup.javapoet.ClassNamestatic final com.squareup.javapoet.ClassNamestatic final com.squareup.javapoet.ClassNamestatic final Modifier[]static final Modifier[]static final Modifier[]static final SourceVersionstatic final com.squareup.javapoet.AnnotationSpecstatic final com.squareup.javapoet.AnnotationSpec由于生成的代码不能很好的处理泛型等信息,因此需要抑制警告 -
方法概要
修饰符和类型方法说明static com.squareup.javapoet.ClassNameclassNameOfCanonicalName(String cname) static voidcopyExceptionsTable(com.squareup.javapoet.MethodSpec.Builder builder, ExecutableElement method) 拷贝一个方法的异常表static com.squareup.javapoet.MethodSpec.BuildercopyMethod(ExecutableElement method) 复制一个方法信息,当然不包括代码块。static voidcopyModifiers(com.squareup.javapoet.MethodSpec.Builder builder, ExecutableElement method) 拷贝一个方法的修饰符static voidcopyParameters(com.squareup.javapoet.MethodSpec.Builder builder, List<? extends VariableElement> parameters) 拷贝这些方法参数static voidcopyParameters(com.squareup.javapoet.MethodSpec.Builder builder, ExecutableElement method) 拷贝一个方法的所有参数static voidcopyReturnType(com.squareup.javapoet.MethodSpec.Builder builder, ExecutableElement method) 拷贝返回值类型static voidcopyTypeVariables(com.squareup.javapoet.MethodSpec.Builder builder, ExecutableElement method) 拷贝一个方法的所有泛型参数static List<TypeMirror> findAllInterfaces(Types typeUtil, Elements elementUtil, TypeElement typeElement) 注意:无法保证顺序 -- 这个方法还不太稳定static AnnotationMirrorfindAnnotation(Types typeUtils, Element element, TypeMirror targetAnnotationMirror) 查找出现的第一个注解,不包含继承的部分static AnnotationMirrorfindAnnotationWithInherit(Types typeUtils, Elements elementUtils, Element element, TypeMirror targetAnnotationMirror) 查找出现的第一个注解,包含继承的注解static DeclaredTypefindDeclaredType(TypeMirror typeMirror) 获取一个元素的声明类型static VariableElementfindFieldByName(TypeElement typeElement, String field) 通过名字查找对应的字段static TypeMirrorfindFirstTypeParameter(TypeMirror typeMirror) 获取第一个泛型参数static ExecutableElementfindMethodByName(TypeElement typeElement, String methodName) 通过名字查找对应的方法static List<TypeElement> flatInherit(TypeElement typeElement) 将继承体系展开,不包含实现的接口。static List<TypeElement> flatInheritAndReverse(TypeElement typeElement) 将继承体系展开,并逆序返回,不包含实现的接口。static AnnotationValuegetAnnotationValue(AnnotationMirror annotationMirror, String propertyName) 获取注解上的某一个属性的值,不包含default值static Map<String, AnnotationValue> getAnnotationValuesMap(AnnotationMirror annotationMirror) 将注解属性转换为 name -> AnnotationValue 的Mapstatic TypeMirrorgetAnnotationValueTypeMirror(AnnotationValue annotationValue) 获取注解中引用的class对象的类型 eg:static <T> TgetAnnotationValueValue(AnnotationMirror annotationMirror, String propertyName) 获取注解上的某一个属性的值,不包含default值static <T> TgetAnnotationValueValue(AnnotationMirror annotationMirror, String propertyName, T def) 获取注解上的某一个属性的值static <T> TgetAnnotationValueValueWithDefaults(Elements elementUtils, AnnotationMirror annotationMirror, String propertyName) 获取注解上的某一个属性的值,包含default值static AnnotationValuegetAnnotationValueWithDefaults(Elements elementUtils, AnnotationMirror annotationMirror, String propertyName) 获取注解上的某一个属性的值,包含default值static TypeMirrorgetComponentType(TypeMirror typeMirror) static StringgetPackageName(TypeElement typeElement, Elements elementUtils) 获取一个类或接口所属的包名static StringgetProxyClassName(Elements elementUtils, TypeElement typeElement, String suffix) 根据原类型,生成获得对应的辅助类的类名 对于内部类,生成的类为:外部类名_内部类名static StringgetStackTrace(Throwable throwable) 获取堆栈信息,避免引入commons-lang3static TypeElementgetTypeElementOfClass(Elements elementUtils, Class<?> clazz) static TypeMirrorgetTypeMirrorOfClass(Elements elementUtils, Class<?> clazz) static booleanisAnnotationPresent(Types typeUtils, Element element, TypeMirror targetAnnotationMirror) 查找指定注解是否出现static booleanisArrayType(TypeMirror typeMirror) 是否是数组类型static booleanstatic booleanisBoxedType(com.squareup.javapoet.TypeName typeName) 查询给定TypeName是否是包装类型 -- 比使用equals快static booleanisByteArray(TypeMirror typeMirror) 判断一个对象是否是字节数组static booleanstatic booleanisEmpty(Collection<?> c) static booleanstatic booleanisPrimitiveBoolean(TypeMirror typeMirror) 是否是基本类型的booleanstatic booleanisSameTypeIgnoreTypeParameter(Types typeUtil, TypeMirror first, TypeMirror second) 忽略两个类型的泛型参数,判断第一个和第二个类型是否相同 如果考虑泛型的话,会存在区别static booleanisSubTypeIgnoreTypeParameter(Types typeUtil, TypeMirror first, TypeMirror second) 忽略两个类型的泛型参数,判断第一个是否是第二个的子类型。static com.squareup.javapoet.AnnotationSpecnewProcessorInfoAnnotation(Class<?> processorType) 为生成代码的注解处理器创建一个通用注解static com.squareup.javapoet.AnnotationSpecnewSourceFileRefAnnotation(com.squareup.javapoet.TypeName sourceFileTypeName) 添加一个指向源代码文件的引用 方便查看文件的依赖static Set<TypeElement> selectSourceFile(Set<TypeElement> typeElementSet, Elements elementUtils) 筛选出java源文件 - 去除带有注解的class文件,用于避免二次处理某个文件。static Set<TypeElement> selectSourceFile(RoundEnvironment env, Elements elementUtils, TypeElement annoType) 筛选出java源文件 - 去除带有注解的class文件,用于避免二次处理某个文件。static Set<TypeElement> selectSourceFileAny(RoundEnvironment env, Elements elementUtils, TypeElement... annoType) 筛选出java源文件 - 去除带有注解的class文件,用于避免二次处理某个文件。static DeclaredTypeupwardToSuperTypeMirror(Types typeUtils, TypeMirror self, TypeMirror target) 将当前类型转型到目标超类型 用于获取当前类型传递给超类型的一些信息static voidwriteToFile(Element sourceElement, com.squareup.javapoet.TypeSpec.Builder typeBuilder, String outPackage, Messager messager, Filer filer) static voidwriteToFile(TypeElement sourceElement, com.squareup.javapoet.TypeSpec.Builder typeBuilder, Elements elementUtils, Messager messager, Filer filer)
-
字段详细资料
-
SOURCE_VERSION
-
PUBLIC_STATIC
-
PUBLIC_STATIC_FINAL
-
PRIVATE_STATIC_FINAL
-
SUPPRESS_UNCHECKED_RAWTYPES
public static final com.squareup.javapoet.AnnotationSpec SUPPRESS_UNCHECKED_RAWTYPES由于生成的代码不能很好的处理泛型等信息,因此需要抑制警告 -
SUPPRESS_UNCHECKED
public static final com.squareup.javapoet.AnnotationSpec SUPPRESS_UNCHECKED -
ANNOTATION_OVERRIDE
public static final com.squareup.javapoet.AnnotationSpec ANNOTATION_OVERRIDE -
ANNOTATION_NONNULL
public static final com.squareup.javapoet.AnnotationSpec ANNOTATION_NONNULL -
CLSNAME_SOURCE_REF
public static final com.squareup.javapoet.ClassName CLSNAME_SOURCE_REF -
CLSNAME_STRING
public static final com.squareup.javapoet.ClassName CLSNAME_STRING -
CLSNAME_BYTES
public static final com.squareup.javapoet.TypeName CLSNAME_BYTES -
CLSNAME_LIST
public static final com.squareup.javapoet.ClassName CLSNAME_LIST -
CLSNAME_ARRAY_LIST
public static final com.squareup.javapoet.ClassName CLSNAME_ARRAY_LIST -
CLSNAME_SET
public static final com.squareup.javapoet.ClassName CLSNAME_SET -
CLSNAME_HASH_SET
public static final com.squareup.javapoet.ClassName CLSNAME_HASH_SET -
CLSNAME_LINKED_SET
public static final com.squareup.javapoet.ClassName CLSNAME_LINKED_SET -
CLSNAME_MAP
public static final com.squareup.javapoet.ClassName CLSNAME_MAP -
CLSNAME_HASH_MAP
public static final com.squareup.javapoet.ClassName CLSNAME_HASH_MAP -
CLSNAME_LINKED_MAP
public static final com.squareup.javapoet.ClassName CLSNAME_LINKED_MAP -
CLSNAME_SUPPLIER
public static final com.squareup.javapoet.ClassName CLSNAME_SUPPLIER -
CLSNAME_ARRAYS
public static final com.squareup.javapoet.ClassName CLSNAME_ARRAYS -
CLSNAME_OBJECTS
public static final com.squareup.javapoet.ClassName CLSNAME_OBJECTS -
CLSNAME_FUTURE
public static final com.squareup.javapoet.ClassName CLSNAME_FUTURE -
CLSNAME_STAGE
public static final com.squareup.javapoet.ClassName CLSNAME_STAGE -
primitiveTypeNameMap
-
boxedTypeNameMap
-
-
方法详细资料
-
newProcessorInfoAnnotation
public static com.squareup.javapoet.AnnotationSpec newProcessorInfoAnnotation(Class<?> processorType) 为生成代码的注解处理器创建一个通用注解- 参数:
processorType- 注解处理器- 返回:
- 代码生成信息注解
-
newSourceFileRefAnnotation
public static com.squareup.javapoet.AnnotationSpec newSourceFileRefAnnotation(com.squareup.javapoet.TypeName sourceFileTypeName) 添加一个指向源代码文件的引用 方便查看文件的依赖 -
selectSourceFile
public static Set<TypeElement> selectSourceFile(RoundEnvironment env, Elements elementUtils, TypeElement annoType) 筛选出java源文件 - 去除带有注解的class文件,用于避免二次处理某个文件。 -
selectSourceFileAny
public static Set<TypeElement> selectSourceFileAny(RoundEnvironment env, Elements elementUtils, TypeElement... annoType) 筛选出java源文件 - 去除带有注解的class文件,用于避免二次处理某个文件。 -
selectSourceFile
public static Set<TypeElement> selectSourceFile(Set<TypeElement> typeElementSet, Elements elementUtils) 筛选出java源文件 - 去除带有注解的class文件,用于避免二次处理某个文件。- 参数:
typeElementSet- 带有注解的所有元素elementUtils- 用于获取元素的完整类名- 返回:
- 过滤后只有java源文件的元素
-
classNameOfCanonicalName
- 参数:
cname- 类的标准名,import语句格式
-
isBoxedType
public static boolean isBoxedType(com.squareup.javapoet.TypeName typeName) 查询给定TypeName是否是包装类型 -- 比使用equals快 -
copyMethod
public static com.squareup.javapoet.MethodSpec.Builder copyMethod(@Nonnull ExecutableElement method) 复制一个方法信息,当然不包括代码块。- 参数:
method- 方法信息- 返回:
- builder
-
copyModifiers
public static void copyModifiers(com.squareup.javapoet.MethodSpec.Builder builder, @Nonnull ExecutableElement method) 拷贝一个方法的修饰符 -
copyTypeVariables
public static void copyTypeVariables(com.squareup.javapoet.MethodSpec.Builder builder, ExecutableElement method) 拷贝一个方法的所有泛型参数 -
copyReturnType
public static void copyReturnType(com.squareup.javapoet.MethodSpec.Builder builder, @Nonnull ExecutableElement method) 拷贝返回值类型 -
copyParameters
public static void copyParameters(com.squareup.javapoet.MethodSpec.Builder builder, ExecutableElement method) 拷贝一个方法的所有参数 -
copyParameters
public static void copyParameters(com.squareup.javapoet.MethodSpec.Builder builder, List<? extends VariableElement> parameters) 拷贝这些方法参数 -
copyExceptionsTable
public static void copyExceptionsTable(com.squareup.javapoet.MethodSpec.Builder builder, ExecutableElement method) 拷贝一个方法的异常表 -
findMethodByName
通过名字查找对应的方法 -
findFieldByName
通过名字查找对应的字段 -
flatInherit
将继承体系展开,不包含实现的接口。 (超类在后) -
flatInheritAndReverse
将继承体系展开,并逆序返回,不包含实现的接口。 (超类在前) -
findAllInterfaces
public static List<TypeMirror> findAllInterfaces(Types typeUtil, Elements elementUtil, TypeElement typeElement) 注意:无法保证顺序 -- 这个方法还不太稳定 -
isAnnotationPresent
public static boolean isAnnotationPresent(Types typeUtils, Element element, TypeMirror targetAnnotationMirror) 查找指定注解是否出现 -
findAnnotation
public static AnnotationMirror findAnnotation(Types typeUtils, Element element, TypeMirror targetAnnotationMirror) 查找出现的第一个注解,不包含继承的部分 -
findAnnotationWithInherit
public static AnnotationMirror findAnnotationWithInherit(Types typeUtils, Elements elementUtils, Element element, TypeMirror targetAnnotationMirror) 查找出现的第一个注解,包含继承的注解 -
getAnnotationValue
@Nullable public static AnnotationValue getAnnotationValue(AnnotationMirror annotationMirror, String propertyName) 获取注解上的某一个属性的值,不包含default值 -
getAnnotationValueWithDefaults
@Nonnull public static AnnotationValue getAnnotationValueWithDefaults(Elements elementUtils, AnnotationMirror annotationMirror, String propertyName) 获取注解上的某一个属性的值,包含default值 -
getAnnotationValueValue
@Nullable public static <T> T getAnnotationValueValue(AnnotationMirror annotationMirror, String propertyName) 获取注解上的某一个属性的值,不包含default值
注意:数组都不是直接值,不能直接得到@SuppressWarnings({"unchecked", "rawtypes"}) => {"unchecked", "rawtypes"}List<String>- 参数:
annotationMirror- 注解编译信息propertyName- 属性的名字- 返回:
- object
-
getAnnotationValueValue
public static <T> T getAnnotationValueValue(AnnotationMirror annotationMirror, String propertyName, T def) 获取注解上的某一个属性的值- 参数:
annotationMirror- 注解编译信息propertyName- 属性的名字- 返回:
- object
-
getAnnotationValueValueWithDefaults
@Nonnull public static <T> T getAnnotationValueValueWithDefaults(Elements elementUtils, AnnotationMirror annotationMirror, String propertyName) 获取注解上的某一个属性的值,包含default值- 参数:
annotationMirror- 注解编译信息propertyName- 属性的名字- 返回:
- object
-
getAnnotationValuesMap
将注解属性转换为 name -> AnnotationValue 的Map -
getAnnotationValueTypeMirror
获取注解中引用的class对象的类型 eg:@AutoService(Processor.class) => AnnotationMirror(javax.annotation.processing.Processor) -
isSubTypeIgnoreTypeParameter
public static boolean isSubTypeIgnoreTypeParameter(Types typeUtil, TypeMirror first, TypeMirror second) 忽略两个类型的泛型参数,判断第一个是否是第二个的子类型。 如果考虑泛型的话,会存在区别 -
isSameTypeIgnoreTypeParameter
public static boolean isSameTypeIgnoreTypeParameter(Types typeUtil, TypeMirror first, TypeMirror second) 忽略两个类型的泛型参数,判断第一个和第二个类型是否相同 如果考虑泛型的话,会存在区别 -
findDeclaredType
获取一个元素的声明类型 -
getTypeElementOfClass
-
getTypeMirrorOfClass
-
isPrimitiveBoolean
是否是基本类型的boolean -
isArrayType
是否是数组类型 -
getComponentType
-
isByteArray
判断一个对象是否是字节数组 -
findFirstTypeParameter
获取第一个泛型参数 -
upwardToSuperTypeMirror
public static DeclaredType upwardToSuperTypeMirror(Types typeUtils, TypeMirror self, TypeMirror target) 将当前类型转型到目标超类型 用于获取当前类型传递给超类型的一些信息- 参数:
self- 当前类型target- 目标类型
-
getProxyClassName
public static String getProxyClassName(Elements elementUtils, TypeElement typeElement, String suffix) 根据原类型,生成获得对应的辅助类的类名 对于内部类,生成的类为:外部类名_内部类名- 参数:
suffix- 后缀
-
writeToFile
public static void writeToFile(TypeElement sourceElement, com.squareup.javapoet.TypeSpec.Builder typeBuilder, Elements elementUtils, Messager messager, Filer filer) - 参数:
sourceElement- 原始类文件,用于获取包名,以及打印错误
-
writeToFile
public static void writeToFile(Element sourceElement, com.squareup.javapoet.TypeSpec.Builder typeBuilder, String outPackage, Messager messager, Filer filer) - 参数:
outPackage- 输出的文件目录
-
getPackageName
获取一个类或接口所属的包名 -
getStackTrace
获取堆栈信息,避免引入commons-lang3 -
isEmpty
-
isBlank
-
isEmpty
-
isEmpty
-