users@jaxb.java.net

Re: custom AnnotationReader

From: Kohsuke Kawaguchi <Kohsuke.Kawaguchi_at_Sun.COM>
Date: Wed, 15 Nov 2006 18:05:04 -0800

Aleksei Valikov wrote:
>> I'm still waiting for someone to volunteer for writing a custom
>> AnnotationReader that reads from a file. I think all the hooks are there
>> in the JAXB RI. We just need an implementation of it.
>
> Seems to be an interesting task. :) I'll see what I can do. I think, a generic
> mechanism must be possible here. Say, a resource name MyClass.ann.xml exist next
> to the MyClass.class. When required, the annotation reader looks for the
> corresponding file and reads annotations.

Yes. If one could change the 'ann' portion, that can be used to have
multiple bindings for the same set of classes.

The other thing to consider is a performance. I might have mentioned
this somewhere else, but reading annotations from an external file could
be a lot quicker than using reflection, because the # of annotations
tend to be much smaller than the # of methods/fields. The RI has to
check for all the fields/methods just to see if there are any JAXB
annotations. If all annotations are available in the file, especially if
they are all in one file, it could be a lot quicker.

In that sense, it might be nicer to have one XML file includes
annotations for more than one class --- like one XML file per package,
maybe. There's obvious trade-offs, but it's worth considering.

> The ann.xml has a generic layout with elements like package, class, field,
> method and property. These elements contain further annotation elements
> associated with specific annotation classes via namespaces.
>
> It could look like:
>
> <class xmlns:jaxb="urn:some-special-prefix:javax.xml.bind.annotation">
> <jaxb:XMLAccessorType value="PROPERTY"/>
> <jaxb:XMLType name="B" propOrder="id version c"/>
> <property name="id">
> <jaxb:XMLAttribute/>
> </property>
> <property name="version">
> <jaxb:XMLAttribute/>
> </property>
> </class>
>
> Annotation classes javax.xml.bind.annotation.XMLAccessorType and so on are
> inferred from the namespace plus element name. Alternatively they may be
> explicitly declared.

Yeah.

> The only thing I miss right now is instantiation of annotations. Annotations are
> essentially either abstract classes or enums. Enums are trivial. Abstract
> classes need to be implemented first (I simply haven't done this before).

You mean annotations are interfaces, right? java.lang.reflect.Proxy
should be able to implement it for you, if you just want to get it done.

If you want to do it efficiently (don't know if the difference matters),
  then there might be other ways, like full byte-code synthesis via ASM
or BCEL.

But I recommend java.lang.reflect.Proxy as the first step.


> This can really be a generic mechanism which simply parallelizes
> reflection-based annotation discovery. Maybe I miss something and the thing like
> this already exists? AnnotationReader could the be implemented on top of this
> mechanism.

There's some effort to back port annotations to JDK 1.4, so those folks
might have something (like http://backport175.codehaus.org/)

But there are also a few key differences. Here, we'd actually prefer to
be able to have a multiple set of annotations. Those projects tend to
simply mirror the semantics of Java5 annotations, so they might not let
you choose annotation sources.


> BTW, I'm working on the "annotate" plugin which converts XML Schema annotations
> into class/field annotations. For instance the XML:
     <snip />
> This works already, so I think I'll try implementing this idea with ann.xml as well.

Cool. Where is this annotate plugin available?

-- 
Kohsuke Kawaguchi
Sun Microsystems                   kohsuke.kawaguchi_at_sun.com