先科普下,泛型是Java SE 1.5的新特性,泛型的本质是参数化类型,也就是说所操作的数据类型被指定为一个参数。这种参数类型可以用在类、接口和方法的创建中,分别称为泛型类、泛型接口、泛型方法。 Java语言引入泛型的好处是安全简单。
在Java SE 1.5之前,没有泛型的情况的下,通过对类型Object的引用来实现参数的“任意化”,“任意化”带来的缺点是要做显式的强制类型转换,而这种转换是要求开发者对实际参数类型可以预知的情况下进行的。对于强制类型转换错误的情况,编译器可能不提示错误,在运行的时候才出现异常,这是一个安全隐患。
泛型的好处是在编译的时候检查类型安全,并且所有的强制转换都是自动和隐式的,以提高代码的重用率。
那么问题来了,如何通过java反射获取实现类中的具体泛型呢,先来看几段代码:
public class TestFacade extends BaseFacade<PersonVO> {
@Override
protected BizAppService<PersonVO> getAppService() {
return null;
}
}
public abstract class BaseFacade<T extends BaseVO> {
/** 日志记录 */
private static final Logger LOGGER = LoggerFactory.getLogger(BaseFacade.class);
protected abstract BizAppService<T> getAppService();
}
我们如何TestFacade具体泛型实现类,查看jdk Class源码可知Class的getGenericSuperclass方法能返回带泛型的具体Type
/**
* Returns the {@code Type} representing the direct superclass of
* the entity (class, interface, primitive type or void) represented by
* this {@code Class}.
*
* <p>If the superclass is a parameterized type, the {@code Type}
* object returned must accurately reflect the actual type
* parameters used in the source code. The parameterized type
* representing the superclass is created if it had not been
* created before. See the declaration of {@link
* java.lang.reflect.ParameterizedType ParameterizedType} for the
* semantics of the creation process for parameterized types. If
* this {@code Class} represents either the {@code Object}
* class, an interface, a primitive type, or void, then null is
* returned. If this object represents an array class then the
* {@code Class} object representing the {@code Object} class is
* returned.
*
* @throws java.lang.reflect.GenericSignatureFormatError if the generic
* class signature does not conform to the format specified in
* <cite>The Java™ Virtual Machine Specification</cite>
* @throws TypeNotPresentException if the generic superclass
* refers to a non-existent type declaration
* @throws java.lang.reflect.MalformedParameterizedTypeException if the
* generic superclass refers to a parameterized type that cannot be
* instantiated for any reason
* @return the superclass of the class represented by this object
* @since 1.5
*/
public Type getGenericSuperclass() {
if (getGenericSignature() != null) {
// Historical irregularity:
// Generic signature marks interfaces with superclass = Object
// but this API returns null for interfaces
if (isInterface())
return null;
return getGenericInfo().getSuperclass();
} else
return getSuperclass();
}
再查看JDK源码ParameterizedType接口可知该接口是继承Type接口的,查看源码注释可知该接口中的getActualTypeArguments方法可以返回实际泛型的具体类型数组,
/**
* 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();
所以我们找到了方法通过如下实现即可取得泛型实际类型:
public class Demo {
/**
* @param args
* xx
*/
public static void main(String[] args) {
//
ParameterizedType type = (ParameterizedType) TestFacade.class.getGenericSuperclass();
Class<?> cls = (Class<?>) type.getActualTypeArguments()[0];
System.out.println("generic type:" + cls);
}
}