/** * Represents a program element such as a package, class, or method. * Each element represents a static, language-level construct * (and not, for example, a runtime construct of the virtual machine). */ publicinterfaceElementextendsjavax.lang.model.AnnotatedConstruct { // 转化为 TypeMirror TypeMirror asType();
// 用以获取节点的具体类型 ElementKind getKind();
// 持外部使用访问者模式进行访问操作 <R, P> R accept(ElementVisitor<R, P> v, P p);
Element 的类型包括:注解、类、接口、构造器、枚举、枚举值、异常参数、变量、初始化块、本地变量、方法、包、参数、资源变量、静态初始化块、泛型参数等
PackageElement:
1 2 3 4 5
/** * Represents a package program element. Provides access to information * about the package and its members. */ publicinterfacePackageElementextendsElement, QualifiedNameable {}
表示一个包程序元素。提供对有关包及其成员的信息的访问。
ExecutableElement:
1 2 3 4 5 6
/** * Represents a method, constructor, or initializer (static or * instance) of a class or interface, including annotation type * elements. */ publicinterfaceExecutableElementextendsElement, Parameterizable {}
表示某个类或接口的方法、构造方法或初始化程序(静态或实例),包括注释类型元素。
VariableElement:
1 2 3 4 5 6
/** * Represents a field, enum constant, method or constructor * parameter, local variable, resource variable, or exception * parameter. */ publicinterfaceVariableElementextendsElement {}
表示一个字段、enum 常量、方法或构造方法参数、局部变量或异常参数。
TypeElement:
1 2 3 4 5 6 7 8
/** * Represents a class or interface program element. Provides access * to information about the type and its members. Note that an enum * type is a kind of class and an annotation type is a kind of * interface. */ publicinterfaceTypeElementextendsElement, Parameterizable, QualifiedNameable {}
/** * Represents a formal type parameter of a generic class, interface, method, * or constructor element. * A type parameter declares a {@link TypeVariable}. */ publicinterfaceTypeParameterElementextendsElement {}
/** * Represents a type in the Java programming language. * Types include primitive types, declared types (class and interface types), * array types, type variables, and the null type. * Also represented are wildcard type arguments, * the signature and return types of executables, * and pseudo-types corresponding to packages and to the keyword {@code void}. */ publicinterfaceTypeMirrorextendsjavax.lang.model.AnnotatedConstruct { // 用以获取当前这个TypeMirror具体类型是什么 TypeKind getKind(); // 使用访问者模式进行类型转换后操作,其中R是Result结果类型,P是Parameter参数类型 <R, P> R accept(TypeVisitor<R, P> v, P p); }
TypeMirror 有如下实现类:
ArrayType:代表数组类型,可通过 API 获取元数据类型;
DeclaredType:声明类型,即类或接口。可以通过 asElement()和后面的 Element 进行转换;