集册 Java实例教程 返回指定包中的所有类。

返回指定包中的所有类。

欢马劈雪     最近更新时间:2020-01-02 10:19:05

426
返回指定包中的所有类。
/* from 
时 代 J a v a 公 众 号*/


//package com.nowjava;

import java.io.File;

import java.net.URL;

import java.util.ArrayList;


public class Main {

    /**

     * Returns all classes from specified package.

     * @param pckgname, full name of package.

     * @return array of classes.

     */

    public static Class[] getClasses(String packageName)

            throws ClassNotFoundException {

        ArrayList<Class> classes = new ArrayList<Class>();

        // Get a File object for the package
        /* 
        *来 自
         nowjava.com
        */

        File directory = null;

        try {

            ClassLoader classLoader = Thread.currentThread()

                    .getContextClassLoader();

            if (classLoader == null) {

                throw new ClassNotFoundException("Can't get class loader.");

            }

            //String path = '/' + pckgname.replace('.', '/');

            String path = packageName.replace('.', '/');

            URL resource = classLoader.getResource(path);

            if (resource == null) {

                throw new ClassNotFoundException("No resource for " + path);

            }

            directory = new File(resource.getFile());

        } catch (NullPointerException x) {

            throw new ClassNotFoundException(packageName + " (" + directory

                    + ") does not appear to be a valid package");

        }

        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(packageName + '.'

                       
展开阅读全文