Java Class from Package getClassesForPackage(String packageName)

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

Description

get Classes For Package

License

Apache License

Declaration

public static Set<Class<?>> getClassesForPackage(String packageName) 

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.net.URLDecoder;
import java.util.Collection;
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<?>> getClassesForPackage(String packageName) {
        Set<Class<?>> classes = new HashSet<Class<?>>();

        JarInputStream jarInputStream = null;
        try {//from   w  ww  . j a va 2 s  .c o  m
            ClassLoader classLoader = Thread.currentThread().getContextClassLoader();

            Enumeration<URL> resources = classLoader.getResources(packageName.replace('.', '/'));
            while (resources.hasMoreElements()) {
                URL res = resources.nextElement();

                if (res.getProtocol().matches("(jar|zip)")) {
                    res = new URL((res.getProtocol().equals("zip") || !res.getFile().contains("file:")
                            ? "file:".concat(res.getFile().startsWith("/") ? "" : "/")
                            : "").concat(res.getFile().split("!")[0]));
                }
                if (res.getFile().contains(".jar")) {
                    jarInputStream = new JarInputStream(res.openStream());
                    JarEntry entry = jarInputStream.getNextJarEntry();

                    while (entry != null) {
                        if (entry.getName().startsWith(packageName.replace('.', '/'))
                                && entry.getName().endsWith(".class") && !entry.getName().contains("$")) {
                            classes.add(Class.forName(entry.getName().replace(".class", "").replace("/", ".")));
                        }

                        entry = jarInputStream.getNextJarEntry();
                    }
                } else {
                    classes.addAll(scanClassesFromDirectory(new File(URLDecoder.decode(res.getPath(), "UTF-8")),
                            packageName));
                }
            }
        } catch (Exception x) {
            throw new IllegalArgumentException("invalid package");
        } finally {
            if (jarInputStream != null) {
                try {
                    jarInputStream.close();
                } catch (IOException e) {
                    throw new IllegalStateException(e.getMessage());
                }
            }
        }

        return classes;
    }

    private static Collection<Class<?>> scanClassesFromDirectory(File directory, String packageName) {
        Set<Class<?>> classes = new HashSet<Class<?>>();

        if (directory.exists() && directory.isDirectory()) {
            String[] files = directory.list();
            for (String fileItem : files) {
                if (fileItem.endsWith(".class")) {
                    try {
                        classes.add(Class.forName(packageName.concat(".").concat(fileItem.replace(".class", ""))));
                    } catch (Exception e) {
                        throw new IllegalArgumentException("Invalid package " + packageName);
                    }
                } else if (!fileItem.matches(".+\\..+")) {
                    File dir = new File(directory, fileItem);
                    if (dir.isDirectory()) {
                        classes.addAll(scanClassesFromDirectory(dir, packageName.concat(".").concat(fileItem)));
                    }
                }
            }
        }
        return classes;
    }
}

Related

  1. getClasses(String pkg)
  2. getClasses(String pkg)
  3. getClassesForPackage(final String packageName)
  4. getClassesForPackage(Package pkg)
  5. getClassesForPackage(String packageName)
  6. getClassesForPackage(String pckgname)
  7. getClassesForPackage(String pckgname)
  8. getClassesInPackage(String packageName)
  9. getClassesInPackage(String packageName)