集册 Java实例教程 从指定包中的JarFile获取所有Class文件

从指定包中的JarFile获取所有Class文件

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

771
从指定包中的JarFile获取所有Class文件

/*

 * This file is part of VIUtils.

 *

 * Copyright ? 2012-2015 Visual Illusions Entertainment

 *

 * VIUtils 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 (at your option) any later version.

 *

 * This library 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 Lesser General Public License for more details.

 *

 * You should have received a copy of the GNU General Public License along with this library.

 * If not, see http://www.gnu.org/licenses/lgpl.html.

 */

import java.io.IOException;

import java.io.InputStream;

import java.net.URISyntaxException;
/**来自 
 时代Java - N o w  J a v a . c o m**/

import java.security.CodeSource;

import java.security.cert.Certificate;

import java.util.ArrayList;

import java.util.Enumeration;

import java.util.jar.JarEntry;

import java.util.jar.JarFile;

import java.util.jar.Manifest;


public class Main{

    /**

     * Gets all {@link Class} files from a {@link JarFile} in a specified package

     *

     * @param jarFile

     *         the {@link JarFile} to get classes from

     * @param packageName

     *         the name of the package to get classes from

     *

     * @return {@link Class} array

     *

     * @throws java.lang.ClassNotFoundException

     *         if the jar doesn't appear on the class-path and subsequently unable to load/find classes

     * @throws java.lang.NullPointerException

     *         if {@code jarFile} or {@code packageName} is null

     * @throws java.lang.IllegalArgumentException

     *         if {@code packageName} is empty

     */

    public static Class[] getClassesInPackage(JarFile jarFile,/*n o w j a v a . c o m 提 供*/

            String packageName) throws ClassNotFoundException {



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

        Enumeration<JarEntry> entries = jarFile.entries();

        while (entries.hasMoreElements()) {

            JarEntry entry = entries.nextElement();

            String entName = entry.getName().replace(
展开阅读全文