Modify a class definition's annotation string parameter at runtime
This code does more or less what you ask for - it is a simple proof of concept:
- a proper implementation needs to also deal with the
declaredAnnotations
- if the implementation of annotations in Class.java changes, the code will break (i.e. it can break at any time in the future)
- I have no idea if there are side effects...
Output:
oldAnnotation = some value
modifiedAnnotation = another value
public static void main(String[] args) throws Exception {
final Something oldAnnotation = (Something) Foobar.class.getAnnotations()[0];
System.out.println("oldAnnotation = " + oldAnnotation.someProperty());
Annotation newAnnotation = new Something() {
@Override
public String someProperty() {
return "another value";
}
@Override
public Class<? extends Annotation> annotationType() {
return oldAnnotation.annotationType();
}
};
Field field = Class.class.getDeclaredField("annotations");
field.setAccessible(true);
Map<Class<? extends Annotation>, Annotation> annotations = (Map<Class<? extends Annotation>, Annotation>) field.get(Foobar.class);
annotations.put(Something.class, newAnnotation);
Something modifiedAnnotation = (Something) Foobar.class.getAnnotations()[0];
System.out.println("modifiedAnnotation = " + modifiedAnnotation.someProperty());
}
@Something(someProperty = "some value")
public static class Foobar {
}
@Retention(RetentionPolicy.RUNTIME)
@interface Something {
String someProperty();
}
Modify a method annotation parameter at runtime
I wrote a class AnnotationUtil
to resolve the seris requirements.
It can add/remove/change annotation value on class/field/method instance.
Note that use ReflectUtil
to get root field/method when add/remove annotation.
See it on github:
AnnotationUtil
ReflectUtil
Modify a class definition's annotation string parameter at runtime
This code does more or less what you ask for - it is a simple proof of concept:
- a proper implementation needs to also deal with the
declaredAnnotations
- if the implementation of annotations in Class.java changes, the code will break (i.e. it can break at any time in the future)
- I have no idea if there are side effects...
Output:
oldAnnotation = some value
modifiedAnnotation = another value
public static void main(String[] args) throws Exception {
final Something oldAnnotation = (Something) Foobar.class.getAnnotations()[0];
System.out.println("oldAnnotation = " + oldAnnotation.someProperty());
Annotation newAnnotation = new Something() {
@Override
public String someProperty() {
return "another value";
}
@Override
public Class<? extends Annotation> annotationType() {
return oldAnnotation.annotationType();
}
};
Field field = Class.class.getDeclaredField("annotations");
field.setAccessible(true);
Map<Class<? extends Annotation>, Annotation> annotations = (Map<Class<? extends Annotation>, Annotation>) field.get(Foobar.class);
annotations.put(Something.class, newAnnotation);
Something modifiedAnnotation = (Something) Foobar.class.getAnnotations()[0];
System.out.println("modifiedAnnotation = " + modifiedAnnotation.someProperty());
}
@Something(someProperty = "some value")
public static class Foobar {
}
@Retention(RetentionPolicy.RUNTIME)
@interface Something {
String someProperty();
}
Java - How to edit annotation fields at runtime?
Use the utility class AnnotationUtil
You can do changeAnnotation(csvColumn, "name", "newName")
Related Topics
A Java Collection of Value Pairs? (Tuples)
How to Programmatically Determine Operating System in Java
How to Do Url Decoding in Java
Java 8 Localdate Jackson Format
How Many Significant Digits Do Floats and Doubles Have in Java
Spring - @Transactional - What Happens in Background
When to Choose Checked and Unchecked Exceptions
When to Use: Java 8+ Interface Default Method, Vs. Abstract Method
Why Aren't Java Collections Remove Methods Generic
Catching Java.Lang.Outofmemoryerror
Why Filter() After Flatmap() Is "Not Completely" Lazy in Java Streams
Is There a Goto Statement in Java
How to Extract Numbers from a String and Get an Array of Ints