custom AnnotationReader

View: New views
6 Messages — Rating Filter:   Alert me  

Parent Message unknown custom AnnotationReader

by Herve Bitteur-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Some parts of this message have been removed. Learn more about Nabble's security policy.
Hi,

Speaking of a "custom AnnotationReader to read annotations from an external file", this might be the right approach for handling Java sources the user cannot annotate. We had a discussion a few days ago about marshalling JRE classes such as classes Point or Dimension in java.awt package. For example, how can we specify that we'll use attributes rather than elements? In the case of "non-annotable" classes, some external file could be the answer.

Just my 2 cents
/Hervé

Subject:
Re: Making JAXBContextImpl serializable
From:
Kohsuke Kawaguchi Kohsuke.Kawaguchi@...
Date:
Fri, 10 Nov 2006 11:57:40 -0800
To:
users@...
To:
users@...

Kohsuke Kawaguchi wrote:
I should see what's going on in your profile that's taking long...

I made one more minor improvement, but it looks like the rest is mostly dominated by the reflection, so without taking more drastic measure, this seems like the best we can get for now.

One possibility is to use a custom AnnotationReader to read annotations from an external file or something. That might be faster than using reflection.

The other possibility is to analyze JAXB bound classes lazily, but that has a spec-related problem of not being able to detect errors early enough (which might be acceptable for enough audience, though) and it probably takes a significant modification of the RI.


--
Hervé Bitteur
Software Program Manager
Sun Microsystems, Inc.
13, Avenue Morane Saulnier
78142 Vélizy Cedex FRANCE
Phone +33 1 34 03 01 12
Mobile +33 6 86 49 04 14
Fax     +33 1 34 03 07 56
Email   herve.bitteur@...
Skype hbitteur

Re: custom AnnotationReader

by Kohsuke Kawaguchi :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Herve Bitteur wrote:
> Hi,
>
> Speaking of a "custom AnnotationReader to read annotations from an
> external file", this might be the right approach for handling Java
> sources the user cannot annotate. We had a discussion a few days ago
> about marshalling JRE classes such as classes Point or Dimension in
> java.awt package. For example, how can we specify that we'll use
> attributes rather than elements? In the case of "non-annotable" classes,
> some external file could be the answer.

Yes. Indeed that was where this original idea came from.

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.

--
Kohsuke Kawaguchi
Sun Microsystems                   kohsuke.kawaguchi@...


smime.p7s (4K) Download Attachment

Re: custom AnnotationReader

by Aleksei Valikov :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi.

>> Speaking of a "custom AnnotationReader to read annotations from an
>> external file", this might be the right approach for handling Java
>> sources the user cannot annotate. We had a discussion a few days ago
>> about marshalling JRE classes such as classes Point or Dimension in
>> java.awt package. For example, how can we specify that we'll use
>> attributes rather than elements? In the case of "non-annotable"
>> classes, some external file could be the answer.
>
> Yes. Indeed that was where this original idea came from.
>
> 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.

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.

Annotation fields are read from attributes or child elements of annotation
elements. It is relatively easy to detect types of fields via reflection, and
parse string values appropriately.

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).

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.

BTW, I'm working on the "annotate" plugin which converts XML Schema annotations
into class/field annotations. For instance the XML:

<A
        xmlns="urn:org.jvnet.hyperjaxb3.plugin.annotate:org.jvnet.hyperjaxb3.plugin.annotate.tests"
        xmlns:n="urn:org.jvnet.hyperjaxb3.plugin.annotate"
        booleanField="false"
        byteField="0"
        charField="a"
        classField="java.lang.String"
        doubleField="1"
        enumField="ONE"
        floatField="2.3"
        intField="4"
        longField="5"
        shortField="6"
        stringField="7">
        <annotationField>
                <B
                        booleanArrayField="false true"
                        byteArrayField="0 1"
                        charArrayField="a b"
                        classArrayField="java.lang.String java.lang.Boolean"
                        doubleArrayField="2 3"
                        enumArrayField="ONE TWO"
                        floatArrayField="4.5 6.7"
                        intArrayField="8 9"
                        longArrayField="10 11"
                        shortArrayField="12 13"
                        stringArrayField="14 15">
                        <stringArrayField>16</stringArrayField>
                        <stringArrayField>17</stringArrayField>
                        <annotationArrayField>
                                <C n:class="org.jvnet.hyperjaxb3.plugin.annotate.tests.B$C"/>
                                <C n:class="org.jvnet.hyperjaxb3.plugin.annotate.tests.B$C"/>
                        </annotationArrayField>
                </B>
        </annotationField>
</A>

is converted into the following annotation:

package org.jvnet.hyperjaxb3.plugin.annotate.tests;


@A(longField = 5, intField = 4, shortField = 6, charField = 97, byteField = 0,
doubleField = 1, floatField = 2, booleanField = false, stringField = "7",
enumField = org.jvnet.hyperjaxb3.plugin.annotate.tests.AnnotateTest.E.ONE,
classField = String.class, annotationField = @B(longArrayField = {
     10,
     11
}, intArrayField = {
     8,
     9
}, shortArrayField = {
     12,
     13
}, charArrayField = {
     97,
     98
}, byteArrayField = {
     0,
     1
}, doubleArrayField = {
     2,
     3
}, floatArrayField = {
     4,
     6
}, booleanArrayField = {
     false,
     true
}, stringArrayField = {
     "14",
     "15",
     "16",
     "17"
}, enumArrayField = {

}, classArrayField = {
     String.class,
     Boolean.class
}, annotationArrayField = {
     @B.C,
     @B.C
}))
public class Demo {


}

This works already, so I think I'll try implementing this idea with ann.xml as well.

Bye.
/lexi

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@...
For additional commands, e-mail: users-help@...


Re: custom AnnotationReader

by Kohsuke Kawaguchi :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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@...


smime.p7s (4K) Download Attachment

Re: custom AnnotationReader

by Aleksei Valikov :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi.

>> 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.

Yeah. Further options are, for instance, selecting which exactly XML resources
will come into play (like, project-ex.jar should be used but not project.jar).

> 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.

Nice idea, I'll consider it.

>> 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?

Well, yes, the difference between abstract classes and interfaces is not
relevant here.

 > 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.

Thanks, I don't think I'll have any problems implementing this. ;) This is just
something I haven't yet done before.
I think I'll avoid ASM/BCEL/Javassist at the moment to avoid additional
dependencies.

>> 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.

Yes, this does not seem like what I'm trying to do.

I've started the new "annox" project few days ago:

https://annox.dev.java.net

This will be the middle layer for annotation readers, independent of JAXB or
anything else. I think this is a nice idea which is good for Java in general.

XAnnotationReader for JAXB will be based on annox then. I'll also use annox in
my "annotate" plugin.

>> 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?

This is the part of hyperjaxb3-tools. It's not released yet and I think I'll
first implement annox and base the annotate plugin on annox.

I've also reworked the "toString" plugin and implemented the "cloneable" plugin.
All these "general-purpose" plugins (hashCode, equals, toString, cloneable,
annotate, jaxbindex) are part of the hyperjaxb3-tools module. They will be
release in 1-2 weeks independently of Hyperjaxb3 (which is also being actively
developed), when I finish documentation and add sample projects/build scripts
for developers.

Bye.
/lexi

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@...
For additional commands, e-mail: users-help@...


Re: custom AnnotationReader

by Kohsuke Kawaguchi :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Aleksei Valikov wrote:

> Yes, this does not seem like what I'm trying to do.
>
> I've started the new "annox" project few days ago:
>
> https://annox.dev.java.net
>
> This will be the middle layer for annotation readers, independent of JAXB or
> anything else. I think this is a nice idea which is good for Java in general.
>
> XAnnotationReader for JAXB will be based on annox then. I'll also use annox in
> my "annotate" plugin.
Cool. I'm looking forward to see more descriptions on the annox website.

Bit off topic, but I was wondering if we can create a maven2 site skin
for java.net. Something that blends nicely in java.net theme, instead of
the stock maven site (so that we won't have to use nonav.) I'd also like
the navigation bar to be placed in project_tools.html so that it gets
picked up by java.net system.

Have you considered something like that? Would you be interested in
working on it with me?


>>> 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?
>
> This is the part of hyperjaxb3-tools. It's not released yet and I think I'll
> first implement annox and base the annotate plugin on annox.
>
> I've also reworked the "toString" plugin and implemented the "cloneable" plugin.
> All these "general-purpose" plugins (hashCode, equals, toString, cloneable,
> annotate, jaxbindex) are part of the hyperjaxb3-tools module. They will be
> release in 1-2 weeks independently of Hyperjaxb3 (which is also being actively
> developed), when I finish documentation and add sample projects/build scripts
> for developers.
The pace in which you develop things are just amazing.

Looking forward to see them released.


--
Kohsuke Kawaguchi
Sun Microsystems                   kohsuke.kawaguchi@...


smime.p7s (4K) Download Attachment