Java方法上注解值修改不成功的問題
前提介紹:
java獲取方法有兩種方式:
1、class.getMethods
2、class.getDeclaredMethod
查看JDK注釋,這兩個(gè)方法:
getMethods:返回一個(gè)數(shù)組,其中包含反映該類對(duì)象表示的類或接口的所有公共方法的方法對(duì)象,包括由類或接口聲明的方法對(duì)象以及從超類和超接口繼承的方法對(duì)象。
getDeclaredMethod:返回一個(gè)方法對(duì)象,該對(duì)象反映由該類對(duì)象表示的類或接口的指定聲明方法。
那方法上面的注解數(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)文章
elasticsearch數(shù)據(jù)信息索引操作action?support示例分析
這篇文章主要為大家介紹了elasticsearch數(shù)據(jù)信息索引操作action?support示例分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-04-04
java web實(shí)現(xiàn)自動(dòng)登錄功能
這篇文章主要為大家詳細(xì)介紹了java web實(shí)現(xiàn)自動(dòng)登錄功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10
關(guān)于各種排列組合java算法實(shí)現(xiàn)方法
這篇文章介紹了幾種用JAVA實(shí)現(xiàn)的排列組合算法,有需要的朋友可以參考一下2013-06-06
SpringSecurity怎樣使用注解控制權(quán)限
這篇文章主要介紹了SpringSecurity怎樣使用注解控制權(quán)限的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06
java后臺(tái)判斷客戶端是手機(jī)/PC并返回不同頁面的實(shí)例
下面小編就為大家分享一篇java后臺(tái)判斷客戶端是手機(jī)/PC并返回不同頁面的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-01-01
Java 實(shí)現(xiàn)將List平均分成若干個(gè)集合
這篇文章主要介紹了Java 實(shí)現(xiàn)將List平均分成若干個(gè)集合,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-08-08

