如何调用泛型参数类型的方法,如SortedMap <integer,vector <double []>&gt; </integer,vector <double []>

时间:2012-01-26 20:02:33

标签: java generics reflection invoke

如何在对象中调用方法,该方法的泛型参数类型为SortedMap<Integer,Vector<double[]>>

// "c" is actually a class of mine that extends Thread
Class<?> cls = Class.forName(c.getClass().getName());

Method meth = cls.getMethod("add", new Class[]{??????});  // What do I put here?

meth.invoke(c, allData);

不确定要放入new Class[]{}数组中的内容。我试过了SortedMap.class,但这不起作用

2 个答案:

答案 0 :(得分:1)

Class.getMethod()这些天使用varags。你试过吗

 Method meth = cls.getMethod("add", Map.class);

答案 1 :(得分:0)

以下代码适用于我:


public class MyClass {

public void test(ArrayList<String> p) {
    System.out.append("INVOKED !!");
}

public static void main(String[] args) throws NoSuchMethodException, ClassNotFoundException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
    MyClass myClass = new MyClass();
    Class<?> klass = Class.forName("MyClass");

    Method m = klass.getMethod("test", ArrayList.class);
    m.invoke(myClass, new ArrayList<String>());
}

}