Java Class Find getClasses(final String pckgname)

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

Description

This method returns all found classes in a specified package by going through the package directory.

License

Open Source License

Parameter

Parameter Description
pckgname the name of the package (e.g. de.randi2.model.criteria)

Exception

Parameter Description
ClassNotFoundException an exception

Return

all found classes

Declaration

public static List<Class<?>> getClasses(final String pckgname) throws ClassNotFoundException 

Method Source Code


//package com.java2s;
/* /* w w w  . j a va 2 s.c o m*/
 * (c) 2008- RANDI2 Core Development Team
 * 
 * This file is part of RANDI2.
 * 
 * RANDI2 is free software: you can redistribute it and/or modify it under the
 * terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 * 
 * RANDI2 is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * RANDI2. If not, see <http://www.gnu.org/licenses/>.
 */

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

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

import java.util.List;

public class Main {
    /**
     * This method returns all found classes in a specified package by going
     * through the package directory.
     *
     * @param pckgname
     *            the name of the package (e.g. de.randi2.model.criteria)
     * @return all found classes
     * @throws ClassNotFoundException
     */
    public static List<Class<?>> getClasses(final String pckgname) throws ClassNotFoundException {
        ArrayList<Class<?>> classes = new ArrayList<Class<?>>();
        // Get a File object for the package
        File directory = null;
        try {
            ClassLoader cld = Thread.currentThread().getContextClassLoader();
            if (cld == null) {
                throw new ClassNotFoundException("Can't get class loader.");
            }
            String path = pckgname.replace('.', '/');
            URL resource = cld.getResource(path);
            if (resource == null) {
                throw new ClassNotFoundException("No resource for " + path);
            }
            directory = new File(URLDecoder.decode(resource.getPath(), "UTF-8"));
            if (directory.exists()) {
                // Get the list of the files contained in the package
                String[] files = directory.list();
                for (int i = 0; i < files.length; i++) {
                    // we are only interested in .class files
                    if (files[i].endsWith(".class")) {
                        // removes the .class extension
                        classes.add(Class.forName(pckgname + '.' + files[i].substring(0, files[i].length() - 6)));
                    }
                }
            } else {
                throw new ClassNotFoundException(pckgname + " does not appear to be a valid package");
            }
        } catch (NullPointerException x) {
            throw new ClassNotFoundException(
                    pckgname + " (" + directory + ") does not appear to be a valid package");
        } catch (UnsupportedEncodingException x) {
            x.printStackTrace();
        }
        return classes;
    }
}

Related

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