Java反射和接口作为参数 [英] Java reflection and interface as parameter

查看:207
本文介绍了Java反射和接口作为参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过反射来调用方法。有问题的方法,假设

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

public void someMethod(someInterface<someObject> arg1)

我无法访问 someMethod someInterface 在运行时,必须通过

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(...)

但它失败了 someInterface 的ClassNotFound异常。如何获取接口的 Class 对象?

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

推荐答案

我相信你忘记了界面的包裹。调用Class.forName()时必须使用完全限定的类名,即Class.forName('com.mycompany.MyClass')

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')

这篇关于Java反射和接口作为参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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