Человек, вы были всего в 2 шагах от решения. После того, как вы получили обработчик вызова, вы должны изменить его поле memberValues. Вот полный код:
import java.lang.annotation.*;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
public class NewApp {
public static void main(String[] args) throws NoSuchMethodException, NoSuchFieldException, IllegalAccessException {
// get the edit map method
Method method = CustomMap.class.getDeclaredMethod("editMap", Map.class);
// get the annotation
UpdateKey updateKeyAnnotation = method.getDeclaredAnnotation(UpdateKey.class);
System.out.println("current annotation: " + updateKeyAnnotation);
Object handler = Proxy.getInvocationHandler(updateKeyAnnotation);
System.out.println(Arrays.toString(handler.getClass().getDeclaredFields()));
// field memberValues contains annotation attributes and their values
// just modify it
Field memberValues = handler.getClass().getDeclaredField("memberValues");
memberValues.setAccessible(true);
System.out.println(memberValues.get(handler));
Map<String, String> annotationAttributes = new HashMap<>();
annotationAttributes.put("newKey", "otherProduct");
annotationAttributes.put("oldKey", "otherDescription");
memberValues.set(handler, annotationAttributes);
System.out.println("changed annotation: " + updateKeyAnnotation);
}
}
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@interface UpdateKey {
String oldKey() default ""; //The key that needs to be replaced
String newKey() default ""; //The new key
}
class CustomMap {
@UpdateKey(oldKey = "description", newKey = "productName")
public void editMap(Map<String, String> map) throws NoSuchMethodException {
//get the annotation values
UpdateKey updateKey = this.getClass().getMethod("editMap").getAnnotation(UpdateKey.class);
//save value and delete old key
Object obj = map.remove(updateKey.oldKey());
//set the value to the new key
map.put(updateKey.newKey(), obj.toString());
}
}
вывод этого кода
current annotation: @UpdateKey(newKey=productName, oldKey=description)
[private static final long sun.reflect.annotation.AnnotationInvocationHandler.serialVersionUID, private final java.lang.Class sun.reflect.annotation.AnnotationInvocationHandler.type, private final java.util.Map sun.reflect.annotation.AnnotationInvocationHandler.memberValues, private transient volatile java.lang.reflect.Method[] sun.reflect.annotation.AnnotationInvocationHandler.memberMethods, static final boolean sun.reflect.annotation.AnnotationInvocationHandler.$assertionsDisabled]
{newKey=productName, oldKey=description}
changed annotation: @UpdateKey(oldKey=otherDescription, newKey=otherProduct)