java得到父类的泛型_Java反射通过class获取父类泛型类型

但是,对于获取父类的泛型类型,还是会有办法的。在Class类中,有这样一个方法:

/**

* Returns the {@code Type} representing the direct superclass of

* the entity (class, interface, primitive type or void) represented by

* this {@code Class}.

*

*

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

* The Java™ Virtual Machine Specification

* @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();

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值