Java-如何创建Class< Map< Object,List< Object>>>。目的 [英] Java - How to create a Class<Map<Object,List<Object>>> object

查看:72
本文介绍了Java-如何创建Class< Map< Object,List< Object>>>。目的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很抱歉,这是一个菜鸟问题。我有一个方法,该方法采用具有通用类型的Class对象作为参数。我可以执行以下操作:

I'm sorry if this is a noob question. I have a method which takes a Class object with the generic type as a parameter. I am able to do the following:

Class cs = Map.class;

但是当我通过此方法时,由于声明了该方法,因此我收到 NoSuchMethodException:

but when I pass this I get an "NoSuchMethodException" since the method is declared:

public void doSomething(Class<Map<Object, List<Object>>> theclass){
...     
}

我试图将其投射为这样:

I have tried to cast it like this:

Class cs = (Class<Map<Object, List<Object>>>)(Class<?>)Map.class;

但我仍然遇到相同的例外情况。

but I still get the same exception.

有没有办法做到这一点?

Is there a way to do this?

我无法在此处发布完整的代码,但是我重现了错误:
该应用程序实际上正在使用反射。我不能在此处发布原始代码,但我已重现了异常:

I cannot post the full code here but I have reproduced the error: The application is actually using reflection. I can not post the original code here but I have reproduced the exception:

import java.lang.reflect.Method;
import java.util.*;

public class Main {
    public static void main(String[] args) {
        Class c = null;
        try {
            c = Class.forName("Second");
        } catch (ClassNotFoundException e1) {
            e1.printStackTrace();
        }
        String m = "doSomething";
        Method method = null;
        Class cs = Map.class;
        try{
            method = c.getMethod(m, cs);
        } catch (NoSuchMethodException e){
            e.printStackTrace();
        }
    }
}

class Second{
    public static void doSomething(Class<Map<Object, List<Object>>> theclass){
        System.out.println("HERE I AM!");   
    }   
}

例外:

java.lang.NoSuchMethodException: Second.doSomething(java.util.Map)
    at java.lang.Class.throwNoSuchMethodException(Class.java:283)
    at java.lang.Class.getMethod(Class.java:825)
    at Main.main(Main.java:16)


推荐答案

知道了!我已经更改了以下行:

Got it! I have changed the following line:

method = c.getMethod(m, cs);

至:

method = c.getMethod(m, cs.getClass());

现在可以了!

这篇关于Java-如何创建Class&lt; Map&lt; Object,List&lt; Object&gt;&gt;&gt;。目的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆