Java-Type
Type是Java 编程语言中所有类型的公共高级接口(官方解释),也就是Java中所有类型的“爹”;其中,“所有类型”的描述尤为值得关注。它并不是我们平常工作中经常使用的 int、String、List、Map等数据类型,而是从Java语言角度来说,对基本类型、引用类型向上的抽象;
Type体系中类型的包括:原始类型(Class)、参数化类型(ParameterizedType)、数组类型(GenericArrayType)、类型变量(TypeVariable)、基本类型(Class);
原始类型,不仅仅包含我们平常所指的类,还包括枚举、数组、注解等;
参数化类型,就是我们平常所用到的泛型List、Map;
数组类型,并不是我们工作中所使用的数组String[] 、byte[],而是带有泛型的数组,即T[] ;
基本类型,也就是我们所说的java的基本类型,即int,float,double等
package java.lang.reflect;
/**
* Type is the common superinterface for all types in the Java
* programming language. These include raw types, parameterized types,
* array types, type variables and primitive types.
*
* @since 1.5
*/
public interface Type {
/**
* Returns a string describing this type, including information
* about any type parameters.
*
* @implSpec The default implementation calls {@code toString}.
*
* @return a string describing this type
* @since 1.8
*/
default String getTypeName() {
return toString();
}
}
查看源码,Type接口下共有4个"儿子",每一个“儿子”代表着Java中的一种类型;
1.ParameterizedType
参数化类型,即泛型;例如:List、Map<K,V>等带有参数化的对象;
注意指的是对象本身
在ParameterizedType接口中,有3个方法,分别是getActualTypeArguments()、 getRawType()、 getOwnerType();
1.1 getActualTypeArguments
获取泛型中的实际类型,可能会存在多个泛型,例如Map<K,V>,所以会返回Type[]数组;
值得注意的是,无论<>中有几层嵌套(List<Map<String,Integer>),getActualTypeArguments()方法永远都是脱去最外层的<>(也就是List<>),将口号内的内容(Map<String,Integer>)返回;
我们经常遇到的List,通过getActualTypeArguments()方法,得到的返回值是TypeVariableImpl对象,也就是TypeVariable类型(后面介绍);
1.2 getRawType
获取声明泛型的类或者接口,也就是泛型中<>前面的那个值;
1.3 getOwnerType
通过方法的名称,我们大概了解到,此方法是获取泛型的拥有者,那么拥有者是个什么意思?
Returns a {@code Type} object representing the type that this type * is a member of. For example, if this type is {@code O.I}, * return a representation of {@code O}. (摘自JDK注释)
通过注解,我们得知,“拥有者”表示的含义–内部类的“父类”,通过getOwnerType()方法可以获取到内部类的“拥有者”;例如: Map 就是 Map.Entry<String,String>的拥有者;
/*
* Copyright (c) 2003, 2004, Oracle and/or its affiliates. All rights reserved.
* ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
*/
package java.lang.reflect;
/**
* ParameterizedType represents a parameterized type such as
* Collection<String>.
*
* <p>A parameterized type is created the first time it is needed by a
* reflective method, as specified in this package. When a
* parameterized type p is created, the generic type declaration that
* p instantiates is resolved, and all type arguments of p are created
* recursively. See {@link java.lang.reflect.TypeVariable
* TypeVariable} for details on the creation process for type
* variables. Repeated creation of a parameterized type has no effect.
*
* <p>Instances of classes that implement this interface must implement
* an equals() method that equates any two instances that share the
* same generic type declaration and have equal type parameters.
*
* @since 1.5
*/
public interface ParameterizedType extends Type {
/**
* Returns an array of {@code Type} objects representing the actual type
* arguments to this type.
*
* <p>Note that in some cases, the returned array be empty. This can occur
* if this type represents a non-parameterized type nested within
* a parameterized type.
*
* @return an array of {@code Type} objects representing the actual type
* arguments to this type
* @throws TypeNotPresentException if any of the
* actual type arguments refers to a non-existent type declaration
* @throws MalformedParameterizedTypeException if any of the
* actual type parameters refer to a parameterized type that cannot
* be instantiated for any reason
* @since 1.5
*/
Type[] getActualTypeArguments();
/**
* Returns the {@code Type} object representing the class or interface
* that declared this type.
*
* @return the {@code Type} object representing the class or interface
* that declared this type
* @since 1.5
*/
Type getRawType();
/**
* Returns a {@code Type} object representing the type that this type
* is a member of. For example, if this type is {@code O<T>.I<S>},
* return a representation of {@code O<T>}.
*
* <p>If this type is a top-level type, {@code null} is returned.
*
* @return a {@code Type} object representing the type that
* this type is a member of. If this type is a top-level type,
* {@code null} is returned
* @throws TypeNotPresentException if the owner type
* refers to a non-existent type declaration
* @throws MalformedParameterizedTypeException if the owner type
* refers to a parameterized type that cannot be instantiated
* for any reason
* @since 1.5
*/
Type getOwnerType();
}
2.TypeVariable
类型变量,即泛型中的变量;例如:T、K、V等变量,可以表示任何类;在这需要强调的是,TypeVariable代表着泛型中的变量,而ParameterizedType则代表整个泛型;
在TypeVariable接口中,有3个方法,分别为getBounds()、getGenericDeclaration()、getName();
2.1 getBounds
获得该类型变量的上限,也就是泛型中extend右边的值;例如 List ,Number就是类型变量T的上限;如果我们只是简单的声明了List(无显式定义extends),那么默认为Object;
值得注意的是,类型变量的上限可以为多个,必须使用&符号相连接,例如 List<T extends Number & Serializable>;其中,& 后必须为接口;
2.2 getGenericDeclaration
获取声明该类型变量实体,也就是TypeVariableTest中的TypeVariableTest;
2.3 getName
获取类型变量在源码中定义的名称;
说到TypeVariable类,就不得不提及Java-Type体系中另一个比较重要的接口—GenericDeclaration;含义为:声明类型变量的所有实体的公共接口;也就是说该接口定义了哪些地方可以定义类型变量(泛型);
通过查看源码发现,GenericDeclaration下有三个子类,分别为Class、Method、Constructor;也就是说,我们定义泛型只能在一个类中这3个地方自定义泛型;
/*
* Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved.
* ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
*/
package java.lang.reflect;
/**
* TypeVariable is the common superinterface for type variables of kinds.
* A type variable is created the first time it is needed by a reflective
* method, as specified in this package. If a type variable t is referenced
* by a type (i.e, class, interface or annotation type) T, and T is declared
* by the nth enclosing class of T (see JLS 8.1.2), then the creation of t
* requires the resolution (see JVMS 5) of the ith enclosing class of T,
* for i = 0 to n, inclusive. Creating a type variable must not cause the
* creation of its bounds. Repeated creation of a type variable has no effect.
*
* <p>Multiple objects may be instantiated at run-time to
* represent a given type variable. Even though a type variable is
* created only once, this does not imply any requirement to cache
* instances representing the type variable. However, all instances
* representing a type variable must be equal() to each other.
* As a consequence, users of type variables must not rely on the identity
* of instances of classes implementing this interface.
*
* @param <D> the type of generic declaration that declared the
* underlying type variable.
*
* @since 1.5
*/
public interface TypeVariable<D extends GenericDeclaration> extends Type, AnnotatedElement {
/**
* Returns an array of {@code Type} objects representing the
* upper bound(s) of this type variable. Note that if no upper bound is
* explicitly declared, the upper bound is {@code Object}.
*
* <p>For each upper bound B: <ul> <li>if B is a parameterized
* type or a type variable, it is created, (see {@link
* java.lang.reflect.ParameterizedType ParameterizedType} for the
* details of the creation process for parameterized types).
* <li>Otherwise, B is resolved. </ul>
*
* @throws TypeNotPresentException if any of the
* bounds refers to a non-existent type declaration
* @throws MalformedParameterizedTypeException if any of the
* bounds refer to a parameterized type that cannot be instantiated
* for any reason
* @return an array of {@code Type}s representing the upper
* bound(s) of this type variable
*/
Type[] getBounds();
/**
* Returns the {@code GenericDeclaration} object representing the
* generic declaration declared this type variable.
*
* @return the generic declaration declared for this type variable.
*
* @since 1.5
*/
D getGenericDeclaration();
/**
* Returns the name of this type variable, as it occurs in the source code.
*
* @return the name of this type variable, as it appears in the source code
*/
String getName();
/**
* Returns an array of AnnotatedType objects that represent the use of
* types to denote the upper bounds of the type parameter represented by
* this TypeVariable. The order of the objects in the array corresponds to
* the order of the bounds in the declaration of the type parameter.
*
* Returns an array of length 0 if the type parameter declares no bounds.
*
* @return an array of objects representing the upper bounds of the type variable
* @since 1.8
*/
AnnotatedType[] getAnnotatedBounds();
}
3.GenericArrayType
泛型数组类型,用来描述ParameterizedType、TypeVariable类型的数组;即List[] 、T[]等;
/*
* Copyright (c) 2003, 2004, Oracle and/or its affiliates. All rights reserved.
* ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
*/
package java.lang.reflect;
/**
* {@code GenericArrayType} represents an array type whose component
* type is either a parameterized type or a type variable.
* @since 1.5
*/
public interface GenericArrayType extends Type {
/**
* Returns a {@code Type} object representing the component type
* of this array. This method creates the component type of the
* array. See the declaration of {@link
* java.lang.reflect.ParameterizedType ParameterizedType} for the
* semantics of the creation process for parameterized types and
* see {@link java.lang.reflect.TypeVariable TypeVariable} for the
* creation process for type variables.
*
* @return a {@code Type} object representing the component type
* of this array
* @throws TypeNotPresentException if the underlying array type's
* component type refers to a non-existent type declaration
* @throws MalformedParameterizedTypeException if the
* underlying array type's component type refers to a
* parameterized type that cannot be instantiated for any reason
*/
Type getGenericComponentType();
}
4.Class
上三者不同,Class是Type的一个实现类,属于原始类型,是Java反射的基础,对Java类的抽象;
在程序运行期间,每一个类都对应一个Class对象,这个对象包含了类的修饰符、方法,属性、构造等信息,所以我们可以对这个Class对象进行相应的操作,这就是Java的反射;
5.WildcardType
泛型表达式(或者通配符表达式),即? extend Number、? super Integer这样的表达式;WildcardType虽然是Type的子接口,但却不是Java类型中的一种;
/*
* Copyright (c) 2003, 2004, Oracle and/or its affiliates. All rights reserved.
* ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
*/
package java.lang.reflect;
/**
* WildcardType represents a wildcard type expression, such as
* {@code ?}, {@code ? extends Number}, or {@code ? super Integer}.
*
* @since 1.5
*/
public interface WildcardType extends Type {
/**
* Returns an array of {@code Type} objects representing the upper
* bound(s) of this type variable. Note that if no upper bound is
* explicitly declared, the upper bound is {@code Object}.
*
* <p>For each upper bound B :
* <ul>
* <li>if B is a parameterized type or a type variable, it is created,
* (see {@link java.lang.reflect.ParameterizedType ParameterizedType}
* for the details of the creation process for parameterized types).
* <li>Otherwise, B is resolved.
* </ul>
*
* @return an array of Types representing the upper bound(s) of this
* type variable
* @throws TypeNotPresentException if any of the
* bounds refers to a non-existent type declaration
* @throws MalformedParameterizedTypeException if any of the
* bounds refer to a parameterized type that cannot be instantiated
* for any reason
*/
Type[] getUpperBounds();
/**
* Returns an array of {@code Type} objects representing the
* lower bound(s) of this type variable. Note that if no lower bound is
* explicitly declared, the lower bound is the type of {@code null}.
* In this case, a zero length array is returned.
*
* <p>For each lower bound B :
* <ul>
* <li>if B is a parameterized type or a type variable, it is created,
* (see {@link java.lang.reflect.ParameterizedType ParameterizedType}
* for the details of the creation process for parameterized types).
* <li>Otherwise, B is resolved.
* </ul>
*
* @return an array of Types representing the lower bound(s) of this
* type variable
* @throws TypeNotPresentException if any of the
* bounds refers to a non-existent type declaration
* @throws MalformedParameterizedTypeException if any of the
* bounds refer to a parameterized type that cannot be instantiated
* for any reason
*/
Type[] getLowerBounds();
// one or many? Up to language spec; currently only one, but this API
// allows for generalization.
}
以上,简单介绍了Java-Type的体系;为了解决泛型,JDK1.5版本开始引入Type接口;在此之前,Java中只有原始类型,所有的原始类型都是通过Class进行抽象;有了Type以后,Java的数据类型得到了扩展,从原始类型扩展为参数化类型(ParameterizedType)、数组类型(GenericArrayType)、类型变量(TypeVariable);