Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Is it possible to modify XmlElement's name attribute at runtime??

Options
  • 25-01-2008 5:07pm
    #1
    Registered Users Posts: 590 ✭✭✭


    Hi,

    I'm using the javax.xml.bind.annotation.XmlElement annotation in some code at the moment and I'm wondering if it's possible to modify it's name variable during runtime?
    @XmlElement(name = "item")
    private List list;
    
    public void modifyAnnotation() {
    		Annotation[] annotations = this.getClass().getAnnotations();
    		((XmlRootElement)annotations[0]).name() = "name_of_item";
    	}
    }
    

    Above is an idea of what I'm trying to do, but obviously I can't modify name in this way. There also is no setName for XmlElement.

    This question is probably relevant to all classes derived from java.lang.annotation.Annotation but I'm particularly interested in the XmlElement class.

    I'm guessing it isn't possible but maybe some of you have had experience at this before and might be able to tell me one way or another.

    Cheers.


Comments

  • Registered Users Posts: 8,070 ✭✭✭Placebo


    ive done it in actionscript but in that case i had method to write to my xml.
    is there such thing as XMLList variables in java?


  • Registered Users Posts: 590 ✭✭✭bman


    Placebo wrote: »
    ive done it in actionscript but in that case i had method to write to my xml.
    is there such thing as XMLList variables in java?

    Thanks for the fast reply. Is this the XmlList you're on about?

    From what I've seen in that Javadoc I'm not sure how it would be of benefit to me?


  • Subscribers Posts: 4,075 ✭✭✭IRLConor


    I can't answer for definite but here's roughly what I make of it:
    • The annotations that getAnnotations() returns refer to the class you're in, not any particular instance of the class. So even in theory if you could modify it, there doesn't appear to be a way of attaching it to any particular instance of the class.
    • I have an idea that annotations are inherently all immutable. I can't find anything definitive in the JLS about it, but it feels right. The fact that annotations are treated as a special kind of interface (kind of like a marker interface) reinforces this for me.
    • I don't see how you could make a mutable annotation as you can't have any methods on an annotation which takes any parameters (JLS section 9.6).


  • Registered Users Posts: 590 ✭✭✭bman


    IRLConor wrote: »
    I can't answer for definite but here's roughly what I make of it:
    • The annotations that getAnnotations() returns refer to the class you're in, not any particular instance of the class. So even in theory if you could modify it, there doesn't appear to be a way of attaching it to any particular instance of the class.
    • I have an idea that annotations are inherently all immutable. I can't find anything definitive in the JLS about it, but it feels right. The fact that annotations are treated as a special kind of interface (kind of like a marker interface) reinforces this for me.
    • I don't see how you could make a mutable annotation as you can't have any methods on an annotation which takes any parameters (JLS section 9.6).

    Cheers Conor. This makes sense alright when you say it but I'm not proficient enough at Java yet to figure this out on my own!!

    Pity I can't do this. Makes what I want to do kinda messy!


  • Subscribers Posts: 4,075 ✭✭✭IRLConor


    bman wrote: »
    Cheers Conor. This makes sense alright when you say it but I'm not proficient enough at Java yet to figure this out on my own!!

    For questions like yours it usually helps to try and dig through the relevant part of the Java Language Specification. It can be quite hard going at times, but it does explain all the dark nooks and crannies of the language. It's a good way of learning some of the quirks of the language.

    I don't recommend reading straight through it unless you're having trouble sleeping. :D


  • Advertisement
Advertisement