We have a Java code library that we intend to use in projects. Some of these projects will require adding annotations to the Java objects in this library (i.e., in one project these objects will be used in the JAX-RS servlet implementation, so they need to be annotated using JAXB, JSON, etc. annotations). The problem I am facing is that I could not figure out how to add these annotations without changing the source library.
Consider the following example:
public class MyClass { private String field1; private int field2; }
In some projects, I would like the class to behave as if it were
public class MyClass { @Annotation1 private String field1; @Annotation2 private int field2; }
Initially, I was thinking about using interfaces or derived classes that were separately annotated but couldn't figure out how to do this (whether it is possible or not). I also talked about the Javassist proposal in this thread (for example, the Java bytecode manipulation approach), but the problem is that this is necessary for working with Android clients, so this is not an option for me. At the moment I have no ideas.
I would appreciate if someone could help anyway. Maybe I missed something, or maybe what I'm trying to do is the wrong way. In any case, I need some guidance to continue.
Thank you very well in advance.
source share