0
点赞
收藏
分享

微信扫一扫

java动态修改注解方法的值

gy2006_sw 2022-02-17 阅读 76

    将注解DictI18n的test()默认值改了.

1. DictI18n 注解定义如下

//在这用于测试的方法
    String test() default "init";

@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface DictI18n {

	/**
	 * If true, the rule same as Bee config
	 * @return
	 */
	boolean namingTransform() default false;
	
	
	Class<? extends BeforeReturnAnnotationHandler> handler() default AbstractDictI18nDefaultHandler.class;

    //在这用于测试的方法
	String test() default "init";

}

2.在一个Javabean的字段majorid使用注解.可以看到注解并没有定义test()的值.

    @DictI18n
    private String majorid;

3.从字段field中获取到注解DictI18n

然后修改test()的值.

            DictI18n anno = field.getAnnotation(DictI18n.class);
            System.out.println("修改前,  test()的值" + anno.test() + "\t");
            
            InvocationHandler invocationHandler = Proxy.getInvocationHandler(anno);
            
            Field value = invocationHandler.getClass().getDeclaredField("memberValues");
            value.setAccessible(true);
            Map<String, Object> memberValues = (Map<String, Object>) value.get(invocationHandler);
            memberValues.put("test", "changed");
            System.out.println("修改后.....");
            System.out.println("修改后,  test()的值" + anno.test() + "\t");

4. 打印结果

修改前,  test()的值init	
修改后.....
修改后,  test()的值changed

DictI18n是ORM Bee的一个多语言国际化字典转化,全局统一设置,无需逐个设置,省时省力.

Bee,互联网新时代的Java ORM工具,更快、更简单、更自动,开发速度快,运行快,更智能!

Bee让程序员/软件工程师,从手工编码中解放出来,Bee更适合智能软件制造时代!

十分钟即可入门!

立志做最懂用户的软件!

举报

相关推荐

0 条评论