users@jaxb.java.net

Re: XJC get the annotaion of an created field or as alternative the property order

From: Aleksei Valikov <valikov_at_gmx.net>
Date: Mon, 23 Nov 2009 16:40:22 +0100

Hi,

> Hello XJC-Users,
> is there a way to get to annotations of an generated class?
> Assume ich have a ClassOutline classOutline.
> Is there a method, like: classOutline.target.getAnnotations();
>
> The reason, I like to get these information is, that I like to access to XmlType propOrder-Annotation.
>
> For Example, JAXB generates
> @XmlAccessorType(XmlAccessType.FIELD)
> @XmlType(name = "MyClassType", propOrder = {
> "firstField",
> "secondField",
> "thirdField",
> "anOtherField"
> })
> public class MyClass {}
>
> Is it possible to access the annotation of the created prop-order annotation, to get the order of the created fields? To know, that the secondField is really the second field and anOtherField is the fourth field? Or is it possible to find out the property/field order?

You can't get this information from the code model without hacks. And
it's not the right way anyway.

The right way would be to get this information from the CClassInfo.
See how the propOrder field is generated:

        CClassInfo target = ...;
        ...
        if(target.isOrdered()) {
            for(CPropertyInfo p : target.getProperties() ) {
                if( ! (p instanceof CAttributePropertyInfo )) {
                    if (!( (p instanceof CReferencePropertyInfo) &&
                           ((CReferencePropertyInfo)p).isDummy())) {
                        xtw.propOrder(p.getName(false));
                    }
                }
            }
        } else {
            // produce empty array
            xtw.getAnnotationUse().paramArray("propOrder");
        }

Bye.
/lexi