In my project, I have defined the an annotation similar to the following:
(Omitting @Retention
, @Target
for brevity)
public @interface DecaysTo {
String[] value();
}
Since originally writing it, our needs have changed and I have now defined an enum that I want to be able to use in place of the string:
public enum Particle {
ELECTRON,
ANTIELECTRON,
NEUTRINO,
ANTINEUTRINO,
...
}
To avoid updating every instance of this annotation, I would like to be able to construct the annotation with either a String
or a member of enum Particle
without having update every instance of this annotation to specify the attribute. However, since we define the annotation's attributes, and not constructors, it seems impossible to overload it.
// In a perfect world, either of these would work ...
public @interface DecaysTo {
String[] value();
Particle[] value();
}
@DecaysTo({"Electron", ...})
@DecaysTo({Particle.ELECTRON, ...})
// without having to explicitly specify which attribute to set:
public @interface DecaysTo {
String[] stringVals();
Particle[] enumVals();
}
@DecaysTo(stringVals = {"Electron", ...})
@DecaysTo(enumVals = {Particle.ELECTRON, ...})
Also attempted:
public @interface DecaysTo {
Object[] value();
}
Is there any way to do this that doesn't involve going back through and editing an enormous amount of code?