简体   繁体   English

Java类实现反射接口

[英]Java class implement reflected interface

Is there some way to tell java to think that Object passed to reflected method implements interface of input attribute of method? 有没有办法告诉Java认为传递给反射方法的Object实现了方法的输入属性的接口?

public class Debuger /*implements IDebuger*/{
...
}

and this Debuger I need to use in reflected method someDocument.attachDebuger(IDebuger) . 这个调试器我需要在反射方法someDocument.attachDebuger(IDebuger)

I know the structure of IDebuger interface, but I cant just simply write implements IDebuger since it is not in my project. 我知道IDebuger接口的结构,但是我不能简单地编写implements IDebuger因为它不在我的项目中。 I want to be able to call something similar 我希望能够打电话给类似的人

Debuger dbg = new Debuger();
Class theClassINeedToImplement = ...;
Object document = ...;
Class docClass = document.GetClass();
/*
HERE call something like 
Object Idbg = theClassINeedToImplement.ForceImplementInterface(dbg);
*/
Method m = docClass.getMethod("attachDebuger", theClassINeedToImplement);
m.invoke(document, Idbg);

I think you need to import IDebuger to your project and then implement it with Debuger. 我认为您需要将IDebuger导入到您的项目中,然后使用Debuger实现它。 Otherwise the method won't know it is being given an IDebuger, and that will cause a compile error. 否则,该方法将不知道是否为它提供了IDebuger,这将导致编译错误。

Even if its in an external jar, you can import it and use the interface. 即使将其放在外部jar中,也可以将其导入并使用该接口。 In Eclipse, right click the project, then select build path, then add external archives. 在Eclipse中,右键单击项目,然后选择构建路径,然后添加外部归档。 I hope that works. 我希望可以。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM