Java Class Find getAllClasses(ClassLoader cl, String packageName)

Here you can find the source of getAllClasses(ClassLoader cl, String packageName)

Description

get All Classes

License

Open Source License

Declaration

public static List<String> getAllClasses(ClassLoader cl, String packageName) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 *     Copyright (C) 2017, 2018 wysohn//from w  w  w.j a  v a 2  s.co m
 *
 *     This program 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.
 *
 *     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 General Public License for more details.
 *
 *     You should have received a copy of the GNU General Public License
 *     along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *******************************************************************************/

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

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

import java.util.Enumeration;

import java.util.List;

import java.util.zip.ZipEntry;
import java.util.zip.ZipException;
import java.util.zip.ZipFile;

public class Main {
    public static List<String> getAllClasses(ClassLoader cl, String packageName) {
        packageName = packageName.replace('.', '/');

        List<String> classes = new ArrayList<>();

        URL[] urls = ((URLClassLoader) cl).getURLs();
        for (URL url : urls) {
            //System.out.println(url.getFile());
            File jar = new File(url.getFile());

            if (jar.isDirectory()) {
                File subdir = new File(jar, packageName);
                if (!subdir.exists())
                    continue;
                File[] files = subdir.listFiles();
                for (File file : files) {
                    if (!file.isFile())
                        continue;
                    if (file.getName().endsWith(".class"))
                        classes.add(file.getName().substring(0, file.getName().length() - 6).replace('/', '.'));
                }
            }

            else {
                // try to open as ZIP
                try {
                    ZipFile zip = new ZipFile(jar);
                    for (Enumeration<? extends ZipEntry> entries = zip.entries(); entries.hasMoreElements();) {
                        ZipEntry entry = entries.nextElement();
                        String name = entry.getName();
                        if (!name.startsWith(packageName))
                            continue;
                        if (name.endsWith(".class") && name.indexOf('$') < 0)
                            classes.add(name.substring(0, name.length() - 6).replace('/', '.'));
                    }
                    zip.close();
                } catch (ZipException e) {
                    //System.out.println("Not a ZIP: " + e.getMessage());
                } catch (IOException e) {
                    System.err.println(e.getMessage());
                }
            }
        }

        return classes;
    }
}

Related

  1. findClassesAnnotatedWith(String packageName, Class a)
  2. findClassesAssignableFrom(String packageName, Class assignableFrom)
  3. findClassLocation(Class context)
  4. findClassOriginFile(Class cls)
  5. getAllAssignedClass(Class cls)
  6. getAllClasses(String packageName)
  7. getAllClassesFromPackage(final String packageName)
  8. getAllClassesIn(String... packageNames)
  9. getAllClassesInPackage(String packageName)