Java Class from Package getClasses(String packageName)

Here you can find the source of getClasses(String packageName)

Description

get Classes

License

Open Source License

Declaration

private static Class[] getClasses(String packageName)
            throws ClassNotFoundException, IOException 

Method Source Code

//package com.java2s;
/*//w w  w  .  j a v  a2  s.c  o  m
 * JBoss, Home of Professional Open Source
 * Copyright 2008, Red Hat, Inc., and others contributors as indicated
 * by the @authors tag. All rights reserved.
 * See the copyright.txt in the distribution for a
 * full listing of individual contributors.
 * This copyrighted material is made available to anyone wishing to use,
 * modify, copy, or redistribute it subject to the terms and conditions
 * of the GNU Lesser General Public License, v. 2.1.
 * This program is distributed in the hope that it will be useful, but WITHOUT A
 * 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 Lesser General Public License,
 * v.2.1 along with this distribution; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA  02110-1301, USA.
 */

import java.io.File;
import java.io.IOException;
import java.net.URL;

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

public class Main {
    private static Class[] getClasses(String packageName)
            throws ClassNotFoundException, IOException {
        ClassLoader classLoader = Thread.currentThread()
                .getContextClassLoader();
        assert classLoader != null;
        String path = packageName.replace('.', '/');
        Enumeration<URL> resources = classLoader.getResources(path);
        List<File> dirs = new ArrayList<File>();
        while (resources.hasMoreElements()) {
            URL resource = resources.nextElement();
            dirs.add(new File(java.net.URLDecoder.decode(resource.getFile()
                    .toString())));
        }
        ArrayList<Class> classes = new ArrayList<Class>();
        for (File directory : dirs) {
            File[] files = directory.listFiles();
            for (File file : files) {
                if (file.getName().endsWith(".class")
                        && file.getName().indexOf('$') < 0) {
                    classes.add(Class.forName(packageName
                            + '.'
                            + file.getName().substring(0,
                                    file.getName().length() - 6)));
                }
            }
        }
        return classes.toArray(new Class[classes.size()]);
    }
}

Related

  1. getClasses(String packageName)
  2. getClasses(String packageName)
  3. getClasses(String packageName)
  4. getClasses(String packageName)
  5. getClasses(String packageName)
  6. getClasses(String packageName)
  7. getClasses(String packageName)