Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何检索实现自定义接口的所有类?_Java_Android_Reflection - Fatal编程技术网

Java 如何检索实现自定义接口的所有类?

Java 如何检索实现自定义接口的所有类?,java,android,reflection,Java,Android,Reflection,我想获得实现IFeature接口的所有类。Java引用似乎是一种可能的解决方案,但它没有按照我希望的方式工作 IFeature feature = new Landing(); Class<?>[] cls = Character.class.getInterfaces(); for(Class<?> c : cls ){ System.out.println(c.toString()); } IFeature功能=新平台(); Class[]cls=Chara

我想获得实现
IFeature
接口的所有类。Java引用似乎是一种可能的解决方案,但它没有按照我希望的方式工作

IFeature feature = new Landing();
Class<?>[] cls = Character.class.getInterfaces();
for(Class<?> c : cls ){
    System.out.println(c.toString());
}
IFeature功能=新平台();
Class[]cls=Character.Class.getInterfaces();
适用于(c类:cls){
System.out.println(c.toString());
}
输出

运行:

接口java.io.Serializable

接口java.lang.com

生成成功(总时间:0秒)

我的界面

public interface IFeature {
    public abstract void update(HashMap<QueueIdentifier, Queue<Measurement>> messageMap);
    public abstract List<QueueIdentifier> getQIdList();
    public abstract int getCountSamples();
}
公共接口IFeature{
公共摘要无效更新(HashMap messageMap);
公共摘要列表getQIdList();
公共摘要int getCountSamples();
}

解决方案是来自ApacheATN库的
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();
            }
        }
ClassPathLoader cpl=newclasspathloader(“.”);
试一试{
Hashtable ht=cpl.getClasses();
设置s=ht.keySet();
迭代器iter=s.Iterator();
字符串fullName=null;
while(iter.hasNext()){
试一试{
全名=(字符串)iter.next();
Class cls=Class.forName(全名);
类[]接口=cls.getInterfaces();
for(int i=0;i
一旦实现正确,它将按照您希望的方式工作。首先,您需要获取所有类的列表,然后检查一个类是否在循环中实现了一个接口,并遍历所有这些类。至于您的代码,您需要获取一个类,并获取其所有接口的列表。您还必须定义所有类的含义。所有在一个特定的包中?在一个罐子中?在你的项目的所有依赖项中?其他?为什么你要打印字符类的接口而不是登陆类?重复的问题:是的,对于所有的类,我指的是在我的项目中或在一个特别的包中的类。有人给我举个例子吗?