Java Class from Package getClassNamesPackage(String pckgname)

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

Description

get Class Names Package

License

Apache License

Declaration

private static Set<String> getClassNamesPackage(String pckgname) throws ClassNotFoundException, IOException 

Method Source Code

//package com.java2s;
/*/*from   w ww . j a  v  a 2s . c  o m*/
 * Copyright 2005-2009 the original author or authors.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.io.File;

import java.io.IOException;

import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLDecoder;

import java.util.Enumeration;
import java.util.HashSet;
import java.util.LinkedList;

import java.util.Queue;
import java.util.Set;

public class Main {
    private static Set<String> getClassNamesPackage(String pckgname) throws ClassNotFoundException, IOException {
        // This will hold a list of directories matching the pckgname. There may be
        // more than one if a package is split over multiple jars/paths
        Queue<File> directories = new LinkedList<File>();
        try {
            ClassLoader cld = Thread.currentThread().getContextClassLoader();
            if (cld == null) {
                throw new ClassNotFoundException("Can't get class loader.");
            }
            String path = pckgname.replace('.', '/');
            // Ask for all resources for the path
            Enumeration<URL> resources = cld.getResources(path);
            while (resources.hasMoreElements()) {
                directories.add(new File(URLDecoder.decode(resources.nextElement().getPath(), "UTF-8")));
            }
        } catch (NullPointerException x) {
            throw new ClassNotFoundException(
                    pckgname + " does not appear to be a valid package (Null pointer exception)");
        } catch (UnsupportedEncodingException encex) {
            throw new ClassNotFoundException(
                    pckgname + " does not appear to be a valid package (Unsupported encoding)");
        } catch (IOException ioex) {
            throw new ClassNotFoundException(
                    "IOException was thrown when trying to get all resources for " + pckgname);
        }

        Set<String> classes = new HashSet<String>();
        // For every directory identified capture all the .class files
        while (!directories.isEmpty()) {
            File directory = directories.poll();
            if (directory.exists()) {
                // Get the list of the files contained in the package
                File[] files = directory.listFiles();
                for (File file : files) {
                    // we are only interested in .class files
                    if (file.getCanonicalPath().endsWith(".class")) {
                        String fileName = file.getPath().substring(directory.getPath().length() + 1);
                        pckgname = file.getPath()
                                .substring(file.getPath().indexOf(File.separator + "nl" + File.separator) + 1);
                        pckgname = pckgname.substring(0, pckgname.lastIndexOf(File.separator))
                                .replaceAll("\\" + File.separator, ".");
                        // if (!fileName.matches("(.+)\\$\\d\\.class"))
                        // removes the .class extension
                        classes.add(fileName.substring(0, fileName.length() - 6));
                    }
                    // Add subdirs
                    if (file.isDirectory()) {
                        directories.add(file);
                    }
                }
            } else {
                throw new ClassNotFoundException(
                        pckgname + " (" + directory.getPath() + ") does not appear to be a valid package");
            }
        }
        return classes;
    }
}

Related

  1. getClassNamesByPkg(String pkg)
  2. getClassNamesForPackage(final Package p)
  3. getClassNamesFromPackage(String packageName)
  4. getClassNamesFromPackage(String packageName)
  5. getClassNamesFromPackage(String packageName)