⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 resolverutil.java

📁 在Struts2中的jar包xwork的源代码.版本为2.0.7
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
        for (String pkg : packageNames) {            findInPackage(test, pkg);        }    }        /**     * Attempts to discover classes who's name ends with the provided suffix. Accumulated classes can be     * accessed by calling {@link #getClasses()}.     *     * @param suffix The class name suffix to match     * @param packageNames one or more package names to scan (including subpackages) for classes     */    public void findSuffix(String suffix, String... packageNames) {        if (packageNames == null) return;        Test test = new NameEndsWith(suffix);        for (String pkg : packageNames) {            findInPackage(test, pkg);        }    }    /**     * Attempts to discover classes that are annotated with to the annotation. Accumulated     * classes can be accessed by calling {@link #getClasses()}.     *     * @param annotation the annotation that should be present on matching classes     * @param packageNames one or more package names to scan (including subpackages) for classes     */    public void findAnnotated(Class<? extends Annotation> annotation, String... packageNames) {        if (packageNames == null) return;        Test test = new AnnotatedWith(annotation);        for (String pkg : packageNames) {            findInPackage(test, pkg);        }    }        /**     * Attempts to discover classes that pass the test. Accumulated     * classes can be accessed by calling {@link #getClasses()}.     *     * @param test the test to determine matching classes     * @param packageNames one or more package names to scan (including subpackages) for classes     */    public void find(Test test, String... packageNames) {        if (packageNames == null) return;        for (String pkg : packageNames) {            findInPackage(test, pkg);        }    }    /**     * Scans for classes starting at the package provided and descending into subpackages.     * Each class is offered up to the Test as it is discovered, and if the Test returns     * true the class is retained.  Accumulated classes can be fetched by calling     * {@link #getClasses()}.     *     * @param test an instance of {@link Test} that will be used to filter classes     * @param packageName the name of the package from which to start scanning for     *        classes, e.g. {@code net.sourceforge.stripes}     */    public void findInPackage(Test test, String packageName) {        packageName = packageName.replace('.', '/');        ClassLoader loader = getClassLoader();        Enumeration<URL> urls;        try {            urls = loader.getResources(packageName);        }        catch (IOException ioe) {            log.warn("Could not read package: " + packageName, ioe);            return;        }        while (urls.hasMoreElements()) {            try {                String urlPath = urls.nextElement().getFile();                urlPath = URLDecoder.decode(urlPath, "UTF-8");                // If it's a file in a directory, trim the stupid file: spec                if ( urlPath.startsWith("file:") ) {                    urlPath = urlPath.substring(5);                }                // Else it's in a JAR, grab the path to the jar                if (urlPath.indexOf('!') > 0) {                    urlPath = urlPath.substring(0, urlPath.indexOf('!'));                }                log.info("Scanning for classes in [" + urlPath + "] matching criteria: " + test);                File file = new File(urlPath);                if ( file.isDirectory() ) {                    loadImplementationsInDirectory(test, packageName, file);                }                else {                    loadImplementationsInJar(test, packageName, file);                }            }            catch (IOException ioe) {                log.warn("could not read entries", ioe);            }        }    }    /**     * Finds matches in a physical directory on a filesystem.  Examines all     * files within a directory - if the File object is not a directory, and ends with <i>.class</i>     * the file is loaded and tested to see if it is acceptable according to the Test.  Operates     * recursively to find classes within a folder structure matching the package structure.     *     * @param test a Test used to filter the classes that are discovered     * @param parent the package name up to this directory in the package hierarchy.  E.g. if     *        /classes is in the classpath and we wish to examine files in /classes/org/apache then     *        the values of <i>parent</i> would be <i>org/apache</i>     * @param location a File object representing a directory     */    private void loadImplementationsInDirectory(Test test, String parent, File location) {        File[] files = location.listFiles();        StringBuilder builder = null;        for (File file : files) {            builder = new StringBuilder(100);            builder.append(parent).append("/").append(file.getName());            String packageOrClass = ( parent == null ? file.getName() : builder.toString() );            if (file.isDirectory()) {                loadImplementationsInDirectory(test, packageOrClass, file);            }            else if (file.getName().endsWith(".class")) {                addIfMatching(test, packageOrClass);            }        }    }    /**     * Finds matching classes within a jar files that contains a folder structure     * matching the package structure.  If the File is not a JarFile or does not exist a warning     * will be logged, but no error will be raised.     *     * @param test a Test used to filter the classes that are discovered     * @param parent the parent package under which classes must be in order to be considered     * @param jarfile the jar file to be examined for classes     */    private void loadImplementationsInJar(Test test, String parent, File jarfile) {        try {            JarEntry entry;            JarInputStream jarStream = new JarInputStream(new FileInputStream(jarfile));            while ( (entry = jarStream.getNextJarEntry() ) != null) {                String name = entry.getName();                if (!entry.isDirectory() && name.startsWith(parent) && name.endsWith(".class")) {                    addIfMatching(test, name);                }            }        }        catch (IOException ioe) {            log.error("Could not search jar file '" + jarfile + "' for classes matching criteria: " +                      test + " due to an IOException", ioe);        }    }    /**     * Add the class designated by the fully qualified class name provided to the set of     * resolved classes if and only if it is approved by the Test supplied.     *     * @param test the test used to determine if the class matches     * @param fqn the fully qualified name of a class     */    protected void addIfMatching(Test test, String fqn) {        try {            String externalName = fqn.substring(0, fqn.indexOf('.')).replace('/', '.');            ClassLoader loader = getClassLoader();            if (log.isDebugEnabled()) {                log.debug("Checking to see if class " + externalName + " matches criteria [" + test + "]");            }            Class type = loader.loadClass(externalName);            if (test.matches(type) ) {                matches.add( (Class<T>) type);            }        }        catch (Throwable t) {            log.warn("Could not examine class '" + fqn + "' due to a " +                     t.getClass().getName() + " with message: " + t.getMessage());        }    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -