简体   繁体   English

java如何找到我未知的兄弟? (2个类实现相同的接口)

[英]java how to find my unknown brother? (2 classes implementing same interface)

i have the following situation: 我有以下情况:

i am a class numberOneUser and a framework loads me as a .class file from the file system at runtime. 我是一个类numberOneUser,一个框架在运行时从文件系统加载我作为.class文件。

i know that in the directory where my .class file lies there are more .class files. 我知道在我的.class文件所在的目录中有更多的.class文件。 (eg theOtherUser.class) (例如theOtherUser.class)

some of them implement the same interface as i do. 其中一些实现了与我相同的界面。 (called User) (称为用户)

now i want to find at runtime the secound (or more) class(es) that implement that interface and modify them with reflection (eg override methods). 现在我想在运行时找到实现该接口的secound(或更多)类,并用反射修改它们(例如覆盖方法)。

the only reference i have to the framework is that i know that i am being called and given a reference to a object that i can respond do which is part of the framework. 我对框架的唯一参考是我知道我被调用并且给出了一个对象,我可以响应,这是框架的一部分。 furthermore i know that the framework lies as framework.jar file in the directory above me. 此外我知道框架位于我上面的目录中的framework.jar文件中。

no additional information is available. 没有其他信息可用。

how to find this other classes? 如何找到其他课程?

Take a look at Reflections may its helps you. 看看思考它可以帮助你。

Set<Class<? extends YourClassOrInterface>> subTypes = 
 reflections.getSubTypesOf(YourClassOrInterface.class);

Check out this answer which addresses how to find .class files in a directory and create a collection of Class elements. 查看此答案该答案解决了如何在目录中查找.class文件并创建Class元素的集合。 Once you have the classes you could use reflection or the instanceof operator to determine which ones implement your interface. 一旦有了类,就可以使用反射或instanceof运算符来确定哪些实现了接口。

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

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