Java Class Find getClasses(Class cls)

Here you can find the source of getClasses(Class cls)

Description

get Classes

License

Apache License

Declaration

public static List<Class<?>> getClasses(Class<?> cls) throws IOException, ClassNotFoundException 

Method Source Code


//package com.java2s;
//License from project: Apache License 

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

public class Main {

    public static List<Class<?>> getClasses(Class<?> cls) throws IOException, ClassNotFoundException {
        String pk = cls.getPackage().getName();
        String path = pk.replace('.', '/');
        ClassLoader classloader = Thread.currentThread().getContextClassLoader();
        URL url = classloader.getResource(path);
        return getClasses(new File(url.getFile()), pk);
    }/* ww w. ja va  2 s. com*/

    private static List<Class<?>> getClasses(File dir, String pk) throws ClassNotFoundException {
        List<Class<?>> classes = new ArrayList<Class<?>>();
        if (!dir.exists()) {
            return classes;
        }
        for (File f : dir.listFiles()) {
            if (f.isDirectory()) {
                classes.addAll(getClasses(f, pk + "." + f.getName()));
            }
            String name = f.getName();
            if (name.endsWith(".class")) {
                classes.add(Class.forName(pk + "." + name.substring(0, name.length() - 6)));
            }
        }
        return classes;
    }
}

Related

  1. getAllClasses(String packageName)
  2. getAllClassesFromPackage(final String packageName)
  3. getAllClassesIn(String... packageNames)
  4. getAllClassesInPackage(String packageName)
  5. getClasses(Class clazz)
  6. getClasses(ClassLoader cl, String pack)
  7. getClasses(final String packageName)
  8. getClasses(final String packageName, final Class annotation)
  9. getClasses(final String pckgname)