程序包 cn.wjybxx.apt

类 AptUtils

java.lang.Object
cn.wjybxx.apt.AptUtils

public class AptUtils extends Object
作者:
wjybxx date 2023/4/6
  • 字段详细资料

    • SOURCE_VERSION

      public static final SourceVersion SOURCE_VERSION
    • PUBLIC_STATIC

      public static final Modifier[] PUBLIC_STATIC
    • PUBLIC_STATIC_FINAL

      public static final Modifier[] PUBLIC_STATIC_FINAL
    • PRIVATE_STATIC_FINAL

      public static final Modifier[] 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

      public static final Map<String,com.squareup.javapoet.TypeName> primitiveTypeNameMap
    • boxedTypeNameMap

      public static final Map<String,com.squareup.javapoet.TypeName> 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

      public static com.squareup.javapoet.ClassName classNameOfCanonicalName(String cname)
      参数:
      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

      public static ExecutableElement findMethodByName(TypeElement typeElement, String methodName)
      通过名字查找对应的方法
    • findFieldByName

      public static VariableElement findFieldByName(TypeElement typeElement, String field)
      通过名字查找对应的字段
    • flatInherit

      public static List<TypeElement> flatInherit(TypeElement typeElement)
      将继承体系展开,不包含实现的接口。 (超类在后)
    • flatInheritAndReverse

      public static List<TypeElement> flatInheritAndReverse(TypeElement typeElement)
      将继承体系展开,并逆序返回,不包含实现的接口。 (超类在前)
    • findAllInterfaces

      public static List<? extends 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

      public static Map<String,AnnotationValue> getAnnotationValuesMap(AnnotationMirror annotationMirror)
      将注解属性转换为 name -> AnnotationValue 的Map
    • getAnnotationValueTypeMirror

      public static TypeMirror getAnnotationValueTypeMirror(AnnotationValue annotationValue)
      获取注解中引用的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

      @Nullable public static DeclaredType findDeclaredType(TypeMirror typeMirror)
      获取一个元素的声明类型
    • getTypeElementOfClass

      public static TypeElement getTypeElementOfClass(Elements elementUtils, Class<?> clazz)
    • getTypeMirrorOfClass

      public static TypeMirror getTypeMirrorOfClass(Elements elementUtils, Class<?> clazz)
    • isPrimitiveNumber

      public static boolean isPrimitiveNumber(TypeMirror typeMirror)
    • getRootComponentType

      public static TypeMirror getRootComponentType(ArrayType arrayType)
    • getArrayRank

      public static int getArrayRank(ArrayType arrayType)
    • isByteArray

      public static boolean isByteArray(TypeMirror typeMirror)
    • findFirstTypeParameter

      @Nullable public static TypeMirror findFirstTypeParameter(TypeMirror typeMirror)
    • 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

      public static String getPackageName(TypeElement typeElement, Elements elementUtils)
      获取类型所在的包名
    • getStackTrace

      public static String getStackTrace(Throwable throwable)
      获取堆栈信息,避免引入commons-lang3
    • isEmpty

      public static boolean isEmpty(String v)
    • isBlank

      public static boolean isBlank(String v)
    • isEmpty

      public static boolean isEmpty(Collection<?> c)
    • isEmpty

      public static boolean isEmpty(Map<?,?> c)