Java Utililty Methods Class Find

List of utility methods to do Class Find

Description

The list of methods to do Class Find are organized into topic(s).

Method

List>getClasses(final String pckgname)
This method returns all found classes in a specified package by going through the package directory.
ArrayList<Class<?>> classes = new ArrayList<Class<?>>();
File directory = null;
try {
    ClassLoader cld = Thread.currentThread().getContextClassLoader();
    if (cld == null) {
        throw new ClassNotFoundException("Can't get class loader.");
    String path = pckgname.replace('.', '/');
...
Set>getClasses(String pack)
Gets the classes.
Set<Class<?>> classes = new LinkedHashSet<Class<?>>();
boolean recursive = true;
String packageName = pack;
String packageDirName = packageName.replace('.', '/');
Enumeration<URL> dirs;
try {
    dirs = Thread.currentThread().getContextClassLoader().getResources(packageDirName);
    while (dirs.hasMoreElements()) {
...
Class[]getClasses(String pkgname, boolean flat)
Scans all classes accessible from the context class loader which belong to the given package and subpackages.
List<File> dirs = new ArrayList<File>();
List<Class<?>> classes = new ArrayList<Class<?>>();
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
String path = pkgname.replace('.', '/');
Enumeration<URL> resources = null;
try {
    resources = classLoader.getResources(path);
} catch (IOException e) {
...
List>getClassesAsList(String pckgname, Class type)
get Classes As List
ArrayList<Class<A>> classes = new ArrayList<Class<A>>();
File directory = null;
try {
    ClassLoader cld = Thread.currentThread().getContextClassLoader();
    if (cld == null) {
        throw new ClassNotFoundException("Can't get class loader.");
    String path = pckgname.replace('.', '/');
...
FilegetClassesDir(Class classWithinDir)
get Classes Dir
return getClassesDir(classWithinDir == null ? null : classWithinDir.getName());
Collection>getClassesExtending(Class rootClass, String pattern)
get Classes Extending
ArrayList<Class<? extends E>> classes = new ArrayList<Class<? extends E>>();
File dir = findOriginClassPath(rootClass, pattern);
for (String filename : dir.list()) {
    if (filename.endsWith(".class")) {
        Class<?> c = Class.forName(pattern.replace('/', '.') + "." + filename.split("\\.")[0], true,
                Thread.currentThread().getContextClassLoader());
        if (rootClass.isAssignableFrom(c) && c != rootClass) {
            classes.add(c.asSubclass(rootClass));
...
List>getClassesInDirectory(File directory, String packageName, ClassLoader classLoader)
get Classes In Directory
List<Class<?>> classes = new ArrayList<Class<?>>();
String[] files = directory.list();
for (String currentFile : files) {
    if (currentFile.endsWith(".class")) {
        try {
            String className = packageName + '.' + currentFile.substring(0, currentFile.length() - 6);
            classes.add(loadClass(className, classLoader));
        } catch (Throwable e) {
...
FilegetClassesLocation(Class cls)
get Classes Location
return new File(cls.getProtectionDomain().getCodeSource().getLocation().toURI());
List>getClassesWithInterface(String packageName, Class interfaceClazz)
get Classes With Interface
if (interfaceClazz == null || !interfaceClazz.isInterface()) {
    return null;
List<Class<?>> classes = new ArrayList<Class<?>>();
List<Class<?>> list = getClasses(packageName);
if (list != null) {
    for (Class<?> clazz : list) {
        if (interfaceClazz.isAssignableFrom(clazz)) {
...