This discussion is archived
2 Replies Latest reply: Jan 15, 2010 12:25 AM by 843793 RSS

is there a way to decorelate annotations from the source code?

843793 Newbie
Currently Being Moderated
sorry : the title of the question may sound bizarre but , not being a native speaker, I do not know how to precisely state my problem.
I'll try:
an example: suppose you want to annotate your code with annotation such as @MightBeNull (a fictitious annotation) to mark a field as being potentially null.
this annotation is going to be used by annotation processor X and is specified by (fictitious) library org.something.staticcheck ...
Now if a new (and better) tool appears for the same purpose you'll have to change your code. At compile time your code is dependent on a precise library (and a set of precise tools for annotation processing).
I my dreams I would like a way to write my own annotations and configure an adapter that redirects to the precise library that I choose at given moment.
For sure that cannot be so easy since adaptations could not fit easily (but let's forget it for the moment).
This seems impossible ... or it is?

( a better way could be to annotate not the source code itself but a separate description of its features that could be annotated independently)
  • 1. Re: is there a way to decorelate annotations from the source code?
    608410 Newbie
    Currently Being Moderated
    Put the annotation in a different jar file from (each of) your annotation processor(s). Then you can mix and match at will at compile time, using either explicit processor invocation, or processor discovery.

    Bruce
  • 2. Re: is there a way to decorelate annotations from the source code?
    843793 Newbie
    Currently Being Moderated
    Sorry bruce I am a bit slow: I don't understand your suggestion.
    if I have a code like that:
    import org.staticcheck.annotations.MightBeNull ;
    public class Thing {
           @MighBeNull private Something relation;
    }
    my code is dependent on library "org.staticcheck.annotations".
    then afterwards if I change for another tool that uses annotation
        com.newprovider.annotations.CouldBeNull
    I am obliged to modify my source code!

    I daresay I am tempted by hacks such as :
    import com.mybusiness.annotations.MetaAnnotation ;
    public class Thing {
           @MetaAnnotation("null_possible") private Something relation;
    }
    and then perform some precompile wizardry (thru a preprocessor ;-( or tricky annotation processor
    that transforms the source code). But it could turn out ugly/clumsy ....
    that means that there will be a resource for describing a "translation" from the MetaAnnotation to the
    real thing needed at a precise moment.

    So my question is: has someone found a way to do that elegantly? (btw I can think of many more reasons for this decorrelation between
    the source code and its annotations).