Android development notes: Find all implementation classes from an interface

1. ClassUitls class

package cb.cuanbo.camera2;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

/** to find all implementation classes by an interface
 * Created by Administrator on 2018/4/21.
 */

public class ClassUtils {

    //Given an interface, return all implementation classes of this interface
    public static List<Class> getAllClassByInterface(Class c){
        List<Class> returnClassList = new ArrayList<Class>(); //return result

        //If it is not an interface, do not process
        if(c.isInterface()){
            String packageName = c.getPackage().getName(); //Get the current package name
            try {
                List<Class> allClass = getClasses(packageName); //Get all classes under the current package and subpackages

                // Determine if it is the same interface
                for(int i=0;i<allClass.size();i++){
                    if(c.isAssignableFrom(allClass.get(i))){ //Determine whether it is an interface
                        if(!c.equals(allClass.get(i))){ //It is not added
                            returnClassList.add(allClass.get(i));
                        }
                    }
                }
            } catch (ClassNotFoundException e) {
                e.printStackTrace ();
            } catch (IOException e) {
                e.printStackTrace ();
            }
        }
        return returnClassList;
    }

    / / Find all classes from a package, can not find in the jar package
    private static List<Class> getClasses(String packageName)
            throws ClassNotFoundException, IOException {
        ClassLoader classLoader = Thread.currentThread()
                .getContextClassLoader();
        String path = packageName.replace('.', '/');
        Enumeration<URL> resources = classLoader.getResources(path);
        List<File> dirs = new ArrayList<File>();
        while (resources.hasMoreElements()) {
            URL resource = resources.nextElement();
            dirs.add(new File(resource.getFile()));
        }
        ArrayList<Class> classes = new ArrayList<Class>();
        for (File directory : dirs) {
            classes.addAll(findClasses(directory, packageName));
        }
        return classes;
    }

    private static List<Class> findClasses(File directory, String packageName)
            throws ClassNotFoundException {
        List<Class> classes = new ArrayList<Class>();
        if (!directory.exists()) {
            return classes;
        }
        File[] files = directory.listFiles();
        for (File file : files) {
            if (file.isDirectory()) {
                assert !file.getName().contains(".");
                classes.addAll(findClasses(file, packageName + "." +
                        file.getName()));
            } else if (file.getName().endsWith(".class")) {
                classes.add(Class.forName(packageName + '.' +
                        file.getName().substring(0, file.getName().length() - 6)));
            }
        }
        return classes;
    }
}




2 calls


   //The first is to get how many implementation classes there are,
 List<Class> concreteHumanList = 
ClassUtils.getAllClassByInterface(Person.class); //How many races are defined






Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324693965&siteId=291194637