本文我们介绍一下javax.lang.model.type.类不多,如图:
至于为啥要看这部分的代码,原因很简单,javac 实现了javax.lang.model中的API.
该类是一个媒介,定义了java中各种类型所对应的枚举.
public enum TypeKind {
/**
* The primitive type {@code boolean}.
* boolean的原始类型
*/
BOOLEAN,
/**
* The primitive type {@code byte}.
* byte的原始类型
*/
BYTE,
/**
* The primitive type {@code short}.
* short的原始类型
*/
SHORT,
/**
* The primitive type {@code int}.
* int的原始类型
*/
INT,
/**
* The primitive type {@code long}.
* long的原始类型
*/
LONG,
/**
* The primitive type {@code char}.
* char的原始类型
*/
CHAR,
/**
* The primitive type {@code float}.
* float的原始类型
*/
FLOAT,
/**
* The primitive type {@code double}.
* double的原始类型
*/
DOUBLE,
/**
* The pseudo-type corresponding to the keyword {@code void}.
* @see NoType
* 代表void的伪类型
*/
VOID,
/**
* A pseudo-type used where no actual type is appropriate.
* @see NoType
* 代表没有合适的类型与之对应的伪类型
*/
NONE,
/**
* The null type.
* 代表null的类型
*/
NULL,
/**
* An array type.
*/
ARRAY,
/**
* A class or interface type.
* 代表类或者接口的类型
*/
DECLARED,
/**
* A class or interface type that could not be resolved.
* 代表无法进行解析的类或接口类型
*/
ERROR,
/**
* A type variable.
* 类型变量
* 如一个类声明如下: C ,那么T,S就是类型变量
*/
TYPEVAR,
/**
* A wildcard type argument.
* 通配符.如:
* ?,? extends Number,? super T
*/
WILDCARD,
/**
* A pseudo-type corresponding to a package element.
* @see NoType
* 代表包的伪类型
*/
PACKAGE,
/**
* A method, constructor, or initializer.
* 代表方法,构造器,初始代码块
*/
EXECUTABLE,
/**
* An implementation-reserved type.
* This is not the type you are looking for.
* 保留类型
*/
OTHER,
/**
* A union type.
* 联合类型
* jdk1.7中的 try multi-catch 中异常的参数就是联合类型
* @since 1.7
*/
UNION,
/**
* An intersection type.
* 交集类型
* 如一个类有泛型参数,如,那么T extends Number & Runnable 就是交集类型
*
* @since 1.8
*/
INTERSECTION;
/**
* Returns {@code true} if this kind corresponds to a primitive
* type and {@code false} otherwise.
* @return {@code true} if this kind corresponds to a primitive type
* 判断是否是原生类型
*/
public boolean isPrimitive() {
switch(this) {
case BOOLEAN:
case BYTE:
case SHORT:
case INT:
case LONG:
case CHAR:
case FLOAT:
case DOUBLE:
return true;
default:
return false;
}
}
}
其中, TYPEVAR, WILDCARD, UNION, INTERSECTION 比较难理解,详细说明在代码的注释中,这里就不在赘述了.
代表了java编程语言中的类型,包括原生类型,声明类型(类和接口),数组类型,类型变量,null类型.同时也包括通配符,可执行语句的签名和返回类型,包和void所对应的伪类型.
public interface TypeMirror extends javax.lang.model.AnnotatedConstruct {
/**
* Returns the {@code kind} of this type.
* 返回该类所对应的TypeKind
*
* @return the kind of this type
*/
TypeKind getKind();
/**
* Obeys the general contract of {@link Object#equals Object.equals}.
* This method does not, however, indicate whether two types represent
* the same type.
* Semantic comparisons of type equality should instead use
* {@link Types#isSameType(TypeMirror, TypeMirror)}.
* The results of {@code t1.equals(t2)} and
* {@code Types.isSameType(t1, t2)} may differ.
* 满足 Object#equals方法的语义,然而,本方法并不代表2个类型是同样的.语义上比较2个类 * 型是否相同应该使用Types#isSameType(TypeMirror, TypeMirror)方法,该方法的返回 * 值可能和Types.isSameType(t1, t2)的返回值不一样.
* @param obj the object to be compared with this type
* @return {@code true} if the specified object is equal to this one
*/
boolean equals(Object obj);
/**
* Obeys the general contract of {@link Object#hashCode Object.hashCode}.
* 按照Object#hashCode的语义来实现
* @see #equals
*/
int hashCode();
/**
* Returns an informative string representation of this type. If
* possible, the string should be of a form suitable for
* representing this type in source code. Any names embedded in
* the result are qualified if possible.
* 返回此类型的字符串表示形式。如果可能的话,字符串应该是适合于在源代码中表示这种类型的形式。如果可能,嵌入在结果中的任何名称都是合格的。
* @return a string representation of this type
*/
String toString();
/**
* Applies a visitor to this type.
*
* @param the return type of the visitor's methods
* @param the type of the additional parameter to the visitor's methods
* @param v the visitor operating on this type
* @param p additional parameter to the visitor
* @return a visitor-specified result
*/
R accept(TypeVisitor v, P p);
}
该类的子接口定义如下:
代表可执行的类型,如方法,构造器,初始代码块.可执行文件表示为某种引用类型的方法(或构造函数或初始化器)。如果引用类型是参数化的,那么它的实际类型参数被替换为该接口的方法返回的任何类型.
代码如下:
public interface ExecutableType extends TypeMirror {
/**
* Returns the type variables declared by the formal type parameters
* of this executable.
* 返回该类型的类型变量.
*
* @return the type variables declared by the formal type parameters,
* or an empty list if there are none
* 返回该类型的类型变量,或者空集合,如果该类型不是泛型的
*/
List getTypeVariables();
/**
* Returns the return type of this executable.
* Returns a {@link NoType} with kind {@link TypeKind#VOID VOID}
* if this executable is not a method, or is a method that does not
* return a value.
* 返回该类型的返回类型。当该类型不是方法,或者该方法没有返回值时,则该方法返回的是
* NoType和TypeKind#VOID构成的TypeMirror
*
* @return the return type of this executable
*/
TypeMirror getReturnType();
/**
* Returns the types of this executable's formal parameters.
* 返回该类型的形式参数,如果没有的话,返回空集合
* @return the types of this executable's formal parameters,
* or an empty list if there are none
*/
List getParameterTypes();
/**
* Returns the receiver type of this executable,
* or {@link javax.lang.model.type.NoType NoType} with
* kind {@link javax.lang.model.type.TypeKind#NONE NONE}
* if the executable has no receiver type.
* 返回该类型的接收器类型,如果该类型没有接收器,则返回NoType,TypeKind#NONE 构成的
* TypeMirror
*
* An executable which is an instance method, or a constructor of an
* inner class, has a receiver type derived from the {@linkplain
* ExecutableElement#getEnclosingElement declaring type}.
* 一个实例方法,或者是内部类的构造器,有接收器类型。可以从xecutableElement#getEnclosingElement 获得
* An executable which is a static method, or a constructor of a
* non-inner class, or an initializer (static or instance), has no
* receiver type.
* 静态方法,不是内部类的构造器,或者是初始代码块(静态或者实例的),没有接收器类型
*
* @return the receiver type of this executable
* @since 1.8
*/
TypeMirror getReceiverType();
/**
* Returns the exceptions and other throwables listed in this
* executable's {@code throws} clause.
* 返回该类型的异常声明列表
* @return the exceptions and other throwables listed in this
* executable's {@code throws} clause,
* or an empty list if there are none.
*/
List getThrownTypes();
}
关于接收器类型,在JLS P150 有说明,现摘抄如下:
在实例方法中,接收器参数的类型必须是声明该方法的类或接口,而接收器的名字必须是this,否则,会产生编译时错误
在内部类构造器中,接收器参数的类型必须是作为该内部类的直接包围类型声明的类或接口,而接收器参数的名字必须是Identifier.this.其中,Identifire是作为该内部类的直接包围类型声明的类或接口的简单名,否则,就会产生编译时错误.
实例代码如下:
class Test{
Test(){
// 没有接收器参数
}
void m(Test this){
// 允许
}
static void n(Test this){
// 错误: 静态方法中,不允许有接收器参数
}
}
代表交集类型.
交集类型可以隐式或显式地声明在程序中。例如,类型参数
public interface IntersectionType extends TypeMirror {
/**
* Return the bounds comprising this intersection type.
* 获得对应的交集类型
* @return the bounds of this intersection types.
*/
List getBounds();
}
在没有实际类型合适的情况下使用的伪类型.其情况如下:
代表原始类型,包括 boolean,byte,short,int,long,char.float,double
代码如下:
public interface PrimitiveType extends TypeMirror {
}
代表引用类型,包括类和接口,数组类型,类型变量,null.代表如下:
public interface ReferenceType extends TypeMirror {
}
代表数组类型,多为数组类型是一个数组类型其组件类型还是数组类型.代码如下:
public interface ArrayType extends ReferenceType {
/**
* Returns the component type of this array type.
* 返回该数组类型对应的组件类型
* @return the component type of this array type
*/
TypeMirror getComponentType();
}
代表声明类型–> 类或者接口类型. 它也可以拥有泛型参数,如 java.util.Set\
public interface DeclaredType extends ReferenceType {
/**
* Returns the element corresponding to this type.
* 返回该类型对应的element
*
* @return the element corresponding to this type
*/
Element asElement();
/**
* Returns the type of the innermost enclosing instance or a
* {@code NoType} of kind {@code NONE} if there is no enclosing
* instance. Only types corresponding to inner classes have an
* enclosing instance.
* 返回最内层包围实例的类型,如果没有包围实例则返回NoType和NONE对应的TypeMirror.
* 只有对应于内部类的类型才有包围实例
*
* @return a type mirror for the enclosing type
* @jls 8.1.3 Inner Classes and Enclosing Instances
* @jls 15.9.2 Determining Enclosing Instances
*/
TypeMirror getEnclosingType();
/**
* Returns the actual type arguments of this type.
* For a type nested within a parameterized type
* (such as {@code Outer.Inner}), only the type
* arguments of the innermost type are included.
* 返回该类型的泛型参数
*
* @return the actual type arguments of this type, or an empty list
* if none
*/
List getTypeArguments();
}
代表null.代码如下:
public interface NullType extends ReferenceType {
}
代表一个通配符类型.包括:
通配符的上界可通过extends 语句来进行设置,下界可通过super 来进行设置.代码如下:
public interface WildcardType extends TypeMirror {
/**
* Returns the upper bound of this wildcard.
* If no upper bound is explicitly declared,
* {@code null} is returned.
* 返回该类型的上界,如果没有的话,则返回null
*
* @return the upper bound of this wildcard
*/
TypeMirror getExtendsBound();
/**
* Returns the lower bound of this wildcard.
* If no lower bound is explicitly declared,
* {@code null} is returned.
* 返回该类型的下界,如果没有的话,则返回null
* @return the lower bound of this wildcard
*/
TypeMirror getSuperBound();
}