http://www.javaeye.com/topic/21933 这里也有人问,遗憾的是 Class有一个getTypeParameters方法,也许可以。 http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Class.html#getTypeParameters() public TypeVariable<Class<T>>[] getTypeParameters() Returns an array of TypeVariable objects that represent the type variables declared by the generic declaration represented by this GenericDeclaration object, in declaration order. Returns an array of length 0 if the underlying generic declaration declares no type variables. TypeVariable可以获得Type。 Type是否能够subcast成Class,就不是很清楚了。 试过叻,取得的是K和V,完全无用
import java.lang.reflect.Field; import java.util.HashMap; import java.util.List;public class FieldSpy<T> { public boolean[][] b = { { false, false }, { true, true } }; public String name = "Alice"; public List<Integer> list; public HashMap<String, Integer> hashmap; public T val; public static void main(String... args) { try { Class<?> c = Class.forName("FieldSpy"); Field f = c.getField("hashmap"); System.out.format("Type: %s%n", f.getType()); System.out.format("GenericType: %s%n", f.getGenericType()); // production code should handle these exceptions more gracefully } catch (ClassNotFoundException x) { x.printStackTrace(); } catch (NoSuchFieldException x) { x.printStackTrace(); } } }
import java.lang.reflect.Method; import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; import java.util.HashMap;public class Main { public static void main(String[] args) throws SecurityException, NoSuchMethodException { HashMap<String, Integer> map = new HashMap<String, Integer>();
这里也有人问,遗憾的是
Class有一个getTypeParameters方法,也许可以。 http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Class.html#getTypeParameters() public TypeVariable<Class<T>>[] getTypeParameters() Returns an array of TypeVariable objects that represent the type variables declared by the generic declaration represented by this GenericDeclaration object, in declaration order. Returns an array of length 0 if the underlying generic declaration declares no type variables. TypeVariable可以获得Type。
Type是否能够subcast成Class,就不是很清楚了。
试过叻,取得的是K和V,完全无用
传入参数就是个Class类型的。
输出是他范式里的类。比如HashMap<String, Integer> 输出 String, Integer
ArrayList<String> 输出 String
这个例子我试了拿到都是java.lang.Object 类型
因为Java泛型的类型信息在编译时已经被JVM擦除了,其类型无法在运行时通过反射获得,Java泛型的作用在于提示编译器进行类型检查。
lz可以换个思路解决问题。
import java.util.HashMap;
import java.util.List;public class FieldSpy<T> {
public boolean[][] b = { { false, false }, { true, true } };
public String name = "Alice";
public List<Integer> list;
public HashMap<String, Integer> hashmap;
public T val; public static void main(String... args) {
try {
Class<?> c = Class.forName("FieldSpy");
Field f = c.getField("hashmap");
System.out.format("Type: %s%n", f.getType());
System.out.format("GenericType: %s%n", f.getGenericType()); // production code should handle these exceptions more gracefully
} catch (ClassNotFoundException x) {
x.printStackTrace();
} catch (NoSuchFieldException x) {
x.printStackTrace();
}
}
}
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.HashMap;public class Main {
public static void main(String[] args) throws SecurityException, NoSuchMethodException {
HashMap<String, Integer> map = new HashMap<String, Integer>();
//通过变量map无法获得HashMap中的泛型的具体类型,但是当把它传给一个方法后,
//通过该方法就可以得到泛型的实际类型,所以下面随便定义个方法applyHashMap,
//其参数是map,在方法getType中得到泛型的具体类型 getType();
}
public static void applyHashMap(HashMap<String, Integer> map) {
//该方法可以为空
}
/*
* 在该方法中得到泛型的实际类型
*/
public static void getType() throws SecurityException, NoSuchMethodException {
Method m = Main.class.getMethod("applyHashMap", HashMap.class);
Type[] type = m.getGenericParameterTypes();
for(int i=0; i<type.length; i++) {
ParameterizedType pt = (ParameterizedType)type[i];
Type[] temp = pt.getActualTypeArguments();
for(int j=0; j<temp.length; j++) {
System.out.println(temp[j]);
}
}
}
}这段代码能够得够得到结果,不知道能不能满足楼主的需求!
我也看到了。不过楼上的代码确实对我有用了。 可以继续下面的工作了,结贴。
我自己写的代码就在楼上基础改的,我想写的那种函数※1估计写不出来的。※1那种函数
指 /**
* 取得分析目标类所定义的泛型的具体类名
* 例:
* 调用
* getParameterizedNames(HashMap<String, Integer>.class)
* 返回一个ArrayList内容为
* java.lang.String
* java.lang.Ineger
*
* @param clazz 分析目标类
* @return 分析目标类所定义的泛型的具体类名
*/
public static ArrayList<String> getParameterizedNames(Class clazz)