简单介绍如何通过反射获取注解的值

简单说明

可以利用反射获取注解中的值,通过反射可以获取一个类的Class对象,通过该对象可以获取注解,获取到对应的注解后,就可以获取注解中的值,具体代码实现如下:

public class AnnotationDemo {

    public static void main(String[] args) throws NoSuchFieldException {
        // 获取类的Class对象
        Class clazz = Demo.class;
        // 获取类上的注解
        CustomerAnnotation classAnnotation =
                (CustomerAnnotation) clazz.getAnnotation(CustomerAnnotation.class);
        // 获取注解上的值
        String classValue = classAnnotation.value();
        String[] classDesc = classAnnotation.desc();
        System.out.println("类上注解的值 value=" + classValue + 
                " desc=" + Arrays.toString(classDesc));
        // 获取属性
        Field field = clazz.getDeclaredField("name");
        // 获取属性上的注解
        CustomerAnnotation fieldAnnotation =
                (CustomerAnnotation) field.getAnnotation(CustomerAnnotation.class);
        // 获取注解上的值
        String fieldValue = fieldAnnotation.value();
        String[] fieldDesc = fieldAnnotation.desc();
        System.out.println("类上注解的值 value=" + fieldValue + 
                " desc=" + Arrays.toString(fieldDesc));
    }

}

@CustomerAnnotation(value = "classValue")
class Demo {

    @CustomerAnnotation(value = "fieldValue",desc = {"desc1","desc2"})
    private String name;
}

@Target({ElementType.TYPE, ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
@interface CustomerAnnotation {
    String value();
    String[] desc() default "defaultValue";
}

注:不理解注解和反射的可以看以下两篇文章:
1.简单介绍Java中的注解
2.简单介绍java中的反射

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,这是一个技术问题,我可以为您提供一些信息。在Java中,要获取注解信息可以使用反射机制。首先需要获取类的Class对象,然后通过这个对象获取到指定方法或字段上的注解对象。可以使用Annotation接口的实现类来获取注解信息,再使用反射机制修改注解的内容。下面是一个简单的示例代码: ```java // 定义一个注解类 @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) public @interface MyAnnotation { String value() default "原始"; } // 在方法上使用注解 public class MyClass { @MyAnnotation("Hello, World!") public void myMethod() { System.out.println("My method."); } } // 反射获取注解并修改内容 Class myClass = MyClass.class; Method myMethod = myClass.getMethod("myMethod"); MyAnnotation myAnnotation = myMethod.getAnnotation(MyAnnotation.class); String oldValue = myAnnotation.value(); System.out.println("原始:" + oldValue); // 修改注解内容 MyAnnotation newAnnotation = new MyAnnotation() { public String value() { return "新"; } public Class<? extends Annotation> annotationType() { return MyAnnotation.class; } }; Method method = myClass.getDeclaredMethod("myMethod"); // 获取该方法上的注解 Annotation annotation = method.getAnnotation(MyAnnotation.class); // 获取 AnnotationInvocationHandler 实例 InvocationHandler invocationHandler = Proxy.getInvocationHandler(annotation); // 获取 memberValues 字段 Field memberValues = invocationHandler.getClass().getDeclaredField("memberValues"); // 取消 Java 语言访问检查以访问私有字段 memberValues.setAccessible(true); // 修改注解 Map<String, String> values = (Map<String, String>) memberValues.get(invocationHandler); values.put("value", newAnnotation.value()); String newValue = myMethod.getAnnotation(MyAnnotation.class).value(); System.out.println("新:" + newValue); ``` 上述代码示例中,我们定义了一个名为“MyAnnotation”的注解,并在MyClass类的myMethod()方法上使用了该注解。然后,我们使用反射机制获取myMethod()方法上的注解对象,并修改了注解的内容。最后,我们再次获取注解对象的内容,验证注解内容已被修改。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值