Let's take a look at the scenario where we usually get the value of the annotation by reflection:
So now we define a @Foo annotation that has a value attribute of type String that applies to the field again:
/**
* @Author The flying snail
@Date 2018/3/11 *
@Description * *
@Target (Elementtype.field)
@ Retention (retentionpolicy.runtime) public
@interface Foo {
String value ();
}
Defines a normal Java object Bar, which has a private string property value and sets a @Foo annotation for which the property value is "Test.annotation.value"
/**
* @Author leaves fly i snail
* @Date 2018/3/11
* @Description
/public class Bar {
@Foo (" Test.annotation.value ")
private String value;
}
A normal scenario for getting the value of a note property:
/**
* @Author The flying snail
@Date 2018/3/10 *
@Description * *
@RunWith (springrunner.class)
public class Reflectionannotationtest {
@Test
the public void Test () throws Nosuchfieldexception, illegalaccessexception {
//Get bar instance
bar bar = new Bar ();
Gets the Val field field field
= Bar.getclass (). Getdeclaredfield ("value") of bar;
Gets the Foo annotation instance on the Val field
foo foo = field.getannotation (foo.class);
Gets the Value property of the Foo annotation instance,
String value = foo.value ();
Print the value
System.out.println ("Note value before modification:" + value);
}
We are above the string value = Foo.value (); Down the breakpoint, we run to find that there are so many variables in the current stack, but one thing is Special: foo, which is actually a proxy instance.
See the property h in the Foo stack is an object of a Annotationinvocationhandler type. The H object contains a Membervalues object with key being the attribute of our custom annotation, and value is what I assign. Let's take a look at the source code for the Annotationinvocationhandler class:
Class Annotationinvocationhandler implements Invocationhandler, Serializable {
private static final long Serialversionuid = 6182022883658399397L;
Private final class<? extends annotation> type;
Private final map<string, object> membervalues;
You can find that the Membervalues object is private final decorated. So we can modify the Membervalues access rights by reflection to the purpose of modifying the Membervalues value.
Therefore, the method of dynamically modifying the value of the annotation is to obtain the proxy object of Foo by reflection, then get the Membervalues property of the proxy object, modify the access rights, and update the value of the annotation. The modified code is as follows:
/** * @Author The flying snail @Date 2018/3/10 * @Description * * @RunWith (Springrunner.class) public class Reflectionannotati
ontest {@Test public void Test () throws Nosuchfieldexception, illegalaccessexception {//Get bar instance
Bar bar = new Bar ();
Gets the Val field field field = Bar.getclass (). Getdeclaredfield ("value") of bar;
Gets the Foo annotation instance on the Val field foo foo = field.getannotation (Foo.class);
Gets the Value property of the Foo annotation instance, String value = Foo.value ();
Print the value System.out.println ("The annotation value before modification:" + value);
System.out.println ("------------below is the value of the modified annotation------------");
Gets the Invocationhandler invocationhandler invocationhandler = Proxy.getinvocationhandler (foo) held by this proxy instance of Foo; Gets annotationinvocationhandler membervalues Field Declaredfield = Invocationhandler.getclass (). getDecl
Aredfield ("Membervalues");
Because this field is private final decorated, you open permission declaredfield.setaccessible (true); Gets the membervalues Map membervalues = (map) declaredfield.get (Invocationhandler);
Modify Value Property Membervalues.put ("value", "Test.annotation.new.value");
Gets the Value property values of foo String newvalue = Foo.value ();
SYSTEM.OUT.PRINTLN ("Modified annotation value: + newvalue);" }
}