P.S: I haven't tried and tested the second method. You can find a reference implementation here. 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). 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. We'll also take a look at some examples of problems and solutions involving annotation attribute values. In this tutorial, we're going to learn some reasons for that limitation and look under the hood of the JVM to explain it better. Then we create an annotation object of the required type. Particularly, an annotation attribute value must be a constant expression. So while processing the annotation for the first time, we resolve the expression and find the actual value. You can use reflection and get hold of that map. Java stores a private variable which maintains a map of annotations on the class/field/method. This is a hacky way of doing what we want.So one thing that might work is to have setter. (Either a global properties object or in a map which can be retrieved anywhere). This is a part of my XML content : Deserialize XML element with attributes with no value using Jackson Java. If you are using spring, you can hook in a beanPostProcessor and process the expression once and store the result somewhere. This is resource intensive and the cpu cycles are wasted every time we want to process the expression. Spring does that by SPEL (Spring expression language). When you read that, you can process the string and make the method invocation and retrieve the value. In your case your annotation can = "objectA.doSomething(args1, args2)") Still if you want to achieve that, there are two ways to do this.Īssign an expression to the property in the annotation and process that expression whenever you retrieve the annotation. There is no way to modify the properties of an annotation dynamically like others said.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |