Modify a method definition's annotation String parameter at runtime on Android

*爱你&永不变心* 提交于 2020-08-26 04:54:00

问题


As the title says I'm trying to modify a method's annotation String parameter at runtime in Java on Android. I've found an answer to another question asked some time ago which should solve the problem. However, it didn't on Android. In the linked answer there is the following code line

Object handler = Proxy.getInvocationHandler(annotation);

It assigns a libcore.reflect.AnnotationFactory Object to the handler variable. Three lines later in the code it is tried to get the class's field "memberValues", which is said to provide the annotation's member-value-pairs. It is realized using this line of code:

f = handler.getClass().getDeclaredField("memberValues");

However, Android's AnnotationFactory class does not have a field named "memberValues". That's why I get an exception telling me: java.lang.NoSuchFieldException: No field memberValues in class Llibcore/reflect/AnnotationFactory; (declaration of 'libcore.reflect.AnnotationFactory' appears in /apex/com.android.art/javalib/core-libart.jar). Also, I cannot even access any declared fields of the class. I don't know why, but handler.getClass().getDeclaredFields() is empty, although handler.getClass() returns libcore.reflect.AnnotationFactory. I am trying to apply this runtime modification to the functionName value of a simple interface looking like this:

public interface LambdaFunctionInterface {
    @LambdaFunction(functionName = "PLACEHOLDER")
    Object lambdaFunction(Object request);
}

How can I realize it on Android? Where are the mentioned member-value-pairs of annotations stored on Android? Thanks a lot in advance!

来源:https://stackoverflow.com/questions/63429688/modify-a-method-definitions-annotation-string-parameter-at-runtime-on-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!