find Classes In Dir Package - Java Reflection

Java examples for Reflection:package

Description

find Classes In Dir Package

Demo Code


import java.io.File;
import java.io.FileFilter;
import java.io.FilenameFilter;
import java.io.IOException;
import java.net.JarURLConnection;
import java.net.URL;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.regex.Pattern;

public class Main{
    private static void findClassesInDirPackage(String packageName,
            List<String> included, List<String> excluded,
            String packagePath, final boolean recursive,
            List<String> classes) {
        File dir = new File(packagePath);

        if (!dir.exists() || !dir.isDirectory()) {
            return;
        }//from w w  w. ja va2  s .  co m

        File[] dirfiles = dir.listFiles(new FileFilter() {
            public boolean accept(File file) {
                return (recursive && file.isDirectory())
                        || (file.getName().endsWith(".class"));
            }
        });

        // PackageUtils.log("Looking for test classes in the directory: " +
        // dir);
        for (File file : dirfiles) {
            if (file.isDirectory() && recursive) {
                findClassesInDirPackage(packageName + "." + file.getName(),
                        included, excluded, file.getAbsolutePath(),
                        recursive, classes);
            } else {
                String className = file.getName().substring(0,
                        file.getName().length() - 6);
                // PackageUtils.log("Found class " + className + ", seeing it if
                // it's included or excluded");
                includeOrExcludeClass(packageName, className, included,
                        excluded, classes);
            }
        }
    }
    private static void includeOrExcludeClass(String packageName,
            String className, List<String> included, List<String> excluded,
            List<String> classes) {
        if (isIncluded(className, included, excluded)) {
            //Utils.log("... Including class " + className);
            //classes.add(packageName + '.' + className);
            classes.add(className);
        }
        //        }       else {
        //Utils.log("... Excluding class " + className);
        //        }
    }
    /**
     * @return true if name should be included.
     */
    private static boolean isIncluded(String name, List<String> included,
            List<String> excluded) {
        boolean result = false;

        //
        // If no includes nor excludes were specified, return true.
        //
        if (included.size() == 0 && excluded.size() == 0) {
            result = true;
        } else {
            boolean isIncluded = PackageUtil.find(name, included);
            boolean isExcluded = PackageUtil.find(name, excluded);
            if (isIncluded && !isExcluded) {
                result = true;
            } else if (isExcluded) {
                result = false;
            } else {
                result = included.size() == 0;
            }
        }
        return result;
    }
    private static boolean find(String name, List<String> list) {
        for (String regexpStr : list) {
            if (Pattern.matches(regexpStr, name))
                return true;
        }
        return false;
    }
}

Related Tutorials