0

I know you can't change annotation parameters dynamically at run-time. I know the value at compile-time but not within the scope of the annotated type. I also have no controll over the annotation itself.

I want to use this for JPA, so I'll use it in the example as well:

I have a base program defining an entity A:

@Entity
@Inheritance(strategy=JOINED) //default strategy
public class A { }

Now I have a flavor built on top of that program, i.e. it uses the base program like a library but both are compiled together. The flavor extends A and it should be able to determine its inheritance strategy, i.e. override the parameter value of @Inhertitance of type A.

Is there a way to accomplish this without messing with the byte code?

Erik Hofer
  • 2,296
  • 2
  • 18
  • 39
  • 1
    You can't "override" inheritance in a subclass. You likely could in orm.xml specifying inheritance for the base class. – Neil Stockton Nov 08 '15 at 11:34
  • Unlike with Spring I never got in touch with XML configuration for JPA, this could very well solve my specific problem, thanks for the hint! I'm still interested in a general approach to this, though. – Erik Hofer Nov 08 '15 at 11:40
  • Also to make it clear, I don't want to "override" the annotation in the subclass, I know that's not how it works. It just has to be done within the *flavor*. – Erik Hofer Nov 08 '15 at 11:51
  • I know. Thus, in the *flavor* I need to change the inheritance type of the root class, that's the whole point of this question... – Erik Hofer Nov 08 '15 at 12:12
  • 1
    There is no "official" way to change an annotation during runtime. Nevertheless there is a question that shows how to do that with reflection: http://stackoverflow.com/questions/24367384/modify-field-annotation-value-dynamically. But remember that this is highly JDK specific and may break for different environments or versions. – Tobias Liefke Nov 08 '15 at 17:03

0 Answers0