users@jaxb.java.net

Re: Introspection and type in JAXB 2

From: Malachi de Ælfweald <malachid_at_gmail.com>
Date: Tue, 28 Feb 2006 09:07:31 -0800

How about this?


> import javax.xml.bind.annotation.*;
> import java.lang.annotation.*;
>
> public class TestAnnotations
> {
> public static void main(String[] args)
> throws Exception
> {
> Class cls = Class.forName(args[0]);
> System.out.println("Class: " + cls);
> System.out.println("Namespace: " + getNamespace(cls));
> }
>
> public static String getNamespace(Class cls)
> {
> XmlSchema stype = (XmlSchema)cls.getPackage().getAnnotation(
> XmlSchema.class);
> XmlType xtype = (XmlType)cls.getAnnotation(XmlType.class);
> return stype.namespace() + "/" + xtype.name();
> }
> }
>
C:\development\testing>java -cp .;%CLASSPATH% TestAnnotations
org.eoti.spec.gaea.person.PersonType

> Class: class org.eoti.spec.gaea.person.PersonType
> Namespace: http://eoti.org/spec/gaea/person/1.0/PersonType




On 2/28/06, Lucas Jordan <lucasjordan_at_softhome.net> wrote:
>
> I have found that the namespace information is stored in the
> package-info.java file. I'm a little new to annotations, but I can't seem
> to actually pull the information out.
>
> I have this method:
> public static String findNamesapce(Class clazz){
>
> Annotation[] annotations = clazz.getPackage().getAnnotations();
> for (Annotation annotation : annotations){
> if (annotation instanceof XmlSchema){
> XmlSchema xmlSchema = (XmlSchema)annotation;
> //return namespace, but how?
> }
> }
> //not found, error?
> return "";
> }
>
> I can find the annotation, and if I System.out it I get:
> @javax.xml.bind.annotation.XmlSchema(namespace=
> http://aegis.chip.org/schemas/aegisEdVisit, attributeFormDefault=UNSET,
> xmlns=[], elementFormDefault=UNSET)
>
> Which I could parse I guess, but I was hopping for a more direct way to
> pull the namespace out. Is there some trivial way to retrieve the value of
> these name value pairs?
>
> Thanks,
> Lucas
>
>
> On 2/28/06 11:05 AM, "Lucas Jordan" <lucasjordan_at_softhome.net> wrote:
>
> Ill give it a look. Good idea!
>
> -Lucas
>
>
> On 2/28/06 10:52 AM, "Malachi de Ælfweald" <malachid_at_gmail.com> wrote:
>
> I was taking a look at my generated classes to see what you were saying. I
> noticed that the autogenerated javadoc showed the same behavior you are
> seeing....
>
> What about combining the QName provided in the ObjectFactory in the same
> package with the "name" portion of the XmlType?
>
> I was going to suggest -Xlocator, but it seems that it produces illegal
> files -- here's some of the errors (at runtime only):
>
> - com.sun.xml.bind.v2.runtime.IllegalAnnotationsException
> - Property locator is present but not specified in @
> XmlType.propOrder
> - org.xml.sax.Locator does not have a no-arg default constructor
> - If a class has @XmlElement property, it cannot have @XmlValue
> property.
> - etc
>
>
> Malachi
>
> On 2/28/06, *Lucas Jordan* <lucasjordan_at_softhome.net> wrote:
>
>
> I see in the generated code for an xsd:element type information is stored
> in
> the class as an annotation:
>
> @XmlType(name = "EdVisitType", propOrder = {
> "visitId",
> "hospitalId",
> ...
> "homeAddress"
> })
>
> I also see that for the fields within my EdVisitType we have annotations
> like:
>
> @XmlElement(name = "HomeAddress", namespace =
> "http://aegis.chip.org/schemas/aegisEdVisit")
> protected AddressType homeAddress;
>
>
> This annotation differs a bit from the first since it specifies a
> namespace.
> My question is this:
>
> Given a Class is it possible through introspection to determine full URN
> that describes the type of that class? I see in the generated javadocs
> there
> is a line:
>
> &lt;extension
> base="{http://aegis.chip.org/schemas/aegisArtifacts}IncidentType<http://aegis.chip.org/schemas/aegisArtifacts%7DIncidentType>
> ">
>
>
> Should this be included as an annotation?
>
> Thanks for any help!
> -Lucas
>
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jaxb.dev.java.net
> <mailto:users-unsubscribe_at_jaxb.dev.java.net><users-unsubscribe_at_jaxb.dev.java.net>
> For additional commands, e-mail: users-help_at_jaxb.dev.java.net
>
>
>
>
>
>
>