Java Annotations values provided in dynamic manner

Solution 1:

There is no way to dynamically generate a string used in an annotation. The compiler evaluates annotation metadata for RetentionPolicy.RUNTIME annotations at compile time, but GENERIC_GENERATED_NAME isn't known until runtime. And you can't use generated values for annotations that are RetentionPolicy.SOURCE because they are discarded after compile time, so those generated values would never be known.

Solution 2:

The solution is to use an annotated method instead. Call that method (with reflection) to get the dynamic value.

From the user's perspective we'd have:

@MyInterface
public class MyClass {
    @MyName
    public String generateName() {
        return MyClass.class.getName();
    }
}

The annotation itself would be defined as

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface @MyName {
}

Implementing the lookup for both of these annotations is rather straight-forward.

// as looked up by @MyInterface
Class<?> clazz;

Method[] methods = clazz.getDeclaredMethods();
if (methods.length != 1) {
    // error
}
Method method = methods[0];
if (!method.isAnnotationPresent(MyName.class)) {
    // error as well
}
// This works if the class has a public empty constructor
// (otherwise, get constructor & use setAccessible(true))
Object instance = clazz.newInstance();
// the dynamic value is here:
String name = (String) method.invoke(instance);

Solution 3:

There is no way to modify the properties of an annotation dynamically like others said. Still if you want to achieve that, there are two ways to do this.

  1. Assign an expression to the property in the annotation and process that expression whenever you retrieve the annotation. In your case your annotation can be

    @MyInterface(aString = "objectA.doSomething(args1, args2)")

When you read that, you can process the string and make the method invocation and retrieve the value. Spring does that by SPEL (Spring expression language). This is resource intensive and the cpu cycles are wasted every time we want to process the expression. If you are using spring, you can hook in a beanPostProcessor and process the expression once and store the result somewhere. (Either a global properties object or in a map which can be retrieved anywhere).

  1. This is a hacky way of doing what we want. Java stores a private variable which maintains a map of annotations on the class/field/method. You can use reflection and get hold of that map. So while processing the annotation for the first time, we resolve the expression and find the actual value. Then we create an annotation object of the required type. We can put the newly created annotation with the actual value (which is constant) on the property of the annotation and override the actual annotation in the retrieved map.

The way jdk stores the annotation map is java version dependent and is not reliable since it is not exposed for use (it is private).

You can find a reference implementation here.

https://rationaleemotions.wordpress.com/2016/05/27/changing-annotation-values-at-runtime/

P.S: I haven't tried and tested the second method.