方法一
该方法不太灵活
过程:使用反射获取指定method上方的指定注解,拿到该注解的String类型的参数值,使用反射修改该String对象内字符数组的地址
import java.lang.annotation.*;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
public class TestAnnotation {
@MyAnnotation("test")
public void test(){
final Class<? extends TestAnnotation> aClass = this.getClass();
try {
System.out.println("运行:"+aClass.getMethod("test").getAnnotation(MyAnnotation.class).value());
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
}
@MyAnnotation("test02")
public void test02(){
final Class<? extends TestAnnotation> aClass = this.getClass();
try {
System.out.println("运行:"+aClass.getMethod("test02").getAnnotation(MyAnnotation.class).value());
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws Exception {
TestAnnotation testAnnotation = new TestAnnotation();
testAnnotation.setAnnotationValue("editor");
testAnnotation.test();
testAnnotation.test02();
new TestAnnotation().test();
new TestAnnotation().test02();
new Runnable() {
@Override
public void run() {
new TestAnnotation().test();
new TestAnnotation().test02();
}
};
}
//修改test02方法上方MyAnnotation注解的value值
private void setAnnotationValue(String value) throws Exception {
Method method = this.getClass().getMethod("test");
MyAnnotation annotation = method.getAnnotation(MyAnnotation.class);
Field field = String.class.getDeclaredField("value");
field.setAccessible(true);
field.set(annotation.value(), value.toCharArray());
System.out.println("修改:"+annotation.value());
}
}
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@interface MyAnnotation{
String value();
}
方法二
改写法比较灵活
使用反射修改注解的动态代理内存储的内容
import java.lang.reflect.Field;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Map;
public class TestAnnotation02 {
@MyAnnotation("test")
public void test(){
final Class<? extends TestAnnotation02> aClass = this.getClass();
try {
System.out.println("运行:"+aClass.getMethod("test").getAnnotation(MyAnnotation.class).value());
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
}
@MyAnnotation("test02")
public void test02(){
final Class<? extends TestAnnotation02> aClass = this.getClass();
try {
System.out.println("运行:"+aClass.getMethod("test02").getAnnotation(MyAnnotation.class).value());
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws Exception {
TestAnnotation02 testAnnotation = new TestAnnotation02();
testAnnotation.setAnnotationValue("editor");
testAnnotation.test();
testAnnotation.test02();
new TestAnnotation02().test();
new TestAnnotation02().test02();
new Runnable() {
@Override
public void run() {
new TestAnnotation02().test();
new TestAnnotation02().test02();
}
};
}
//修改test02方法上方MyAnnotation注解的value值
private void setAnnotationValue(String value) throws Exception {
Method method = this.getClass().getMethod("test");
MyAnnotation annotation = method.getAnnotation(MyAnnotation.class);
InvocationHandler handler = Proxy.getInvocationHandler(annotation);
Field field = handler.getClass().getDeclaredField("memberValues");
field.setAccessible(true);
Map meberValues= (Map) field.get(handler);
meberValues.put("value", value);
System.out.println("修改:"+annotation.value());
}
}