Skip to content

Commit 61135ac

Browse files
Anand kumarpivovarit
authored andcommitted
Change annotation parameters during runtime (eugenp#2006)
1 parent cdc9f8f commit 61135ac

4 files changed

Lines changed: 97 additions & 0 deletions

File tree

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.baeldung.java.reflection;
2+
3+
import java.lang.annotation.Annotation;
4+
5+
public class DynamicGreeter implements Greeter {
6+
7+
private String greet;
8+
9+
public DynamicGreeter(String greet) {
10+
this.greet = greet;
11+
}
12+
13+
@Override
14+
public Class<? extends Annotation> annotationType() {
15+
return DynamicGreeter.class;
16+
}
17+
18+
@Override
19+
public String greet() {
20+
return greet;
21+
}
22+
23+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.baeldung.java.reflection;
2+
3+
import java.lang.annotation.Retention;
4+
import java.lang.annotation.RetentionPolicy;
5+
6+
@Retention(RetentionPolicy.RUNTIME)
7+
public @interface Greeter {
8+
9+
public String greet() default "";
10+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.baeldung.java.reflection;
2+
3+
import java.lang.annotation.Annotation;
4+
import java.lang.reflect.Field;
5+
import java.lang.reflect.Method;
6+
import java.util.Map;
7+
8+
public class GreetingAnnotation {
9+
10+
private static final String ANNOTATION_METHOD = "annotationData";
11+
private static final String ANNOTATION_FIELDS = "declaredAnnotations";
12+
private static final String ANNOTATIONS = "annotations";
13+
14+
public static void main(String ...args) {
15+
Greeter greetings = Greetings.class.getAnnotation(Greeter.class);
16+
System.err.println("Hello there, " + greetings.greet() + " !!");
17+
18+
Greeter targetValue = new DynamicGreeter("Good evening");
19+
//alterAnnotationValueJDK8(Greetings.class, Greeter.class, targetValue);
20+
alterAnnotationValueJDK7(Greetings.class, Greeter.class, targetValue);
21+
22+
greetings = Greetings.class.getAnnotation(Greeter.class);
23+
System.err.println("Hello there, " + greetings.greet() + " !!");
24+
}
25+
26+
@SuppressWarnings("unchecked")
27+
public static void alterAnnotationValueJDK8(Class<?> targetClass, Class<? extends Annotation> targetAnnotation, Annotation targetValue) {
28+
try {
29+
Method method = Class.class.getDeclaredMethod(ANNOTATION_METHOD, null);
30+
method.setAccessible(true);
31+
32+
Object annotationData = method.invoke(targetClass);
33+
34+
Field annotations = annotationData.getClass().getDeclaredField(ANNOTATIONS);
35+
annotations.setAccessible(true);
36+
37+
Map<Class<? extends Annotation>, Annotation> map = (Map<Class<? extends Annotation>, Annotation>) annotations.get(annotationData);
38+
map.put(targetAnnotation, targetValue);
39+
} catch (Exception e) {
40+
e.printStackTrace();
41+
}
42+
}
43+
44+
@SuppressWarnings("unchecked")
45+
public static void alterAnnotationValueJDK7(Class<?> targetClass, Class<? extends Annotation> targetAnnotation, Annotation targetValue) {
46+
try {
47+
Field annotations = Class.class.getDeclaredField(ANNOTATIONS);
48+
annotations.setAccessible(true);
49+
50+
Map<Class<? extends Annotation>, Annotation> map = (Map<Class<? extends Annotation>, Annotation>) annotations.get(targetClass);
51+
System.out.println(map);
52+
map.put(targetAnnotation, targetValue);
53+
System.out.println(map);
54+
} catch (Exception e) {
55+
e.printStackTrace();
56+
}
57+
}
58+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.baeldung.java.reflection;
2+
3+
@Greeter(greet="Good morning")
4+
public class Greetings {
5+
6+
}

0 commit comments

Comments
 (0)