读取项目里含指定注解的方法 @interface妙用
来源:互联网 发布:淘宝店铺4钻能卖多少 编辑:程序博客网 时间:2024/05/16 04:52
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()); } } } }}
0 0
- 读取项目里含指定注解的方法 @interface妙用
- 隐藏注解里的http方法过滤器
- 如何读取项目里的.properties文件
- 怎么利用Spring利用注解读取配置文件里的数据
- vim里gf的妙用
- android读取指定文件夹里的所有文件
- Java 注解的读取注解信息的方法
- 读取指定XML节点的方法(XMLtextreader)
- StreamReader.ReadLine 的方法读取指定行
- 读取***.properties配置文件里数据的方法
- Java注解的使用@interface:
- Java注解@interface的用法
- Springmvc @interface注解的使用
- 注解:@interface 自定义注解的语法
- getItemValue方法的妙用
- 方法选择器的妙用
- 反射和注解的妙用——struts2基于方法的权限控制
- 反射和注解的妙用——struts2基于方法的权限控制
- WAV文件格式解析
- react基于webpack和babel以及es6的项目搭建
- kdumpctl: No memory reserved for crash kernel.
- 1.1 javascript组成
- a标签的target指向iframe的name和id的区别
- 读取项目里含指定注解的方法 @interface妙用
- gluLookAt 函数详解
- 多态
- sudo保持前用用户的env环境变量
- 35.You executed the command to create a password file in the database server:
- CodeForces 447A DZY Loves Hash
- SVM再次总结 - 3 - 线性支持向量机
- 搞了个miniblink的github站点
- 继承