Java reflection and interface as parameter

久未见 提交于 2020-01-06 19:32:38

问题


I'm trying to invoke a method via reflection. The method in question, let's say

public void someMethod(someInterface<someObject> arg1)

I do not have access to someMethod and someInterface at runtime, and have to invoke by

someclass.getMethod("someMethod", new Class[]{Class.forName("someInterface")})
         .invoke(...)

But it fails with a ClassNotFound exception for someInterface. How do I get the Class object for interfaces?


回答1:


I believe that you forgot the interface's package. You have to use fully qualified class name when you are calling Class.forName(), i.e. Class.forName('com.mycompany.MyClass')




回答2:


That looks correct to me, conceptually. Check these things:

  • Is the interface on the classpath at run time?
  • Is the interface public (not package private)
  • Is the interface really in the default package (you have to fully qualify it)

Last but not least ;-)

  • Check your spelling (case-sensitive)


来源:https://stackoverflow.com/questions/4516609/java-reflection-and-interface-as-parameter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!