Java Class Find getClasses(final String packageName)

Here you can find the source of getClasses(final String packageName)

Description

get Classes

License

Open Source License

Declaration

public static Set<Class<?>> getClasses(final String packageName) throws Exception 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URL;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.Set;
import java.util.jar.JarEntry;
import java.util.jar.JarInputStream;

public class Main {
    public static Set<Class<?>> getClasses(final String packageName) throws Exception {
        final ClassLoader loader = Thread.currentThread().getContextClassLoader();
        return getClasses(loader, packageName);
    }//from  w w  w.  j  a v  a 2s.c o  m

    private static Set<Class<?>> getClasses(final ClassLoader loader, final String packageName)
            throws IOException, ClassNotFoundException {

        final Set<Class<?>> classes = new HashSet<Class<?>>();
        final String path = packageName.replace('.', '/');
        final Enumeration<URL> resources = loader.getResources(path);
        if (resources != null) {
            while (resources.hasMoreElements()) {
                String filePath = resources.nextElement().getFile();
                // WINDOWS HACK
                if (filePath.indexOf("%20") > 0) {
                    filePath = filePath.replaceAll("%20", " ");
                }
                if (filePath != null) {
                    if ((filePath.indexOf("!") > 0) & (filePath.indexOf(".jar") > 0)) {
                        String jarPath = filePath.substring(0, filePath.indexOf("!"))
                                .substring(filePath.indexOf(":") + 1);
                        // WINDOWS HACK
                        if (jarPath.indexOf(":") >= 0) {
                            jarPath = jarPath.substring(1);
                        }
                        classes.addAll(getFromJARFile(jarPath, path));
                    } else {
                        classes.addAll(getFromDirectory(new File(filePath), packageName));
                    }
                }
            }
        }
        return classes;
    }

    private static Set<Class<?>> getFromJARFile(final String jar, final String packageName)
            throws ClassNotFoundException, IOException {

        final Set<Class<?>> classes = new HashSet<Class<?>>();
        final JarInputStream jarFile = new JarInputStream(new FileInputStream(jar));
        JarEntry jarEntry;
        do {
            jarEntry = jarFile.getNextJarEntry();
            if (jarEntry != null) {
                String className = jarEntry.getName();
                if (className.endsWith(".class")) {
                    className = stripFilenameExtension(className);
                    if (className.startsWith(packageName)) {
                        classes.add(Class.forName(className.replace('/', '.')));
                    }
                }
            }
        } while (jarEntry != null);
        jarFile.close();
        return classes;
    }

    private static Set<Class<?>> getFromDirectory(final File directory, final String packageName)
            throws ClassNotFoundException {

        final Set<Class<?>> classes = new HashSet<Class<?>>();
        if (directory.exists()) {
            for (final String file : directory.list()) {
                if (file.endsWith(".class")) {
                    final String name = packageName + '.' + stripFilenameExtension(file);
                    final Class<?> clazz = Class.forName(name);
                    classes.add(clazz);
                }
            }
        }
        return classes;
    }

    private static String stripFilenameExtension(final String file) {
        return file.substring(0, file.length() - (".class".length()));
    }
}

Related

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