如何检索实现自定义接口的所有类? [英] How can I retrieve all classes that implement a custom interface?
本文介绍了如何检索实现自定义接口的所有类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想获取所有实现 IFeature
接口的类.Java Refelctions似乎是一种可能的解决方案,但它并不能达到我想要的方式.
I want to get all classes that implement the IFeature
Interface. Java Refelctions appear to be a possible solution, but it doesn't work the way I want it to.
IFeature feature = new Landing();
Class<?>[] cls = Character.class.getInterfaces();
for(Class<?> c : cls ){
System.out.println(c.toString());
}
输出
运行:
接口java.io.Serializable
interface java.io.Serializable
接口java.lang.Comparable
interface java.lang.Comparable
成功建立(总时间:0秒)
BUILD SUCCESSFUL (total time: 0 seconds)
我的界面...
public interface IFeature {
public abstract void update(HashMap<QueueIdentifier, Queue<Measurement>> messageMap);
public abstract List<QueueIdentifier> getQIdList();
public abstract int getCountSamples();
}
推荐答案
解决方案是apache atn库中的 ClassPathLoader
.这是我的代码...
The soluton is the ClassPathLoader
from the apache atn library.
Here my code...
ClassPathLoader cpl = new ClassPathLoader(".");
try {
Hashtable ht = cpl.getClasses();
Set s = ht.keySet();
Iterator iter = s.iterator();
String fullName = null;
while(iter.hasNext()) {
try {
fullName = (String) iter.next();
Class cls = Class.forName(fullName);
Class[] interfaces = cls.getInterfaces();
for(int i = 0; i < interfaces.length; i++) {
if(interfaces[i].getName().equals("IMyObserver.IFeature")) {
Object o = cls.newInstance();
}
}
这篇关于如何检索实现自定义接口的所有类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文