亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在運行時修改類定義的注釋字符串參數

在運行時修改類定義的注釋字符串參數

牧羊人nacy 2019-07-02 17:39:29
在運行時修改類定義的注釋字符串參數想象一下有一個類:@Something(someProperty = "some value")public class Foobar {     //...}它已經編譯(我無法控制源代碼),并且是JVM啟動時類路徑的一部分。我希望能夠在運行時將“某些值”更改為其他值,這樣以后的任何反射都將使我的新價值而不是默認的“一些值”。這個是可能的嗎?如果是,怎么做?
查看完整描述

3 回答

?
倚天杖

TA貢獻1828條經驗 獲得超3個贊

在OSX上測試過。效果很好。

由于我還需要在運行時更改注釋值,所以我重新討論了這個問題。

下面是@assylias方法的修改版本(非常感謝您的啟發)。

/**
 * Changes the annotation value for the given key of the given annotation to newValue and returns
 * the previous value.
 */@SuppressWarnings("unchecked")public static Object changeAnnotationValue(Annotation annotation, String key, Object newValue){
    Object handler = Proxy.getInvocationHandler(annotation);
    Field f;
    try {
        f = handler.getClass().getDeclaredField("memberValues");
    } catch (NoSuchFieldException | SecurityException e) {
        throw new IllegalStateException(e);
    }
    f.setAccessible(true);
    Map<String, Object> memberValues;
    try {
        memberValues = (Map<String, Object>) f.get(handler);
    } catch (IllegalArgumentException | IllegalAccessException e) {
        throw new IllegalStateException(e);
    }
    Object oldValue = memberValues.get(key);
    if (oldValue == null || oldValue.getClass() != newValue.getClass()) {
        throw new IllegalArgumentException();
    }
    memberValues.put(key,newValue);
    return oldValue;}

用法示例:

@Retention(RetentionPolicy.RUNTIME)@Target(ElementType.TYPE)public @interface ClassAnnotation {
  String value() default "";}@Retention(RetentionPolicy.RUNTIME)@Target(ElementType.FIELD)public @interface FieldAnnotation {
  String value() default "";}@Retention(RetentionPolicy.RUNTIME)@Target(ElementType.METHOD)public @interface MethodAnnotation {
  String value() default "";}@ClassAnnotation("class test")public static class TestClass{
    @FieldAnnotation("field test")
    public Object field;
    @MethodAnnotation("method test")
    public void method(){

    }}public static void main(String[] args) throws Exception {
    final ClassAnnotation classAnnotation = TestClass.class.getAnnotation(ClassAnnotation.class);
    System.out.println("old ClassAnnotation = " + classAnnotation.value());
    changeAnnotationValue(classAnnotation, "value", "another class annotation value");
    System.out.println("modified ClassAnnotation = " + classAnnotation.value());

    Field field = TestClass.class.getField("field");
    final FieldAnnotation fieldAnnotation = field.getAnnotation(FieldAnnotation.class);
    System.out.println("old FieldAnnotation = " + fieldAnnotation.value());
    changeAnnotationValue(fieldAnnotation, "value", "another field annotation value");
    System.out.println("modified FieldAnnotation = " + fieldAnnotation.value());

    Method method = TestClass.class.getMethod("method");
    final MethodAnnotation methodAnnotation = method.getAnnotation(MethodAnnotation.class);
    System.out.println("old MethodAnnotation = " + methodAnnotation.value());
    changeAnnotationValue(methodAnnotation, "value", "another method annotation value");
    System.out.println("modified MethodAnnotation = " + methodAnnotation.value());}

這種方法的優點是不需要創建新的注釋實例。因此,不需要預先知道具體的注釋類。另外,副作用應該是最小的,因為原始的注釋實例保持不變。

用Java 8進行測試。


查看完整回答
反對 回復 2019-07-02
?
嗶嗶one

TA貢獻1854條經驗 獲得超8個贊

這個在我的機器上用Java 8工作。ignoreUnknown在注釋中@JsonIgnoreProperties(ignoreUnknown = true)從…千真萬確假的.

final List<Annotation> matchedAnnotation = Arrays.stream(SomeClass.class.getAnnotations()).filter(annotation -> annotation.annotationType().
equals(JsonIgnoreProperties.class)).collect(Collectors.toList());    final Annotation modifiedAnnotation = new JsonIgnoreProperties() {
    @Override public Class<? extends Annotation> annotationType() {
        return matchedAnnotation.get(0).annotationType();
    }    @Override public String[] value() {
        return new String[0];
    }    @Override public boolean ignoreUnknown() {
        return false;
    }    @Override public boolean allowGetters() {
        return false;
    }    @Override public boolean allowSetters() {
        return false;
    }};    final Method method = Class.class.getDeclaredMethod("getDeclaredAnnotationMap", null);method.setAccessible(true);
    final Map<Class<? extends Annotation>, Annotation> annotations = (Map<Class<? extends Annotation>, Annotation>)
     method.invoke(SomeClass.class, null);annotations.put(JsonIgnoreProperties.class, modifiedAnnotation);


查看完整回答
反對 回復 2019-07-02
  • 3 回答
  • 0 關注
  • 841 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號