我想要获得所有实现的类IFeature
界面。 Java Refelctions 似乎是一个可能的解决方案,但它并没有按照我想要的方式工作。
IFeature feature = new Landing();
Class<?>[] cls = Character.class.getInterfaces();
for(Class<?> c : cls ){
System.out.println(c.toString());
}
输出
run:
java.io.Serialized 接口
接口 java.lang.Comparable
构建成功(总时间:0 秒)
我的界面...
public interface IFeature {
public abstract void update(HashMap<QueueIdentifier, Queue<Measurement>> messageMap);
public abstract List<QueueIdentifier> getQIdList();
public abstract int getCountSamples();
}
解决方案是ClassPathLoader
来自 apache atn 库。
这是我的代码...
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();
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)