第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

在運(yùn)行時(shí)修改類定義的注釋字符串參數(shù)

在運(yùn)行時(shí)修改類定義的注釋字符串參數(shù)

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

3 回答

?
倚天杖

TA貢獻(xiàn)1828條經(jīng)驗(yàn) 獲得超3個(gè)贊

在OSX上測(cè)試過。效果很好。

由于我還需要在運(yùn)行時(shí)更改注釋值,所以我重新討論了這個(gè)問題。

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

/**
 * 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());}

這種方法的優(yōu)點(diǎn)是不需要?jiǎng)?chuàng)建新的注釋實(shí)例。因此,不需要預(yù)先知道具體的注釋類。另外,副作用應(yīng)該是最小的,因?yàn)樵嫉淖⑨寣?shí)例保持不變。

用Java 8進(jìn)行測(cè)試。


查看完整回答
反對(duì) 回復(fù) 2019-07-02
?
嗶嗶one

TA貢獻(xiàn)1854條經(jīng)驗(yàn) 獲得超8個(gè)贊

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

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


查看完整回答
反對(duì) 回復(fù) 2019-07-02
  • 3 回答
  • 0 關(guān)注
  • 852 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)