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

网友投稿 473 2022-12-22

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

获取包下所有类中注解的值

作用:

这个工具类主要的作用就是获取类中的注解的值。

应用场景:

做权限的时候获取@RequestMapping();的值,自动添加到数据库中。

/**

* getRequestMappingValue方法描述:

* 日期:2016年7月18日 下午5:41:00

* 异常对象:@param packageName

* 异常对象:@return

*/

public static List getRequesthttp://MappingValue(String packageName) {

GetAnnotationValueUtil getAnnotationValueUtil = new GetAnnotationValueUtil();

//第一个class类的集合

List> classes = new ArrayList>();

//是否循环迭代

boolean recursive = true;

//获取包的名字 并进行替换

String packageDirName = packageName.replace('.', '/');

//定义一个枚举的集合 并进行循环来处理这个目录下的文件

Enumeration dirs;

try {

//读取指定package下的所有class

dirs = 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 = annotation.value();

for (String string : value) {

//放入List集合

stringList.add(string);

}

}

}

}

return stringList;

}

/**

http:// * findAndAddClassesInPackageByFile方法描述:

* 日期:2016年7月18日 下午5:41:12

* 异常对象:@param pacixFuEkageName

* 异常对象:@param packagePath

* 异常对象:@param recursive

* 异常对象:@param classes

*/

public static void findAndAddClassesInPackageByFile(String packageName, String packagePath, final boolean recursive, List> classes){

//获取此包的目录 建立一个File

File 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());

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:使用springboot单例模式与线程安全问题踩的坑
下一篇:SpringBoot实现邮件发送功能的姿势分享
相关文章

 发表评论

暂时没有评论,来抢沙发吧~