users@jaxb.java.net

Re: Using annox to generate annotations: works for me in only limited case

From: Aleksei Valikov <valikov_at_gmx.net>
Date: Fri, 18 Sep 2009 09:27:59 +0200

Hi Roger,


> I'm using annox to add annotations to my classes generated by XJC.
> This works:
> -----------------------------------------------------------------------------------
> ...
> <element name="name">
> <xsd:annotation>
> <xsd:appinfo>
> <annox:annotate>
> <MyAnnotation myName="xxx"
> xmlns="http://annox.dev.java.net/nz.co.senanque"/>
> </annox:annotate>
> </xsd:appinfo>
> </xsd:annotation>
> <simpleType>
> <restriction base="string">
> <maxLength value="30"></maxLength>
> </restriction>
> </simpleType>
> </element>
> ...
> -----------------------------------------------------------------------------------
> Where the annotation class looks like this:
> -----------------------------------------------------------------------------------
> package nz.co.senanque;
>
> import java.lang.annotation.Retention;
> import java.lang.annotation.RetentionPolicy;
>
> @Retention(RetentionPolicy.RUNTIME)
>
> public @interface MyAnnotation {
> public String myName();
>
> }
> -----------------------------------------------------------------------------------
> I have two questions on this:
>
> If I add a second parameter to MyAnnotation XJC refuses to parse it.
> This is a problem because I really do want to have multiple arguments on
> that annotation.

This must work. Annox can parse arbitrary annotations from XML, there
are no limitations on number of fields or whatever.
I have just investigated the problem you're reporting - I can't reproduce it.

Here's an example:

        <xsd:complexType name="OneType">
                <xsd:sequence>
                        <xsd:element name="a" type="xsd:string">
                                <xsd:annotation>
                                        <xsd:appinfo>
                                                <annox:annotate>
                                                        <a:Alpha longField="-1" enumField="SIX" stringField="seven">
                                                                <intField>0</intField>
                                                                <shortField>3</shortField>
                                                        </a:Alpha>
                                                </annox:annotate>
                                        </xsd:appinfo>
                                </xsd:annotation>
                        </xsd:element>
                </xsd:sequence>
        </xsd:complexType>

Here's the code that is generated:

    @Alpha(longField = -1, intField = 0, shortField = 3, enumField =
Digits.SIX, stringField = "seven")
    public String getA() {
        return a;
    }

The test is available from SVN:

https://jaxb2-commons-svn.dev.java.net/svn/jaxb2-commons-svn/trunk/basics/tests/annox/

When you say "XJC refuses to parse it" - what do you mean exactly? Are
you getting any error messages?

> Second question is curiosity. XJC fails to parse unless I add 'public' to
> 'myName' I notice the examples don't have this.
>
> Why not?

Hm, this does not make any sense to me at all. The "public" modifier
is not required on interface method declarations, this should not make
any difference at all.

I have only one idea how this can be happening. Is there a chance that
you have some old copy of nz.co.senanque.MyAnnotation somewhere on you
classpath?

The thing is, Annox need to know annotation classes you're parsing.
This means they must be available on the classpath of the plugin. For
instance, in the example I've put together there are two modules,
"annotations" and "schema". The first module contains the annotation
classes I'm going to parse, the second module is the schema
compilation with XJC. In the latter I have to include the first module
in the XJC classpath:

                        <plugin>
                                <groupId>org.jvnet.jaxb2.maven2</groupId>
                                <artifactId>maven-jaxb2-plugin</artifactId>
                                <configuration>
                                        <extension>true</extension>
                                        <args>
                                                <arg>-Xannotate</arg>
                                        </args>
                                        <plugins>
                                                <plugin>
                                                        <groupId>org.jvnet.jaxb2_commons</groupId>
                                                        <artifactId>jaxb2-basics-annotate</artifactId>
                                                        <version>0.5.2-SNAPSHOT</version>
                                                </plugin>
                                                <!-- This includes compiled annotations into the XJC classpath -->
                                                <plugin>
                                                        <groupId>org.jvnet.jaxb2_commons</groupId>
                                                        <artifactId>jaxb2-basics-test-annox-annotations</artifactId>
                                                        <version>0.5.2-SNAPSHOT</version>
                                                </plugin>
                                        </plugins>
                                </configuration>
                        </plugin>

ps. During my experiments I've found out the annotate plugin requires
codemode version 2.2 (otherwise you may get NSME on some
JAnnotationUse classes). I've made the corrections, please use
maven-jaxb2-plugin version 0.7.2. Will be available from the
repository in few hours.

Bye.
/lexi