TypeVariable
TypeVariable是在反射中用来反映泛型类型参数的信息,在Spring的注解开发中会使用到。
下面是方法泛型参数的获取方式。
public class TypeVariableExample<T extends Object> {
public static void main(String[] args) {
// 返回类或接口声明的方法,包括: public protected 缺省的 private,但是不包括继承的
Method[] declaredMethods = TypeVariableExample.class.getDeclaredMethods();
for (Method declaredMethod : declaredMethods) {
if (declaredMethod.getName().equals("genericType")){
Class<?>[] parameterTypes = declaredMethod.getParameterTypes();
for (Class<?> parameterType : parameterTypes) {
System.out.println("class parameterType: " + parameterType.getName());
}
// 获取方法的所有参数
Parameter[] parameters = declaredMethod.getParameters();
for (Parameter parameter : parameters) {
if (parameter.getAnnotation(RequestBody.class) != null){
// 方法参数的类型
Type parameterizedType = parameter.getParameterizedType();
if (parameterizedType instanceof TypeVariable){
System.out.println("is TypeVariable");
}
if (parameter.getParameterizedType() instanceof ParameterizedType){
System.out.println("is ParameterizedType");
}
}
}
}
}
// 测试同一个类下的泛型是否相等
TypeVariableExample<String> stringType = new TypeVariableExample<>();
TypeVariableExample<User> userType = new TypeVariableExample<>();
Type stringParameterType = getParameterType(stringType);
Type userParameterType = getParameterType(userType);
if (stringParameterType != null && userParameterType != null){
System.out.println(stringParameterType.equals(userParameterType));
}
}
public void genericType(@RequestBody T t){
}
private static Type getParameterType(TypeVariableExample<?> example){
Method[] methods = example.getClass().getMethods();
for (Method method : methods) {
for (Parameter parameter : method.getParameters()) {
if (parameter.getAnnotation(RequestBody.class) != null){
return parameter.getParameterizedType();
}
}
}
return null;
}
}
注意:同一个类下的同一泛型参数对象是一样的
TypeVariable方法
- Type[] getBounds();返回此类型的上界
- D getGenericDeclaration();返回声明这个泛型声明类型变量的对象
- String getName();返回泛型的名称
- AnnotatedType[] getAnnotatedBounds();
泛型的使用方式
- 泛型类:public class User<T>
- 泛型方法:public <T> metho(T t);
- 泛型接口:public interface InterfaceTest <T>
- <?>无界泛型 <T extends Object>上界泛型为Object,只能是Object或其子类 <? super User> 下界泛型为User,只能是User或User父类型
ParameterizedType
反射获取获取泛型类型的实际参数信息。
public class ParameterizedTypeExample {
public static void main(String[] args) {
// 获取所有的公共方法,包括继承的
Method[] methods = ParameterizedTypeExample.class.getMethods();
for (Method method : methods) {
if (method.getName().equals("parameterizedType")){
Parameter[] parameters = method.getParameters();
for (Parameter parameter : parameters) {
if (parameter.getAnnotation(RequestBody.class) != null){
Class<?> type = parameter.getType();
System.out.println("parameter type: " + type.getName());
// 参数泛型类型判断
if (parameter.getParameterizedType() instanceof ParameterizedType){
System.out.println("is ParameterizedType");
}
if (parameter.getParameterizedType() instanceof TypeVariable){
System.out.println("is TypeVariable");
}
}
}
}
}
}
public void parameterizedType(@RequestBody List<String> list){
}
}
ParameterizedType
- Type[] getActualTypeArguments();泛型的实参
- Type getRawType();
- Type getOwnerType();