Java Class Find getAllClassesIn(String... packageNames)

Here you can find the source of getAllClassesIn(String... packageNames)

Description

Returns a collection of all classes present in the packages with the given packageNames.

License

Apache License

Parameter

Parameter Description
packageNames - names of the packages to scan for classes

Return

all classes present in the packages with the given packageNames

Declaration

public static Collection<Class<?>> getAllClassesIn(String... packageNames) 

Method Source Code

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

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;

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

import java.util.Collection;

import java.util.Enumeration;
import java.util.List;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

public class Main {
    /**//  w w w.  j  a  v  a2  s .c o m
     * Returns a collection of all classes present in the packages with the given packageNames.
     * 
     * @param packageNames - names of the packages to scan for classes
     * @return all classes present in the packages with the given packageNames
     */
    public static Collection<Class<?>> getAllClassesIn(String... packageNames) {
        Collection<Class<?>> allClasses = new ArrayList<Class<?>>();
        for (String packageName : packageNames) {
            allClasses.addAll(getAllClassesIn(packageName));
        }
        return allClasses;
    }

    /**
     * Returns a collection of all classes present in the package with the given packageName.
     * 
     * @param packageName - name of the package to scan for classes
     * @return all classes present in the packages with the given packageName
     */
    public static Collection<Class<?>> getAllClassesIn(String packageName) {
        Collection<String> classNames = getFullyQualifiedClassNamesForPackage(packageName);

        Collection<Class<?>> allClasses = new ArrayList<Class<?>>();
        for (String className : classNames) {
            try {
                allClasses.add(Class.forName(className));
            } catch (ClassNotFoundException e) {
                //cannot happen, ensured by getFullyQualifiedClassNamesForPackage call?
                e.printStackTrace();
            }
        }

        return allClasses;
    }

    /**
     * Returns all the fully qualified class names of the classes within a package.
     * Starts from the Classloader of the current thread and then uses a filebased approach to scan files.
     * 
     * @param packageName - the name of the package to get class names for
     * @return a list of fully qualified class names present in the package with given packageName
     */
    public static Collection<String> getFullyQualifiedClassNamesForPackage(String packageName) {
        List<String> fullyQualifiedClassNames = new ArrayList<String>();
        for (String className : getSimpleClassNamesForPackage(packageName)) {
            fullyQualifiedClassNames.add(packageName + "." + className);
        }
        return fullyQualifiedClassNames;
    }

    /**
     * Returns all the simple class names of the classes within a package.
     * Starts from the Classloader of the current thread and then uses a filebased approach to scan files.
     * 
     * @param packageName - the name of the package to get class names for
     * @return a list of simple(without package) class names present in the package with given packageName
     */
    public static Collection<String> getSimpleClassNamesForPackage(String packageName) {
        ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
        URL packageURL;
        ArrayList<String> names = new ArrayList<String>();

        String packageFolder = packageName.replace(".", "/");
        packageURL = classLoader.getResource(packageFolder);

        if (packageURL.getProtocol().equals("jar")) {
            String jarFileName;
            JarFile jf;
            Enumeration<JarEntry> jarEntries;
            String entryName;

            // build jar file name, then loop through zipped entries
            try {
                jarFileName = URLDecoder.decode(packageURL.getFile(), "UTF-8");
                jarFileName = jarFileName.substring(5, jarFileName.indexOf("!"));
                System.out.println(">" + jarFileName);
                jf = new JarFile(jarFileName);
                jarEntries = jf.entries();
                while (jarEntries.hasMoreElements()) {
                    entryName = jarEntries.nextElement().getName();
                    if (entryName.startsWith(packageFolder) && entryName.length() > packageFolder.length() + 5) {
                        entryName = entryName.substring(packageFolder.length(), entryName.lastIndexOf('.'));
                        names.add(entryName);
                    }
                }
            } catch (UnsupportedEncodingException e) {
                //Cannot happen? UTF-8 always supported?
                e.printStackTrace();
            } catch (IOException e) {
                //Cannot happen? Jar file always present?
                e.printStackTrace();
            }

            // loop through files in classpath
        } else {
            File folder = new File(packageURL.getFile());
            File[] contenuti = folder.listFiles();
            String entryName;
            for (File actual : contenuti) {
                entryName = actual.getName();
                if (actual.getName().endsWith(".class")) {
                    entryName = entryName.substring(0, entryName.lastIndexOf('.'));
                    names.add(entryName);
                }
            }
        }
        return names;
    }
}

Related

  1. findClassOriginFile(Class cls)
  2. getAllAssignedClass(Class cls)
  3. getAllClasses(ClassLoader cl, String packageName)
  4. getAllClasses(String packageName)
  5. getAllClassesFromPackage(final String packageName)
  6. getAllClassesInPackage(String packageName)
  7. getClasses(Class clazz)
  8. getClasses(Class cls)
  9. getClasses(ClassLoader cl, String pack)