Java Class Loader getFilesByPackgeName(String packageName, File _file, ClassLoader classLoader, List list)

Here you can find the source of getFilesByPackgeName(String packageName, File _file, ClassLoader classLoader, List list)

Description

get Files By Packge Name

License

Open Source License

Declaration

private static List<File> getFilesByPackgeName(String packageName, File _file, ClassLoader classLoader,
            List<File> list) throws IOException 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

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 {
    public static List<File> getFilesByPackgeName(String packageName) throws IOException {
        return getFilesByPackgeName(packageName, null, Thread.currentThread().getContextClassLoader(),
                new ArrayList<File>());
    }/*w  ww  .  j  a  va  2  s .  c om*/

    private static List<File> getFilesByPackgeName(String packageName, File _file, ClassLoader classLoader,
            List<File> list) throws IOException {
        _file = _file != null ? _file : getPackageFolder(packageName, classLoader);

        if (_file != null) {
            File[] files = _file.listFiles();

            for (File file : files) {
                if (file.isDirectory()) {
                    if (!file.getName().contains("."))
                        getFilesByPackgeName(packageName + '.' + file.getName(), _file, classLoader, list);
                } else
                    list.add(file);
            }
        }

        return list;
    }

    public static File getPackageFolder(String packageName) throws IOException {
        return getPackageFolder(packageName, Thread.currentThread().getContextClassLoader());
    }

    private static File getPackageFolder(String packageName, ClassLoader classLoader) throws IOException {
        Enumeration<URL> resources = classLoader.getResources(packageName.replace('.', '/'));

        if (resources.hasMoreElements())
            return new File(resources.nextElement().getFile());

        return null;
    }
}

Related

  1. getClassLoaderStack(ClassLoader cl)
  2. getClassLocation(Class clazz)
  3. getClassLocation(String className)
  4. getCustomClassloader(List entries)
  5. getFileFromClassLoader(String fileName)
  6. getJvmExtClassLoader()
  7. getLoadingDir(Class clazz)
  8. getManifest(ClassLoader cl, String extension)
  9. getPackageFolder(String packageName, ClassLoader classLoader)