From 61135ac4da4023b415aad4c90ea63b83fc57ad90 Mon Sep 17 00:00:00 2001 From: Anand kumar Date: Wed, 7 Jun 2017 18:54:47 +0530 Subject: [PATCH] Change annotation parameters during runtime (#2006) --- .../java/reflection/DynamicGreeter.java | 23 ++++++++ .../com/baeldung/java/reflection/Greeter.java | 10 ++++ .../java/reflection/GreetingAnnotation.java | 58 +++++++++++++++++++ .../baeldung/java/reflection/Greetings.java | 6 ++ 4 files changed, 97 insertions(+) create mode 100644 core-java/src/main/java/com/baeldung/java/reflection/DynamicGreeter.java create mode 100644 core-java/src/main/java/com/baeldung/java/reflection/Greeter.java create mode 100644 core-java/src/main/java/com/baeldung/java/reflection/GreetingAnnotation.java create mode 100644 core-java/src/main/java/com/baeldung/java/reflection/Greetings.java diff --git a/core-java/src/main/java/com/baeldung/java/reflection/DynamicGreeter.java b/core-java/src/main/java/com/baeldung/java/reflection/DynamicGreeter.java new file mode 100644 index 0000000000..3776ef82e2 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/java/reflection/DynamicGreeter.java @@ -0,0 +1,23 @@ +package com.baeldung.java.reflection; + +import java.lang.annotation.Annotation; + +public class DynamicGreeter implements Greeter { + + private String greet; + + public DynamicGreeter(String greet) { + this.greet = greet; + } + + @Override + public Class annotationType() { + return DynamicGreeter.class; + } + + @Override + public String greet() { + return greet; + } + +} diff --git a/core-java/src/main/java/com/baeldung/java/reflection/Greeter.java b/core-java/src/main/java/com/baeldung/java/reflection/Greeter.java new file mode 100644 index 0000000000..ede269528a --- /dev/null +++ b/core-java/src/main/java/com/baeldung/java/reflection/Greeter.java @@ -0,0 +1,10 @@ +package com.baeldung.java.reflection; + +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; + +@Retention(RetentionPolicy.RUNTIME) +public @interface Greeter { + + public String greet() default ""; +} diff --git a/core-java/src/main/java/com/baeldung/java/reflection/GreetingAnnotation.java b/core-java/src/main/java/com/baeldung/java/reflection/GreetingAnnotation.java new file mode 100644 index 0000000000..601306f5d2 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/java/reflection/GreetingAnnotation.java @@ -0,0 +1,58 @@ +package com.baeldung.java.reflection; + +import java.lang.annotation.Annotation; +import java.lang.reflect.Field; +import java.lang.reflect.Method; +import java.util.Map; + +public class GreetingAnnotation { + + private static final String ANNOTATION_METHOD = "annotationData"; + private static final String ANNOTATION_FIELDS = "declaredAnnotations"; + private static final String ANNOTATIONS = "annotations"; + + public static void main(String ...args) { + Greeter greetings = Greetings.class.getAnnotation(Greeter.class); + System.err.println("Hello there, " + greetings.greet() + " !!"); + + Greeter targetValue = new DynamicGreeter("Good evening"); + //alterAnnotationValueJDK8(Greetings.class, Greeter.class, targetValue); + alterAnnotationValueJDK7(Greetings.class, Greeter.class, targetValue); + + greetings = Greetings.class.getAnnotation(Greeter.class); + System.err.println("Hello there, " + greetings.greet() + " !!"); + } + + @SuppressWarnings("unchecked") + public static void alterAnnotationValueJDK8(Class targetClass, Class targetAnnotation, Annotation targetValue) { + try { + Method method = Class.class.getDeclaredMethod(ANNOTATION_METHOD, null); + method.setAccessible(true); + + Object annotationData = method.invoke(targetClass); + + Field annotations = annotationData.getClass().getDeclaredField(ANNOTATIONS); + annotations.setAccessible(true); + + Map, Annotation> map = (Map, Annotation>) annotations.get(annotationData); + map.put(targetAnnotation, targetValue); + } catch (Exception e) { + e.printStackTrace(); + } + } + + @SuppressWarnings("unchecked") + public static void alterAnnotationValueJDK7(Class targetClass, Class targetAnnotation, Annotation targetValue) { + try { + Field annotations = Class.class.getDeclaredField(ANNOTATIONS); + annotations.setAccessible(true); + + Map, Annotation> map = (Map, Annotation>) annotations.get(targetClass); + System.out.println(map); + map.put(targetAnnotation, targetValue); + System.out.println(map); + } catch (Exception e) { + e.printStackTrace(); + } + } +} diff --git a/core-java/src/main/java/com/baeldung/java/reflection/Greetings.java b/core-java/src/main/java/com/baeldung/java/reflection/Greetings.java new file mode 100644 index 0000000000..4f3a20c3b9 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/java/reflection/Greetings.java @@ -0,0 +1,6 @@ +package com.baeldung.java.reflection; + +@Greeter(greet="Good morning") +public class Greetings { + +}