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

📄 resolverutil.java

📁 在Struts2中的jar包xwork的源代码.版本为2.0.7
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* Copyright 2005-2006 Tim Fennell * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * *     http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package com.opensymphony.xwork2.util;import java.io.File;import java.io.FileInputStream;import java.io.IOException;import java.lang.annotation.Annotation;import java.net.URL;import java.net.URLDecoder;import java.util.Enumeration;import java.util.HashSet;import java.util.Set;import java.util.jar.JarEntry;import java.util.jar.JarInputStream;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;/** * <p>ResolverUtil is used to locate classes that are available in the/a class path and meet * arbitrary conditions. The two most common conditions are that a class implements/extends * another class, or that is it annotated with a specific annotation. However, through the use * of the {@link Test} class it is possible to search using arbitrary conditions.</p> * * <p>A ClassLoader is used to locate all locations (directories and jar files) in the class * path that contain classes within certain packages, and then to load those classes and * check them. By default the ClassLoader returned by *  {@code Thread.currentThread().getContextClassLoader()} is used, but this can be overridden * by calling {@link #setClassLoader(ClassLoader)} prior to invoking any of the {@code find()} * methods.</p> * * <p>General searches are initiated by calling the * {@link #find(com.opensymphony.xwork2.util.ResolverUtil.Test, String...)} ()} method and supplying * a package name and a Test instance. This will cause the named package <b>and all sub-packages</b> * to be scanned for classes that meet the test. There are also utility methods for the common * use cases of scanning multiple packages for extensions of particular classes, or classes * annotated with a specific annotation.</p> * * <p>The standard usage pattern for the ResolverUtil class is as follows:</p> * *<pre> *ResolverUtil&lt;ActionBean&gt; resolver = new ResolverUtil&lt;ActionBean&gt;(); *resolver.findImplementation(ActionBean.class, pkg1, pkg2); *resolver.find(new CustomTest(), pkg1); *resolver.find(new CustomTest(), pkg2); *Collection&lt;ActionBean&gt; beans = resolver.getClasses(); *</pre>  * * <p>This class was copied from Stripes - http://stripes.mc4j.org/confluence/display/stripes/Home * </p> *  * @author Tim Fennell */public class ResolverUtil<T> {    /** An instance of Log to use for logging in this class. */    private static final Log log = LogFactory.getLog(ResolverUtil.class);    /**     * A simple interface that specifies how to test classes to determine if they     * are to be included in the results produced by the ResolverUtil.     */    public static interface Test {        /**         * Will be called repeatedly with candidate classes. Must return True if a class         * is to be included in the results, false otherwise.         */        boolean matches(Class type);    }    /**     * A Test that checks to see if each class is assignable to the provided class. Note     * that this test will match the parent type itself if it is presented for matching.     */    public static class IsA implements Test {        private Class parent;        /** Constructs an IsA test using the supplied Class as the parent class/interface. */        public IsA(Class parentType) { this.parent = parentType; }        /** Returns true if type is assignable to the parent type supplied in the constructor. */        public boolean matches(Class type) {            return type != null && parent.isAssignableFrom(type);        }        @Override public String toString() {            return "is assignable to " + parent.getSimpleName();        }    }        /**     * A Test that checks to see if each class name ends with the provided suffix.     */    public static class NameEndsWith implements Test {        private String suffix;        /** Constructs a NameEndsWith test using the supplied suffix. */        public NameEndsWith(String suffix) { this.suffix = suffix; }        /** Returns true if type name ends with the suffix supplied in the constructor. */        public boolean matches(Class type) {            return type != null && type.getName().endsWith(suffix);        }        @Override public String toString() {            return "ends with the suffix " + suffix;        }    }    /**     * A Test that checks to see if each class is annotated with a specific annotation. If it     * is, then the test returns true, otherwise false.     */    public static class AnnotatedWith implements Test {        private Class<? extends Annotation> annotation;        /** Construts an AnnotatedWith test for the specified annotation type. */        public AnnotatedWith(Class<? extends Annotation> annotation) { this.annotation = annotation; }        /** Returns true if the type is annotated with the class provided to the constructor. */        public boolean matches(Class type) {            return type != null && type.isAnnotationPresent(annotation);        }        @Override public String toString() {            return "annotated with @" + annotation.getSimpleName();        }    }    /** The set of matches being accumulated. */    private Set<Class<? extends T>> matches = new HashSet<Class<?extends T>>();    /**     * The ClassLoader to use when looking for classes. If null then the ClassLoader returned     * by Thread.currentThread().getContextClassLoader() will be used.     */    private ClassLoader classloader;    /**     * Provides access to the classes discovered so far. If no calls have been made to     * any of the {@code find()} methods, this set will be empty.     *     * @return the set of classes that have been discovered.     */    public Set<Class<? extends T>> getClasses() {        return matches;    }    /**     * Returns the classloader that will be used for scanning for classes. If no explicit     * ClassLoader has been set by the calling, the context class loader will be used.     *     * @return the ClassLoader that will be used to scan for classes     */    public ClassLoader getClassLoader() {        return classloader == null ? Thread.currentThread().getContextClassLoader() : classloader;    }    /**     * Sets an explicit ClassLoader that should be used when scanning for classes. If none     * is set then the context classloader will be used.     *     * @param classloader a ClassLoader to use when scanning for classes     */    public void setClassLoader(ClassLoader classloader) { this.classloader = classloader; }    /**     * Attempts to discover classes that are assignable to the type provided. In the case     * that an interface is provided this method will collect implementations. In the case     * of a non-interface class, subclasses will be collected.  Accumulated classes can be     * accessed by calling {@link #getClasses()}.     *     * @param parent the class of interface to find subclasses or implementations of     * @param packageNames one or more package names to scan (including subpackages) for classes     */    public void findImplementations(Class parent, String... packageNames) {        if (packageNames == null) return;        Test test = new IsA(parent);

⌨️ 快捷键说明

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