jeudi 16 juillet 2015

Java editing annotation attributes at runtime

Is there a way modify the attributes of Field annotations at runtime? Not just the annotation object you can get from a Field at any one point, but whenever in the future you get the same annotation it is updated with the new values.

My task is to be able to at runtime compile a Java file and then use that Class in the application. I've done that, but the only problem is that while in a standard Java application custom annotations do get saved, they are for some reason stripped when run on a Spring application running on Tomcat 7. This is all in memory compilation BTW, no files are ever created in the process.

I couldn't find any way to solve that issue, so I decided to just parse the Java file a second time and add annotations to whatever fields I needed to. I figured out how to add an annotation to a field which didn't have any before through some hacks, but now I need to configure it, seeing as the annotation has some attributes, several booleans and an int.

I've looked into several ways provided in a lot of StackOverflow questions, but they all modify the annotation object that you obtain through reflection, and if I in another method try to get the same annotation, it's values are reset. Any insight into the matter would be greatly appreciated.





Aucun commentaire:

Enregistrer un commentaire