Java Class Find getClasses(final String packageName, final Class annotation)

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

Description

Return all classes below the package root that have the specified annotation.

License

Mozilla Public License

Parameter

Parameter Description
packageName the starting package to search.
annotation the annotation to find

Exception

Parameter Description
ClassNotFoundExceptionthrows class not found
IOException throws I/O exception in searching package structures

Return

return a list of classes found (an empty list is possible)

Declaration

public static List<Class<?>> getClasses(final String packageName, final Class<? extends Annotation> annotation)
        throws ClassNotFoundException, IOException 

Method Source Code


//package com.java2s;
/*/*from  w  ww  .j  a v a  2  s.c  om*/
 * Copyright (c) 2015 Jeremy Miller
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

import java.io.File;
import java.io.IOException;
import java.lang.annotation.Annotation;

import java.net.URL;
import java.util.Enumeration;
import java.util.LinkedList;
import java.util.List;

public class Main {
    /**
     * Return all classes below the package root that have the specified annotation.
     * @param packageName              the starting package to search.
     * @param annotation               the annotation to find
     * @return                         return a list of classes found (an empty list is possible)
     * @throws ClassNotFoundException  throws class not found
     * @throws IOException             throws I/O exception in searching package structures
     */
    public static List<Class<?>> getClasses(final String packageName, final Class<? extends Annotation> annotation)
            throws ClassNotFoundException, IOException {
        final List<Class<?>> classes = getClasses(packageName);
        final List<Class<?>> annotatedClasses = new LinkedList<Class<?>>();

        for (Class<?> clazz : classes) {
            if (clazz.isAnnotationPresent(annotation)) {
                annotatedClasses.add(clazz);
            }
        }

        return annotatedClasses;
    }

    /**
     * Scans all classes accessible from the context class loader which belong to the given package and sub-packages.
     * @param packageName              the base package
     * @return                         return the classes foundThe classes
     * @throws ClassNotFoundException  throws class not found exception
     * @throws IOException             throws I/O exception
     */
    public static List<Class<?>> getClasses(final String packageName) throws ClassNotFoundException, IOException {
        final ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
        final String path = packageName.replace('.', '/');
        final Enumeration<URL> resources = classLoader.getResources(path);
        final List<File> dirs = new LinkedList<File>();

        while (resources.hasMoreElements()) {
            URL resource = resources.nextElement();
            dirs.add(new File(resource.getFile()));
        }

        final List<Class<?>> classes = new LinkedList<Class<?>>();

        for (File directory : dirs) {
            classes.addAll(findClasses(directory, packageName));
        }

        return classes;
    }

    /**
     * Recursive method used to find all classes in a given directory and sub-directories.
     * @param directory                the base directory
     * @param packageName              the package name for classes found inside the base directory
     * @return                         return the found classes
     * @throws ClassNotFoundException  throws class not found exception
     */
    private static List<Class<?>> findClasses(final File directory, final String packageName)
            throws ClassNotFoundException {
        final List<Class<?>> classes = new LinkedList<Class<?>>();

        if (!directory.exists()) {
            return classes;
        }

        final File[] files = directory.listFiles();

        for (File file : files) {
            if (file.isDirectory()) {
                classes.addAll(findClasses(file, packageName + "." + file.getName()));
            } else if (file.getName().endsWith(".class")) {
                classes.add(Class
                        .forName(packageName + '.' + file.getName().substring(0, file.getName().length() - 6)));
            }
        }

        return classes;
    }
}

Related

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