Java反射工具类

/**  
 * @Description: 反射工具类  
 */  
public class ReflectionUtil {  
  
    /**  
     * 定义类集合(用于存放所有加载的类)  
     */  
    private static final Set> CLASS_SET;  
  
    static {  
        //指定加载包路径  
        CLASS_SET = getClassSet("com.yaolong");  
    }  
  
    /**  
     * 获取类加载器  
     * @return  
     */  
    public static ClassLoader getClassLoader(){  
        return Thread.currentThread().getContextClassLoader();  
    }  
  
    /**  
     * 加载类  
     * @param className 类全限定名称  
     * @param isInitialized 是否在加载完成后执行静态代码块  
     * @return  
     */  
    public static Class loadClass(String className,boolean isInitialized) {  
        Class cls;  
        try {  
            cls = Class.forName(className,isInitialized,getClassLoader());  
        } catch (ClassNotFoundException e) {  
            throw new RuntimeException(e);  
        }  
        return cls;  
    }  
  
    public static Class loadClass(String className) {  
        return loadClass(className,true);  
    }  
  
    /**  
     * 获取指定包下所有类  
     * @param packageName  
     * @return  
     */  
    public static Set> getClassSet(String packageName) {  
        Set> classSet = new HashSet<>();  
        try {  
            Enumeration urls = getClassLoader().getResources(packageName.replace(".","/"));  
            while (urls.hasMoreElements()) {  
                URL url = urls.nextElement();  
                if (url != null) {  
                    String protocol = url.getProtocol();  
                    if (protocol.equals("file")) {  
                        String packagePath = url.getPath().replace("%20","");  
                        addClass(classSet,packagePath,packageName);  
                    } else if (protocol.equals("jar")) {  
                        JarURLConnection jarURLConnection = (JarURLConnection) url.openConnection();  
                        if (jarURLConnection != null) {  
                            JarFile jarFile = jarURLConnection.getJarFile();  
                            if (jarFile != null) {  
                                Enumeration jarEntries = jarFile.entries();  
                                while (jarEntries.hasMoreElements()) {  
                                    JarEntry jarEntry = jarEntries.nextElement();  
                                    String jarEntryName = jarEntry.getName();  
                                    if (jarEntryName.endsWith(".class")) {  
                                        String className = jarEntryName.substring(0, jarEntryName.lastIndexOf(".")).replaceAll("/", ".");  
                                        doAddClass(classSet,className);  
                                    }  
                                }  
                            }  
                        }  
                    }  
                }  
            }  
  
  
        } catch (IOException e) {  
            throw new RuntimeException(e);  
        }  
        return classSet;  
    }  
  
    private static void doAddClass(Set> classSet, String className) {  
        Class cls = loadClass(className,false);  
        classSet.add(cls);  
    }  
  
    private static void addClass(Set> classSet, String packagePath, String packageName) {  
        final File[] files = new File(packagePath).listFiles(new FileFilter() {  
            @Override  
            public boolean accept(File file) {  
                return (file.isFile() && file.getName().endsWith(".class")) || file.isDirectory();  
            }  
        });  
        for (File file : files) {  
            String fileName = file.getName();  
            if (file.isFile()) {  
                String className = fileName.substring(0, fileName.lastIndexOf("."));  
                if (StringUtils.isNotEmpty(packageName)) {  
                    className = packageName + "." + className;  
                }  
                doAddClass(classSet,className);  
            } else {  
                String subPackagePath = fileName;  
                if (StringUtils.isNotEmpty(packagePath)) {  
                    subPackagePath = packagePath + "/" + subPackagePath;  
                }  
                String subPackageName = fileName;  
                if (StringUtils.isNotEmpty(packageName)) {  
                    subPackageName = packageName + "." + subPackageName;  
                }  
                addClass(classSet,subPackagePath,subPackageName);  
            }  
        }  
    }  
  
  
    public static Set> getClassSet() {  
        return CLASS_SET;  
    }  
  
    /**  
     * 获取应用包名下某父类(或接口)的所有子类(或实现类)  
     * @param superClass  
     * @return  
     */  
    public static Set> getClassSetBySuper(Class superClass) {  
        Set> classSet = new HashSet<>();  
        for (Class cls : CLASS_SET) {  
            if (superClass.isAssignableFrom(cls) && !superClass.equals(cls)) {  
                classSet.add(cls);  
            }  
        }  
        return classSet;  
    }  
  
    /**  
     * 获取应用包名下带有某注解的类  
     * @param annotationClass  
     * @return  
     */  
    public static Set> getClassSetByAnnotation(Class annotationClass) {  
        Set> classSet = new HashSet<>();  
        for (Class cls : CLASS_SET) {  
            if (cls.isAnnotationPresent(annotationClass)) {  
                classSet.add(cls);  
            }  
        }  
        return classSet;  
    }  
  
}  

使用:

public static List getReceiptHandlerList(){  
        List receiptHandlerList = new ArrayList<>();  
        //获取IReceiptHandler接口的实现类  
        Set> classList = ReflectionUtil.getClassSetBySuper(IReceiptHandler.class);  
        if (classList != null && classList.size() > 0) {  
            for (Class clazz : classList) {  
                try {  
                    receiptHandlerList.add((IReceiptHandler)clazz.newInstance());  
                } catch ( Exception e) {  
                    e.printStackTrace();  
                }  
            }  
        }  
        return receiptHandlerList;  
    }  

 

你可能感兴趣的:(Java,java,开发语言)