如何获取包下所有类中的注解的值(java工具类)

获取包下所有类中注解的值 作用:
这个工具类主要的作用就是获取类中的注解的值。
应用场景:
做权限的时候获取@RequestMapping(); 的值,自动添加到数据库中。

/*** getRequestMappingValue方法描述:* 作者:thh* 日期:2016年7月18日 下午5:41:00* 异常对象:@param packageName* 异常对象:@return */public static List getRequestMappingValue(String packageName) {GetAnnotationValueUtil getAnnotationValueUtil = new GetAnnotationValueUtil(); //第一个class类的集合List> classes = new ArrayList>(); //是否循环迭代boolean recursive = true; //获取包的名字 并进行替换String packageDirName = packageName.replace('.', '/'); //定义一个枚举的集合 并进行循环来处理这个目录下的文件 Enumeration dirs; try {//读取指定package下的所有classdirs = Thread.currentThread().getContextClassLoader().getResources(packageDirName); while (dirs.hasMoreElements()){URL url = dirs.nextElement(); //得到协议的名称String protocol = url.getProtocol(); //判断是否以文件的形式保存在服务器上if ("file".equals(protocol)) {//获取包的物理路径String filePath = URLDecoder.decode(url.getFile(), "UTF-8"); //以文件的方式扫描整个包下的文件 并添加到集合中findAndAddClassesInPackageByFile(packageName, filePath, recursive, classes); } }} catch (IOException e) { e.printStackTrace(); } List stringList = new ArrayList(); for (Class clazz : classes) {//循环获取所有的类Class c = clazz; //获取类的所有方法Method[] methods = c.getMethods(); for (Method method : methods) {//获取RequestMapping注解RequestMapping annotation = method.getAnnotation(RequestMapping.class); if (annotation != null) {//获取注解的value值String[] value = https://www.it610.com/article/annotation.value(); for (String string : value) {//放入List集合stringList.add(string); }}}}return stringList; }/*** findAndAddClassesInPackageByFile方法描述:* 作者:thh* 日期:2016年7月18日 下午5:41:12* 异常对象:@param packageName* 异常对象:@param packagePath* 异常对象:@param recursive* 异常对象:@param classes */public static void findAndAddClassesInPackageByFile(String packageName, String packagePath, final boolean recursive, List> classes){//获取此包的目录 建立一个FileFile dir = new File(packagePath); //如果不存在或者 也不是目录就直接返回if (!dir.exists() || !dir.isDirectory()) {return; }//如果存在 就获取包下的所有文件 包括目录File[] dirfiles = dir.listFiles(new FileFilter() {//自定义过滤规则 如果可以循环(包含子目录) 或则是以.class结尾的文件(编译好的java类文件)public boolean accept(File file) { return (recursive && file.isDirectory()) || (file.getName().endsWith(".class")); }}); //循环所有文件for (File file : dirfiles) { //如果是目录 则继续扫描if (file.isDirectory()) {findAndAddClassesInPackageByFile(packageName + "." + file.getName(),file.getAbsolutePath(),recursive,classes); }else {//如果是java类文件 去掉后面的.class 只留下类名String className = file.getName().substring(0, file.getName().length() - 6); try {//添加到集合中去classes.add(Thread.currentThread().getContextClassLoader().loadClass(packageName + "." + className)); } catch (ClassNotFoundException e) {e.printStackTrace(); }}} }

java 类,变量,方法上注解值的获取 首先定义三个注解类, 分别适用于类,成员变量, 方法
@Target(ElementType.TYPE)@Retention(RetentionPolicy.RUNTIME)public @interface LeiMode { public int value() default 1; }

@Target(ElementType.FIELD)@Retention(RetentionPolicy.RUNTIME)public @interface FiledMode { public int value() default 1; }

@Target(ElementType.METHOD)@Retention(RetentionPolicy.RUNTIME)public @interface TreahMode { public int value() default 1; }

然后,定义一个类,使用了注解
@LeiMode(5)public class AnnotationDemo { @FiledMode(10) private int itest; @TreahMode() private void test(){}}

1.获取类上的注解值
LeiMode annotation = AnnotationDemo.class.getAnnotation(LeiMode.class); System.out.println(annotation.value());

2.获取所有变量,并获取指定方法上的注解信息
Field[] fields = AnnotationDemo.class.getDeclaredFields(); Field field = null; for(Field f : fields){if(f.getName().equals("itest")){field = f; break; }}FiledMode annotation = field.getAnnotation(FiledMode.class); System.out.println(annotation.value());

3.获取指定变量上的注解信息
Field field = AnnotationDemo.class.getDeclaredField("itest"); FiledMode annotation = field.getAnnotation(FiledMode.class); System.out.println(annotation.value());

4.获取所有方法,并获取指定方法上的注解信息
Method[] methods = AnnotationDemo.class.getDeclaredMethods(); //可以获取私有方法和公有方法, getMethods() 获取公有方法Method meth = null; for(Method method : methods){if(method.getName().equals("test")){meth = method; break; }}Annotation annotation = meth.getAnnotations()[0]; TreahMode mode = (TreahMode) annotation; System.out.println(mode.value());

5.获取指定方法上的注解信息
Method method = AnnotationDemo.class.getDeclaredMethod("test", null); //可以获取私有方法和公有方法System.out.println(method); Annotation[] annotations = method.getAnnotations(); Annotation annotation = annotations[0]; System.out.println(annotation); TreahMode mode = (TreahMode) annotation; System.out.println(mode.value());

【如何获取包下所有类中的注解的值(java工具类)】以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

    推荐阅读