Skip to content Skip to sidebar Skip to footer

How Can I Retrieve All Classes That Implement A Custom Interface?

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

Solution 1:

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

Post a Comment for "How Can I Retrieve All Classes That Implement A Custom Interface?"