Java Class from Package getClasses(String pckgname)

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

Description

list Classes inside a given package.

License

Open Source License

Parameter

Parameter Description
pckgname String name of a Package, EG "java.lang"

Exception

Parameter Description
ClassNotFoundException if the Package is invalid

Return

Class[] classes inside the root of the given package

Declaration

private static Class[] getClasses(String pckgname) throws ClassNotFoundException 

Method Source Code


//package com.java2s;
/*//from   w ww . jav a  2s.c o m
 * $Id$
 * --------------------------------------------------------------------------------------
 * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.com
 *
 * The software in this package is published under the terms of the CPAL v1.0
 * license, a copy of which has been included with this distribution in the
 * LICENSE.txt file.
 */

import java.io.File;
import java.net.URL;
import java.util.ArrayList;

public class Main {
    /** It refers to suffix leght refer to a class name ( ".class" , 6 chars) */
    private static final int SUFFIX_CLASS_LENGTH = 6;

    /**
     * list Classes inside a given package.
     * 
     * @param pckgname String name of a Package, EG "java.lang"
     * @return Class[] classes inside the root of the given package
     * @throws ClassNotFoundException if the Package is invalid
     */
    private static Class[] getClasses(String pckgname) throws ClassNotFoundException {

        ArrayList classes = new ArrayList();
        // Get a File object for the package
        File directory = null;
        try {
            URL str = Thread.currentThread().getContextClassLoader().getResource(pckgname.replace('.', '/'));
            System.out.println("instance advice..." + new File(str.getFile()).getName());
            directory = new File(str.getFile());

        } catch (NullPointerException x) {
            throw new ClassNotFoundException(pckgname + " 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 *Advice.class files
                if (files[i].endsWith("Advice.class")) {
                    // removes the .class extension
                    classes.add(Class.forName(
                            pckgname + '.' + files[i].substring(0, files[i].length() - SUFFIX_CLASS_LENGTH)));
                }
            }
        } else {
            throw new ClassNotFoundException(pckgname + " does not appear to be a valid package");
        }
        Class[] classesA = new Class[classes.size()];
        classes.toArray(classesA);
        return classesA;
    }
}

Related

  1. getClasses(String packageName)
  2. getClasses(String packageName)
  3. getClasses(String packageName)
  4. getClasses(String packageName, Class inheritedType)
  5. getClasses(String pckgname)
  6. getClasses(String pkg)
  7. getClasses(String pkg)
  8. getClassesForPackage(final String packageName)
  9. getClassesForPackage(Package pkg)