Java Class Find findClasses(String directory, String packageName)

Here you can find the source of findClasses(String directory, String packageName)

Description

Recursive method used to find all classes in a given directory and subdirs.

License

Open Source License

Parameter

Parameter Description
directory The base directory
packageName The package name for classes found inside the base directory

Exception

Parameter Description
ClassNotFoundException an exception

Return

The classes

Declaration

private static TreeSet<String> findClasses(String directory, String packageName) throws Exception 

Method Source Code


//package com.java2s;
import java.io.File;
import java.net.URL;

import java.util.TreeSet;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

public class Main {
    /**/*  w w  w. j a v a2 s  . c  o m*/
     * Recursive method used to find all classes in a given directory and
     * subdirs. Adapted from http://snippets.dzone.com/posts/show/4831 and
     * extended to support use of JAR files
     * 
     * @param directory
     *            The base directory
     * @param packageName
     *            The package name for classes found inside the base directory
     * @return The classes
     * @throws ClassNotFoundException
     */
    private static TreeSet<String> findClasses(String directory, String packageName) throws Exception {
        TreeSet<String> classes = new TreeSet<String>();
        if (directory.startsWith("file:") && directory.contains("!")) {
            String[] split = directory.split("!");
            URL jar = new URL(split[0]);
            ZipInputStream zip = new ZipInputStream(jar.openStream());
            ZipEntry entry = null;
            while ((entry = zip.getNextEntry()) != null) {
                if (entry.getName().endsWith(".class")) {
                    String className = entry.getName().replaceAll("[$].*", "").replaceAll("[.]class", "")
                            .replace('/', '.');
                    classes.add(className);
                }
            }
        }
        File dir = new File(directory);
        if (!dir.exists()) {
            return classes;
        }
        File[] files = dir.listFiles();
        for (File file : files) {
            if (file.isDirectory()) {
                assert !file.getName().contains(".");
                classes.addAll(findClasses(file.getAbsolutePath(), packageName + "." + file.getName()));
            } else if (file.getName().endsWith(".class")) {
                classes.add(packageName + '.' + file.getName().substring(0, file.getName().length() - 6));
            }
        }
        return classes;
    }
}

Related

  1. findAllClasses(String packageName)
  2. findClass(String packageName, Class[] superClass)
  3. findClassBase(Class clazz)
  4. findClasses(String path, String packageName)
  5. findClassesAnnotatedWith(String packageName, Class a)
  6. findClassesAssignableFrom(String packageName, Class assignableFrom)
  7. findClassLocation(Class context)