Java Class from Package getClassNameByPackage(String packageName)

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

Description

get Class Name By Package

License

Apache License

Declaration

public static List<String> getClassNameByPackage(String packageName) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.io.File;
import java.net.URL;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

public class Main {

    public static List<String> getClassNameByPackage(String packageName) {
        List<String> fileNames = null;
        ClassLoader loader = Thread.currentThread().getContextClassLoader();
        String packagePath = packageName.replace(".", "/");
        URL url = loader.getResource(packagePath);
        if (url != null) {
            String type = url.getProtocol();
            if (type.equals("file")) {
                return fileNames = getClassNameByFile(packageName, URLDecoder.decode(url.getPath()), null);
            } else {
                return null;
            }/*from w  w w . j a  v a  2s.  c  o  m*/
        } else {
            return null;
        }
    }

    public static List<String> getClassNameByFile(String packageName, String filePath, List<String> className) {
        Properties properties = new Properties(System.getProperties());
        String FileSeparator = properties.getProperty("file.separator");
        List<String> myClassName = new ArrayList<String>();
        File file = new File(filePath);
        File[] childFiles = file.listFiles();
        for (File childFile : childFiles) {
            if (childFile.isDirectory()) {
                myClassName.addAll(getClassNameByFile(packageName, childFile.getPath(), myClassName));
            } else {
                String childFilePath = childFile.getPath();
                if (childFilePath.endsWith(".class")) {
                    int lastIndex = -1;
                    if (FileSeparator.equals("/")) {
                        lastIndex = childFilePath.lastIndexOf('/');
                    } else if (FileSeparator.equals("\\")) {
                        lastIndex = childFilePath.lastIndexOf('\\');
                    } else {
                        lastIndex = childFilePath.lastIndexOf('/');
                    }
                    int endIndex = childFilePath.lastIndexOf(".class");
                    String busServlet = childFilePath.substring(lastIndex + 1, endIndex);
                    myClassName.add(packageName + "." + busServlet);
                }
            }
        }

        return myClassName;
    }
}

Related

  1. getClassesInPackage(String packageName)
  2. getClassesInPackage(String packageName)
  3. getClassesInPackage(String packageName)
  4. getClassesInPackage(String pckgname)
  5. getClassesInPackage(String targetPackage)
  6. getClassNamesByPkg(String pkg)
  7. getClassNamesForPackage(final Package p)
  8. getClassNamesFromPackage(String packageName)
  9. getClassNamesFromPackage(String packageName)