AnnotationProcessor之Element和TypeElement

Element

原文(摘自Java8的官方文档):
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).
Elements should be compared using the equals(Object) method. There is no guarantee that any particular element will always be represented by the same object.

To implement operations based on the class of an Element object, either use a visitor or use the result of the getKind() method. Using instanceof is not necessarily a reliable idiom for determining the effective class of an object in this modeling hierarchy since an implementation may choose to have a single object implement multiple Element subinterfaces.

译文:
代表一个程序元素,例如包、类或者方法。每一个元素代表一个静态的、语言层的构件(并且不是一个虚拟机的运行时构件)。
元素的比较应该使用equals方法。不能保证一个特定的元素总能被同样的对象代表。
要实现基于元素对象的类的操作,要么使用一个visitor,要么使用getKind()方法的返回值。在此建模层中使用instanceof决定一个对象的有效类不是可靠的语法,因为一个实现可能选择让一个对象实现多个元素子接口。

TypeElement

原文(摘自Java8的官方文档):
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.
While a TypeElement represents a class or interface element, a DeclaredType represents a class or interface type, the latter being a use (or invocation) of the former. The distinction is most apparent with generic types, for which a single element can define a whole family of types. For example, the element java.util.Set corresponds to the parameterized types java.util.Set and java.util.Set (and many others), and to the raw type java.util.Set.

Each method of this interface that returns a list of elements will return them in the order that is natural for the underlying source of program information. For example, if the underlying source of information is Java source code, then the elements will be returned in source code order.

译文:
TypeElement 代表一个类或接口程序元素,它提供了访问与其类型和成员相关的信息的方法。注意,枚举类型是一类class,而注解类型是一类interface。
当TypeElement代表一个类或接口元素,DeclaredType代表类或接口类型,后者成为前者的(use)使用(或调用)。两者的区别在泛型上是最明显的,对于泛型,单独的元素可以定义整个类型集。例如元素java.util.Set对应参数化类型java.util.Set、java.util.Set(以及许多其他类型)和根类型java.util.Set.
接口中每一个返回元素列表的的方法,都将按照程序信息的底层源码的自然顺序返回列表。例如,如果信息的底层源码是java源代码,这些元素则按源代码的顺序返回。


版权声明:本文为KevinsCSDN原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。