You can create annotations at runtime through Proxy . You can then add them to your Java objects via reflection, as suggested in other answers (but you probably would be better off finding an alternative way to handle this, as messing up existing types through reflection can be dangerous and hard to debug).
But it is not very simple ... I wrote a library that I named, hope that Javanna just make it easy using a clean API.
At JCenter and Maven Central .
Using it:
@Retention( RetentionPolicy.RUNTIME ) @interface Simple { String value(); } Simple simple = Javanna.createAnnotation( Simple.class, new HashMap<String, Object>() {{ put( "value", "the-simple-one" ); }} );
If any map entry does not match the declared annotation fields and type (s), an Exception is thrown. If any value that does not have a default value is absent, an exception is thrown.
This suggests that each instance of the annotation that was created successfully is safe to use as an instance of the compilation time annotation.
As a bonus, this library can also analyze annotation classes and return annotation values ββin the form of a map:
Map<String, Object> values = Javanna.getAnnotationValues( annotation );
This is useful for creating mini-frameworks.
Renato Nov 25 '16 at 20:40 2016-11-25 20:40
source share