-1

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

the output

run:

interface java.io.Serializable

interface java.lang.Comparable

BUILD SUCCESSFUL (total time: 0 seconds)

my interface...

public interface IFeature {
    public abstract void update(HashMap<QueueIdentifier, Queue<Measurement>> messageMap);
    public abstract List<QueueIdentifier> getQIdList();
    public abstract int getCountSamples();
}
Community
  • 1
  • 1
cojack
  • 59
  • 1
  • 4
  • 1
    It will work the way you want once you implement it right. First you need to get the list of all classes, and then check if a class implements an interface in cycle looping through all that classes. As for you code, you take one class, and get list of it's all interfaces. – Vladyslav Matviienko Mar 13 '17 at 11:18
  • You will also have to define what you mean by _**all** classes_ . All the classes found in a certain package ? In a JAR ? In all the dependencies of your project ? Other ? – SantiBailors Mar 13 '17 at 11:31
  • 1
    Why are you printing the interfaces of Character class instead of Landing class? – Eduardo Yáñez Parareda Mar 13 '17 at 11:31
  • 1
    Duplicated question: http://stackoverflow.com/questions/347248/how-can-i-get-a-list-of-all-the-implementations-of-an-interface-programmatically – Eduardo Yáñez Parareda Mar 13 '17 at 11:42
  • yes, with all classes i mean classes which are in my project or in a especially package. dose enyone have a example for me? – cojack Mar 13 '17 at 12:05
  • in the future, the Classes which implements the IFeature interface, will be placed in a library. – cojack Mar 13 '17 at 12:08
  • You did not explain what's your problem. You just showed some code, the output it correctly produces, and you just say it's not what you want. You don't even say why that's not what you want - or what is that you want. How are people supposed to help you ? – SantiBailors Mar 13 '17 at 13:08
  • You can't just do it with normal Java code, because the classes implementing the interface might not even have been loaded yet. If you really need to do this, you need to scan the classpath and parse .class files directly (because loading each of them is a no-go obviously). Library called Reflections already does this, so use that. https://github.com/ronmamo/reflections But your question is indeed terrible. Do you need to find all implementations of `IFeature` (also don't call interfaces `ISomething`, just `Something`) or all interfaces of `Character`? – kaqqao Mar 13 '17 at 13:14

1 Answers1

2

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();
            }
        }
cojack
  • 59
  • 1
  • 4