最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java方法上注解值修改不成功的問(wèn)題

 更新時(shí)間:2023年02月01日 14:44:36   作者:喝花茶  
這篇文章主要介紹了Java方法上注解值修改不成功的解決方法,本文結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

前提介紹:

java獲取方法有兩種方式:

  1、class.getMethods

  2、class.getDeclaredMethod

查看JDK注釋?zhuān)@兩個(gè)方法:

  getMethods:返回一個(gè)數(shù)組,其中包含反映該類(lèi)對(duì)象表示的類(lèi)或接口的所有公共方法的方法對(duì)象,包括由類(lèi)或接口聲明的方法對(duì)象以及從超類(lèi)和超接口繼承的方法對(duì)象。

  getDeclaredMethod:返回一個(gè)方法對(duì)象,該對(duì)象反映由該類(lèi)對(duì)象表示的類(lèi)或接口的指定聲明方法。

那方法上面的注解數(shù)據(jù)是掛在哪的呢?

private synchronized  Map<Class<? extends Annotation>, Annotation> declaredAnnotations() {
        if (declaredAnnotations == null) {
            Executable root = getRoot();
            if (root != null) {
                declaredAnnotations = root.declaredAnnotations();
            } else {
                declaredAnnotations = AnnotationParser.parseAnnotations(
                    getAnnotationBytes(),
                    sun.misc.SharedSecrets.getJavaLangAccess().
                    getConstantPool(getDeclaringClass()),
                    getDeclaringClass());
            }
        }
        return declaredAnnotations;
    }

//
private Method root;

//
Executable getRoot() {
  return root;
}

方法上面的注解數(shù)據(jù)是放在root的declaredAnnotations數(shù)組里面的

正文內(nèi)容:

getMethods獲取的methods和getDeclaredMethod獲取的methods的root是不同的兩個(gè)實(shí)例。意味著,方法上面的注解實(shí)例存在兩個(gè)。

so,如果不確定方法上面的注解使用時(shí)是從哪里獲?。╣etMethods or getDeclaredMethod)的,哪你兩個(gè)方法上面的注解實(shí)例都要反射修改。

上個(gè)例子:

@SneakyThrows
public void changeAnnotation() {
    Method method_4_getMethods = this.getClass().getMethod("annotation");
    Options options_4_getMethods = method_4_getMethods.getAnnotationsByType(Options.class)[0];
    System.out.println(String.format("getMethods修改前:%d", options_4_getMethods.timeout()));
    changeTimeout(options_4_getMethods, 8888);
    System.out.println(String.format("getMethods修改后:%d", options_4_getMethods.timeout()));

    Method method_4_getDeclaredMethod = this.getClass().getDeclaredMethod("annotation");
    Options options_4_getDeclaredMethod = method_4_getDeclaredMethod.getAnnotationsByType(Options.class)[0];
    System.out.println(String.format("getDeclaredMethod修改前:%d", options_4_getDeclaredMethod.timeout()));
    changeTimeout(options_4_getDeclaredMethod, 9999);
    System.out.println(String.format("getDeclaredMethod修改前:%d", options_4_getDeclaredMethod.timeout()));
}

@SneakyThrows
private void changeTimeout(Options options, int timeout) {
    InvocationHandler invocationHandler = Proxy.getInvocationHandler(options);
    Field memberValues = invocationHandler.getClass().getDeclaredField("memberValues");
    memberValues.setAccessible(true);
    Map memberValuesMap = (Map) memberValues.get(invocationHandler);
    memberValuesMap.put("timeout", timeout);
}
@Options(timeout = -1)
public void annotation() {

}

結(jié)果輸出

getMethods修改前:-1
getMethods修改后:8888
getDeclaredMethod修改前:-1
getDeclaredMethod修改前:9999

debug:

  1、root是兩個(gè)實(shí)例

2、注解是兩個(gè)實(shí)例

到此這篇關(guān)于Java方法上注解值修改不成功的文章就介紹到這了,更多相關(guān)java方法上注解內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論

和平县| 万载县| 香港 | 乳源| 上思县| 罗田县| 陇南市| 瑞安市| 苍梧县| 梧州市| 大渡口区| 万宁市| 喀什市| 紫云| 两当县| 安达市| 徐水县| 洪雅县| 尉氏县| 灵石县| 雅江县| 辽源市| 陕西省| 达日县| 黄山市| 庆城县| 会昌县| 平安县| 阜宁县| 望谟县| 阿城市| 内江市| 贵阳市| 锦州市| 凤阳县| 获嘉县| 广东省| 九龙城区| 泸定县| 紫阳县| 黑龙江省|