Java Class from Package getClassesInPackage(String packageName)

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

Description

Given a package name, attempts to reflect to find all classes within the package on the local file system.

License

Open Source License

Parameter

Parameter Description
packageName a parameter

Return

Class list

Declaration

public static List<Class> getClassesInPackage(String packageName) 

Method Source Code

//package com.java2s;
/* Copyright 2012 Yaqiang Wang,
* yaqiang.wang@gmail.com//from ww w  .  ja v a2s  .  c o m
* 
* This library 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 2.1 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.
*/

import java.io.File;

import java.net.URL;

import java.util.ArrayList;

import java.util.List;

public class Main {
    /**
     * Given a package name, attempts to reflect to find all classes within the
     * package on the local file system.
     *
     * @param packageName
     * @return Class list
     */
    public static List<Class> getClassesInPackage(String packageName) {
        List<Class> classes = new ArrayList<>();
        String packageNameSlashed = "/" + packageName.replace(".", "/");
        // Get a File object for the package  
        URL directoryURL = Thread.currentThread().getContextClassLoader().getResource(packageNameSlashed);
        if (directoryURL == null) {
            System.out.println("Could not retrieve URL resource: " + packageNameSlashed);
            return classes;
        }

        String directoryString = directoryURL.getFile();
        if (directoryString == null) {
            System.out.println("Could not find directory for URL resource: " + packageNameSlashed);
            return classes;
        }

        File directory = new File(directoryString);
        if (directory.exists()) {
            // Get the list of the files contained in the package  
            String[] files = directory.list();
            for (String fileName : files) {
                // We are only interested in .class files  
                if (fileName.endsWith(".class")) {
                    // Remove the .class extension  
                    fileName = fileName.substring(0, fileName.length() - 6);
                    try {
                        classes.add(Class.forName(packageName + "." + fileName));
                    } catch (ClassNotFoundException e) {
                        System.out.println(packageName + "." + fileName + " does not appear to be a valid class.");
                    }
                }
            }
        } else {
            System.out.println(packageName + " does not appear to exist as a valid package on the file system.");
        }
        return classes;
    }
}

Related

  1. getClassesForPackage(Package pkg)
  2. getClassesForPackage(String packageName)
  3. getClassesForPackage(String packageName)
  4. getClassesForPackage(String pckgname)
  5. getClassesForPackage(String pckgname)
  6. getClassesInPackage(String packageName)
  7. getClassesInPackage(String packageName)
  8. getClassesInPackage(String pckgname)
  9. getClassesInPackage(String targetPackage)