Ralf Kistner wrote:
> In the reflection library, is there a nice way to get the XML element
> name of a specific property? I am looking for a function similar to
> RuntimePropertyInfo.getName(), but one that returns the XML name.
Yes. Once you get to RuntimePropertyInfo, first you have to check the
kind of property. For Attribute and Value properties, obviously there's
no element name.
That leaves ElementProperty, ReferenceProperty, and MapProperty. They
each model different kinds of properties, so the way to obtain the
element names are different with each other.
With an element property, it can hold multiple element names in a
general case (depending on the type of the value in it), so you need to
loop through TypeRefs in ElementProperty.
With a reference property, the tag name is determined by the actual
object instance stored in the property. So you iterate through
ElementInfos from ReferenceProperty and get its element name.
With a map property, there's one and only unique tag name.
If you look at the schema generator, you'll see this in action.
> The only solution I found so far was to search for the annotations myself:
>
> try {
> Field f = item.getClass().getDeclaredField(e.getName());
> if(f.isAnnotationPresent(XmlElement.class)) {
> ename = f.getAnnotation(XmlElement.class).name();
> }
> } catch (NoSuchFieldException e1) { }
>
> but this doesn't always work. Does anyone know about a better way to do this?
You are right that this doesn't always work. I hope the above
explanation has helped.
--
Kohsuke Kawaguchi
Sun Microsystems kohsuke.kawaguchi_at_sun.com