利用反射获取类或者方法或者字段上的注解的值
import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; /** * 测试Annotation * @author zkn * */ @AnnotationTest02(getUserName="zhangsan") public class AnnotationTest03 { @AnnotationTest01(color="red") public static String testColor(String color){ System.out.println(color); return color; } @AnnotationTest04(getAddress="北京市海淀区") String address; public static void main(String[] args) { //获取方法上的注解值 Method[] methods = AnnotationTest03.class.getDeclaredMethods(); if(methods != null){ for(Method method : methods){ AnnotationTest01 annotation = method.getAnnotation(AnnotationTest01.class); if(annotation == null) continue; Method[] me = annotation.annotationType().getDeclaredMethods(); for(Method meth : me){ try { String color = (String) meth.invoke(annotation,null); System.out.println(color); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } } } } //获取类上的注解值 AnnotationTest02 anno = AnnotationTest03.class.getAnnotation(AnnotationTest02.class); if(anno != null){ Method[] met = anno.annotationType().getDeclaredMethods(); for(Method me : met ){ if(!me.isAccessible()){ me.setAccessible(true); } try { System.out.println(me.invoke(anno, null)); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } } } //获取字段上的注解值 AnnotationTest03 noon = new AnnotationTest03(); Field[] field = AnnotationTest03.class.getDeclaredFields(); if(field != null){ for(Field fie : field){ if(!fie.isAccessible()){ fie.setAccessible(true); } AnnotationTest04 annon = fie.getAnnotation(AnnotationTest04.class); Method[] meth = annon.annotationType().getDeclaredMethods(); for(Method me : meth){ if(!me.isAccessible()){ me.setAccessible(true); } try { //给字段重新赋值 fie.set(noon, me.invoke(annon, null)); System.out.println(fie.get(noon)); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } } } } } }
- import java.lang.reflect.Field;
- import java.lang.reflect.InvocationTargetException;
- import java.lang.reflect.Method;
- /**
- * 测试Annotation
- * @author zkn
- *
- */
- @AnnotationTest02(getUserName="zhangsan")
- public class AnnotationTest03 {
- @AnnotationTest01(color="red")
- public static String testColor(String color){
- System.out.println(color);
- return color;
- }
- @AnnotationTest04(getAddress="北京市海淀区")
- String address;
- public static void main(String[] args) {
- //获取方法上的注解值
- Method[] methods = AnnotationTest03.class.getDeclaredMethods();
- if(methods != null){
- for(Method method : methods){
- AnnotationTest01 annotation = method.getAnnotation(AnnotationTest01.class);
- if(annotation == null)
- continue;
- Method[] me = annotation.annotationType().getDeclaredMethods();
- for(Method meth : me){
- try {
- String color = (String) meth.invoke(annotation,null);
- System.out.println(color);
- } catch (IllegalAccessException e) {
- e.printStackTrace();
- } catch (IllegalArgumentException e) {
- e.printStackTrace();
- } catch (InvocationTargetException e) {
- e.printStackTrace();
- }
- }
- }
- }
- //获取类上的注解值
- AnnotationTest02 anno = AnnotationTest03.class.getAnnotation(AnnotationTest02.class);
- if(anno != null){
- Method[] met = anno.annotationType().getDeclaredMethods();
- for(Method me : met ){
- if(!me.isAccessible()){
- me.setAccessible(true);
- }
- try {
- System.out.println(me.invoke(anno, null));
- } catch (IllegalAccessException e) {
- e.printStackTrace();
- } catch (IllegalArgumentException e) {
- e.printStackTrace();
- } catch (InvocationTargetException e) {
- e.printStackTrace();
- }
- }
- }
- //获取字段上的注解值
- AnnotationTest03 noon = new AnnotationTest03();
- Field[] field = AnnotationTest03.class.getDeclaredFields();
- if(field != null){
- for(Field fie : field){
- if(!fie.isAccessible()){
- fie.setAccessible(true);
- }
- AnnotationTest04 annon = fie.getAnnotation(AnnotationTest04.class);
- Method[] meth = annon.annotationType().getDeclaredMethods();
- for(Method me : meth){
- if(!me.isAccessible()){
- me.setAccessible(true);
- }
- try {
- //给字段重新赋值
- fie.set(noon, me.invoke(annon, null));
- System.out.println(fie.get(noon));
- } catch (IllegalAccessException e) {
- e.printStackTrace();
- } catch (IllegalArgumentException e) {
- e.printStackTrace();
- } catch (InvocationTargetException e) {
- e.printStackTrace();
- }
- }
- }
- }
- }
- }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)