Java Class Find getClassesAsList(String pckgname, Class type)

Here you can find the source of getClassesAsList(String pckgname, Class type)

Description

get Classes As List

License

Open Source License

Declaration

@SuppressWarnings({ "unchecked", "rawtypes" })
    public static <A> List<Class<A>> getClassesAsList(String pckgname,
            Class<A> type) throws ClassNotFoundException 

Method Source Code

//package com.java2s;
/******************************************************************************
 * Copyright (C) 2009-2015  BIMserver.org
 * /*ww  w. j a va  2s . co m*/
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 * 
 * This program 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 Affero General Public License for more details.
 * 
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *****************************************************************************/

import java.io.File;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

public class Main {
    @SuppressWarnings({ "unchecked", "rawtypes" })
    public static <A> List<Class<A>> getClassesAsList(String pckgname,
            Class<A> type) throws ClassNotFoundException {
        ArrayList<Class<A>> classes = new ArrayList<Class<A>>();
        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(resource.getFile());
        } catch (NullPointerException x) {
            throw new ClassNotFoundException(pckgname + " (" + directory
                    + ") does not appear to be a valid package");
        }
        if (directory.exists()) {
            String[] files = directory.list();
            for (int i = 0; i < files.length; i++) {
                if (files[i].endsWith(".class")) {
                    Class forName = Class.forName(pckgname + '.'
                            + files[i].substring(0, files[i].length() - 6));
                    if (type.isAssignableFrom(forName) && type != forName) {
                        classes.add(forName);
                    }
                }
            }
        } else {
            throw new ClassNotFoundException(pckgname
                    + " does not appear to be a valid package");
        }
        return classes;
    }
}

Related

  1. getClasses(final String packageName)
  2. getClasses(final String packageName, final Class annotation)
  3. getClasses(final String pckgname)
  4. getClasses(String pack)
  5. getClasses(String pkgname, boolean flat)
  6. getClassesDir(Class classWithinDir)
  7. getClassesExtending(Class rootClass, String pattern)
  8. getClassesInDirectory(File directory, String packageName, ClassLoader classLoader)
  9. getClassesLocation(Class cls)