Java Class Find findAllClasses(String packageName)

Here you can find the source of findAllClasses(String packageName)

Description

find All Classes

License

Apache License

Declaration

@SuppressWarnings("rawtypes")
    private static List<Class> findAllClasses(String packageName)
            throws ClassNotFoundException 

Method Source Code

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

import java.io.File;

import java.net.URL;
import java.util.ArrayList;
import java.util.List;

public class Main {
    @SuppressWarnings("rawtypes")
    private static List<Class> findAllClasses(String packageName)
            throws ClassNotFoundException {
        List<Class> classes = new ArrayList<Class>();
        File directory = null;//w w  w .j a  va 2s .co  m
        try {
            ClassLoader classLoader = Thread.currentThread()
                    .getContextClassLoader();
            if (classLoader == null)
                throw new ClassNotFoundException("Can't get class loader.");
            String path = '/' + packageName.replace('.', '/');
            URL resource = classLoader.getResource(path);
            if (resource == null)
                throw new ClassNotFoundException("No resource for " + path);
            directory = new File(resource.getFile());
        } catch (NullPointerException x) {
            throw new ClassNotFoundException(packageName + " (" + directory
                    + ") does not appear to be a valid package a");
        }
        if (directory != null && directory.exists()) {
            classes = findAllClassesInFile(packageName, directory);
        } else {
            throw new ClassNotFoundException(packageName
                    + " does not appear to be a valid package b");
        }
        return classes;
    }

    /**
     * @param packageName should be consistent with directory
     * @param directory
     * @return all subclasses in the dir and sub dirs
     * @throws ClassNotFoundException 
     */
    @SuppressWarnings("rawtypes")
    private static List<Class> findAllClassesInFile(String packageName,
            File directory) throws ClassNotFoundException {
        List<Class> classes = new ArrayList<Class>();
        if (directory != null && directory.exists()) {
            // Get the list of the files contained in the package
            File[] files = directory.listFiles();
            for (File f : files) {
                String fileName = f.getName();
                if (f.isDirectory()) {
                    classes.addAll(findAllClassesInFile(packageName + "."
                            + f.getName(), f));
                } else if (fileName.endsWith(".class")) {
                    classes.add(Class.forName(packageName + '.'
                            + fileName.substring(0, fileName.length() - 6)));
                }
            }
        }
        return classes;
    }
}

Related

  1. findClass(String packageName, Class[] superClass)
  2. findClassBase(Class clazz)
  3. findClasses(String directory, String packageName)
  4. findClasses(String path, String packageName)