This discussion is archived
5 Replies Latest reply: Apr 7, 2010 1:06 AM by 843793 RSS

Newbie question: how to make a field that knows if it is annotated

843793 Newbie
Currently Being Moderated
Hi all,
I'm trying to use annotations to write a new wrapper for a library: basically, i'd like to use the elements of this library in an easy way while, later, i'd like to add further informations by annotating the declaration of the elements.
I'll try to be more clear by making some examples:
I'd like to declare a variable this way:
myClass c = new myClass();
c.process();
and, later, to change my code into this:
@FurtherParams(p1="x", p2="y", p3="z")
myClass c = new myClass();
c.process();
The problem is that I don't really have any idea about which code I need to write into the process() method to read the annotation values... I know that I should use the Reflection, but I really can't guess how can I do it...
I know that this way I'm using annotations in a strange way, and that I could solve this problem in a cleaner way by creating many overloaded constructors, but the use of annotations would grant me many benefits, like the ability to choose which parameters I'd like to specify and which I don't, and it would be great to logically separate the "algorithmic" code from the "physical implementation" details.
Do you have any idea?

Thank you
ma1069

Edited by: ma1069 on Apr 4, 2010 10:22 AM
  • 1. Re: Newbie question: how to make a field that knows if it is annotated
    843793 Newbie
    Currently Being Moderated
    ma1069 wrote:
    Do you have any idea[s]?
    Absolutely; use a map!

    Or go your route and write an annotation processor. Though I suspect that would be overengineering and you'd eventually have to pay the piper.

    With kind regards
    Ben
  • 2. Re: Newbie question: how to make a field that knows if it is annotated
    843793 Newbie
    Currently Being Moderated
    @Ben,
    thank you for your tip, I haven't thought about that! But even so, the annotation processor still tempts me as it would be much more compact if we look the written code: the performance loss is not a problem in this case... In your experience do you think that is actually possible to do such things? The main problem is that I should read the annotations written on an element inside a method of the element itself, without even knowing the element's name...
  • 3. Re: Newbie question: how to make a field that knows if it is annotated
    843793 Newbie
    Currently Being Moderated
    There would be no performance loss anyway, since it would all happen at compile time. You'd have to extract the arguments from the annotation and "insert" them as constructor invocation arguments. From what I know of the APTs you'd have to generate a new copy (with the args fixed) of the entire source tree and compile that. Might be simpler than that though.

    With kind regards
    Ben
  • 4. Re: Newbie question: how to make a field that knows if it is annotated
    608410 Newbie
    Currently Being Moderated
    BenSchulz wrote:
    Might be simpler than that though.
    Actually - NO - it is more complex because the APIs do not provide a way to modify the source tree, so you'd have to roll your own way of doing that. Not impossible but is generally a can of worms. Also the annotation processing APIs (and indeed the reflection API) do not provide a standardised view of the code (including variables and their annotations) inside a method, so doing this in such a way that works with different compilers (for example the JDK's javac and the Eclipse IDE compiler) is more than you might think.

    There would have to be a huge benefit accruing from this approach before I would consider investing the considerable effort required to get it working.

    Have you considered using static factory methods which would allow you separate the two aspects in an "orders of magnitude simpler" way?

    Bruce


    >
    With kind regards
    Ben
  • 5. Re: Newbie question: how to make a field that knows if it is annotated
    843793 Newbie
    Currently Being Moderated
    I really hadn't any idea it would have been so difficult to write my annotation processor...
    I thought I just didn't know "the trick" that would make my job easier =)
    But if that's the case, I'll move on other "simpler" programming patterns ;)
    Thank you all for your help

    ma1069