一家店不要说去一一回答,就算试着去读一读所有的攻击,我想这家店还不如关门去做其他生意。我尽我所能也尽我所知道的最好的办法去做,这样,即使别人再花费十倍的力气来说我做得不够好或是做错了,也是毫无用处的。——林肯
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
/**
* 泛型反射
*
* @author ningdalong
*
*/
public class GenericReflect extends ArrayList<Long> {
private static final long serialVersionUID = 1L;
public List<String> a;
public List<Integer> b(List<BigDecimal> a, Integer b) throws A {
// 参数反射
Type parameterType = a.getClass().getGenericSuperclass();
sys((ParameterizedType) parameterType);
// 本地变量反射
List<Integer> ints = new ArrayList<Integer>();
Type localType = ints.getClass().getGenericSuperclass();
sys((ParameterizedType) localType);
return ints;
}
public static void main(String[] args) throws NoSuchMethodException, SecurityException, NoSuchFieldException {
// 一下为声明解析都可以调用
extend(GenericReflect.class);
methodException(GenericReflect.class, "b", List.class, Integer.class);
methodReturn(GenericReflect.class, "b", List.class, Integer.class);
methodParameter(GenericReflect.class, "b", List.class, Integer.class);
field(GenericReflect.class, "a");
// 一下是运行时无法解析到
new GenericReflect().b(new ArrayList<BigDecimal>(), 1);
}
/**
* 类的父类泛型
*
* @param clazz
*/
public static void extend(Class<?> clazz) {
sys((ParameterizedType) clazz.getGenericSuperclass());
}
/**
* 异常的泛型 类的接口泛型
*
* @param clazz
* @param methodName
* @param parameterTypes
* @throws NoSuchMethodException
* @throws SecurityException
*/
public static void methodException(Class<?> clazz, String methodName, Class<?>... parameterTypes)
throws NoSuchMethodException, SecurityException {
Method method = clazz.getDeclaredMethod(methodName, parameterTypes);
Type type = method.getGenericExceptionTypes()[0];
// 样例中的第一个异常
Class<?> cl = (Class<?>) type;
// 实现的第一个接口
sys((ParameterizedType) cl.getGenericInterfaces()[0]);
}
/**
* 方法返回值的泛型
*
* @param clazz
* @param methodName
* @param parameterTypes
* @throws NoSuchMethodException
* @throws SecurityException
*/
public static void methodReturn(Class<?> clazz, String methodName, Class<?>... parameterTypes)
throws NoSuchMethodException, SecurityException {
Method method = clazz.getDeclaredMethod(methodName, parameterTypes);
sys((ParameterizedType) method.getGenericReturnType());
}
/**
* 方法参数的泛型
*
* @param clazz
* @param methodName
* @param parameterTypes
* @throws NoSuchMethodException
* @throws SecurityException
*/
public static void methodParameter(Class<?> clazz, String methodName, Class<?>... parameterTypes)
throws NoSuchMethodException, SecurityException {
Method method = clazz.getDeclaredMethod(methodName, parameterTypes);
// 样例中的第一个参数
sys((ParameterizedType) method.getGenericParameterTypes()[0]);
}
/**
* 字段的泛型
*
* @param clazz
* @param fieldName
* @throws NoSuchFieldException
* @throws SecurityException
*/
public static void field(Class<?> clazz, String fieldName) throws NoSuchFieldException, SecurityException {
Field field = clazz.getDeclaredField(fieldName);
sys((ParameterizedType) field.getGenericType());
}
public static void sys(ParameterizedType type) {
Type[] actualTypeArguments = type.getActualTypeArguments();
for (Type t : actualTypeArguments) {
if (t instanceof TypeVariable) {
System.out.println(((TypeVariable<?>) t).getName());
} else if (t instanceof Class) {
System.out.println(((Class<?>) t).getName());
} else {
System.out.println("没有匹配");
}
}
}
}
class A extends RuntimeException implements Iterable<GenericReflect> {
private static final long serialVersionUID = 1L;
@Override
public Iterator<GenericReflect> iterator() {
return null;
}
}