读取项目里带指定注解的方法

package com.galaxy.mobile.api;

import com.galaxy.mobile.api.config.ApiAction;
import com.galaxy.mobile.common.utils.StringUtils;
import org.apache.commons.collections.CollectionUtils;
import org.apache.log4j.Logger;

import java.io.File;
import java.io.FileFilter;
import java.lang.annotation.Annotation;
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;

/**
 * Created by fengyiming on 2016/7/18.
 */
public class MobileInterfaceStudyTest {

    private final static Logger logger = Logger.getLogger("myTest");

    public static void main(String[] args) {
        try{
            //扫描的包
            String packName = "com.galaxy.mobile.api.action";
            //jar包
            packName = "com.galaxy.image";
            logger.error("----packName:"+packName);
            //文件夹里所有类的集合
            List<Class<?>> classes = new ArrayList<Class<?>>();
            //扫描的包的路径
            String dirPath = packName.replaceAll("\\.", "/");
            //查找所有给定名称的资源
            Enumeration<URL> dirs = Thread.currentThread().getContextClassLoader()
                    .getResources(dirPath);
            //是否需要递归遍历
            boolean isRecursive = true;
            //需要判定是否含有的注解
            Class<? extends Annotation> annotation = ApiAction.class;
            annotation = null;
            while (dirs.hasMoreElements()) {
                //获取该资源的URL
                URL url = dirs.nextElement();
                //判断该文件的文件类型
                String protocol = url.getProtocol();
                logger.error("----type:"+protocol);
                // 获取包的物理路径
                String filePath = URLDecoder.decode(url.getFile(), "UTF-8");
                if ("file".equals(protocol)) {
                    getClassList(classes, packName, filePath, isRecursive, annotation);
                }else if("jar".equals(protocol)){
                    getClassList(classes, packName, url,isRecursive, annotation);
                }
            }
            if(CollectionUtils.isNotEmpty(classes)){
                logger.error("-----classes all data start-----");
                for(int i=1;i<=classes.size();i++){
                    logger.error("---data " + i + "---" + classes.get(i - 1).getName());
                }
                logger.error("-----classes all data end-----");
            }
            logger.error("----the end-----");
        }catch (Exception e){
            logger.error("eee",e);
        }
    }

    /**
     * 获取包下所有的class类的集合
     * @param classList
     * @param packName
     * @param filePath
     * @param isRecursive
     */
    public static void getClassList(List<Class<?>> classList,String packName,String filePath,boolean isRecursive,Class<? extends Annotation> annotation){
        File[] files = fileFilterClass(filePath);
        logger.error("---filePath:"+filePath+",cout size:"+files.length);
        if(files != null) {
            for (File file:files) {
                if(file.isFile()){
                    logger.error("---fileName---" + file.getName());
                    String className = getClassName(packName, file.getName());
                    logger.error("----className :"+className);
                    addClass(classList, className, null);
                }else if(file.isDirectory()){
                    /** 以下不设置会导致实例化类的时候报错 */
                    //获取当前的包名称
                    String subPackName = packName+"."+file.getName();
                    //获取当前的包路径
                    String subPath = filePath+"/"+file.getName();
                    getClassList(classList,subPackName,subPath,isRecursive,annotation);
                }
            }
        }
    }

    public static void getClassList(List<Class<?>> classList,String packName,URL url,boolean isRecursive,Class<? extends Annotation> annotation){
        try{
            JarURLConnection jarURLConnection = (JarURLConnection)url.openConnection();
            JarFile jarFile = jarURLConnection.getJarFile();
            logger.error("----jar name:"+jarFile.getName());
            //此举会吧jar里所有的文件遍历出来
            Enumeration<JarEntry> jarEntries = jarFile.entries();
            while (jarEntries.hasMoreElements()){
                //jar里的每个文件对象(包括文件夹)
                JarEntry jarEntry = jarEntries.nextElement();
                String jarEntryName = jarEntry.getName();
                String classAllName = jarEntryName.replaceAll("/", "\\.");
                logger.error("----jar className"+classAllName);
                if(classAllName.endsWith(".class")){
                    int lastIndex = classAllName.lastIndexOf(".");
                    String className = classAllName.substring(0,lastIndex);
                    addClass(classList,className,annotation);
                }
            }
        }catch (Exception e){
            logger.error("---jar--error --",e);
        }
    }

    /**
     * 过滤class文件和文件夹
     * @param filePath
     * @return
     */
    private static File[] fileFilterClass(String filePath){
        if(StringUtils.isNullorBlank(filePath)){
            return null;
        }else{
            return new File(filePath).listFiles(new FileFilter() {
                @Override
                public boolean accept(File file) {
                    return ((file.isFile() && file.getName().endsWith(".class")) || file.isDirectory());
                }
            });
        }
    }

    /**
     * 获取class的类名和路径
     * @param path
     * @param fileName
     * @return
     */
    private static String getClassName(String path,String fileName){
        int lastIndex = fileName.lastIndexOf(".");
        if(lastIndex > 0){
            return path+"."+fileName.substring(0,lastIndex);
        }else{
            return "";
        }
    }

    private static void addClass(List<Class<?>> classList,String className,Class<? extends Annotation> annotation){
        if(!StringUtils.isNullorBlank(className)) {
            Class<?> classObject = null;
            try {
                classObject = Class.forName(className);
            } catch (ClassNotFoundException e) {
                logger.error("----class:" + className + "--cannot class.forname");
            }
            //类对象存在
            if(classObject != null) {
                //是否需要判断该类是否包含此注解
                if (annotation == null) {
                    classList.add(classObject);
                }else if(classObject.isAnnotationPresent(annotation)){
                    classList.add(classObject);
                }else{
                    logger.error("----class:" + className + "-not include--"+annotation.getName());
                }
            }
        }
    }
}
上一篇:无法使用SQL login去登陆SQL Server - 'Password did not match'


下一篇:C# 消息处理机制及自定义过滤方式