如何检索实现自定义界面的所有类?

时间:2017-03-13 11:14:23

标签: java android reflection

我希望获得实现IFeature接口的所有类。 Java Refelctions似乎是一种可能的解决方案,但它并不像我想要的那样工作。

IFeature feature = new Landing();
Class<?>[] cls = Character.class.getInterfaces();
for(Class<?> c : cls ){
    System.out.println(c.toString());
}

输出

  

运行:

     

interface java.io.Serializable

     

interface 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();
}

1 个答案:

答案 0 :(得分:1)

解决方案是来自apache atn库的ClassPathLoader。 这是我的代码......

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();
            }
        }
相关问题