读取项目里含指定注解的方法 @interface妙用
2016-07-19 14:30
369 查看
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); // 4000 是否需要递归遍历 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()); } } } } }
相关文章推荐
- 1.1 javascript组成
- a标签的target指向iframe的name和id的区别
- eclipse运行代码时——类路径引用的归档不存在"XXXXX.jar"问题解决
- kdumpctl: No memory reserved for crash kernel.
- WAV文件格式解析
- 农业垂直搜索引擎五》索引与检索模块的设计与实现
- 关于 SimpleDateFormat 的非线程安全问题及其解决方案
- 约束与索引
- 杭电ACM—HDU1200
- 对多态的理解
- MySQL查看当前库所用引擎
- 10A - Power Consumption Calculation
- Object
- SDUT2133数据结构实验之栈三:后缀式求值(栈)
- CMD-NET命令详解
- 更新按钮上面文字,并根据变化判断活动
- 168. Excel Sheet Column Title
- Exchange & Lotus Notes邮件系统在线批量迁移账号及邮件
- Intellij IDEA 启动出现“前言中不允许有内容”
- python正则表达式--基本用法和函数(一个例子)