Java Class from Package getClassesInPackage(String pckgname)

Here you can find the source of getClassesInPackage(String pckgname)

Description

get Classes In Package

License

Open Source License

Declaration

@SuppressWarnings({ "rawtypes" })
    public static Class[] getClassesInPackage(String pckgname) throws ClassNotFoundException 

Method Source Code

//package com.java2s;
/*//from  w  w w.  ja v a 2s . com
 * Copyright 2010, 2011 Jingjing Li.
 *
 * This file is part of jplot2d.
 *
 * jplot2d is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or any later version.
 *
 * jplot2d 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 Lesser Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with jplot2d. If not, see <http://www.gnu.org/licenses/>.
 */

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

import java.net.URISyntaxException;
import java.net.URL;

import java.util.ArrayList;

import java.util.Enumeration;

public class Main {
    @SuppressWarnings({ "rawtypes" })
    public static Class[] getClassesInPackage(String pckgname) throws ClassNotFoundException {
        ArrayList<Class> classes = new ArrayList<>();
        // Get a File object for the package

        ClassLoader cld = Thread.currentThread().getContextClassLoader();
        if (cld == null) {
            throw new ClassNotFoundException("Can't get class loader.");
        }
        String path = pckgname.replace('.', '/');
        Enumeration<URL> resources = null;
        try {
            resources = cld.getResources(path);
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        if (resources == null || !resources.hasMoreElements()) {
            throw new ClassNotFoundException("No resource for " + path);
        }
        while (resources.hasMoreElements()) {
            URL url = resources.nextElement();
            File directory = null;
            try {
                directory = new File(url.toURI());
            } catch (URISyntaxException e) {
                e.printStackTrace();
            }
            if (directory != null && directory.exists()) {
                // Get the list of the files contained in the package
                String[] files = directory.list();
                for (String file : files) {
                    // we are only interested in .class files
                    if (file.endsWith(".class")) {
                        // removes the .class extension
                        classes.add(Class.forName(pckgname + '.' + file.substring(0, file.length() - 6)));
                    }
                }
            } else {
                throw new ClassNotFoundException(pckgname + " does not appear to be a valid package");
            }
        }

        return classes.toArray(new Class[classes.size()]);
    }
}

Related

  1. getClassesForPackage(String pckgname)
  2. getClassesForPackage(String pckgname)
  3. getClassesInPackage(String packageName)
  4. getClassesInPackage(String packageName)
  5. getClassesInPackage(String packageName)
  6. getClassesInPackage(String targetPackage)
  7. getClassNameByPackage(String packageName)
  8. getClassNamesByPkg(String pkg)
  9. getClassNamesForPackage(final Package p)