users@jaxb.java.net

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

From: Roger Parkinson <roger_at_senanque.co.nz>
Date: Tue, 22 Sep 2009 07:55:44 +1200
Thanks for the help, Lexi!

I grabbed your example (yes, I had looked at the examples already there) and tried it in my dev environment.
Oddly it failed to recognise the -Xannotate switch until I added hyperjaxb to the path (which I want to do anyway).
But once that was there it all worked.

So with some more confidence I went back to my own example and tried it again. I happened to add both fields to the MyAnnotation so it became:

<MyAnnotation myName="xxx" myField="yyy"
xmlns="http://annox.dev.java.net/nz.co.senanque"/>
 and I changed my annotate interface accordingly.

And that just worked. Generated the right code and all. No problems.

So what I was doing wrong earlier was I was not specifying all of the arguments for the annotation. The error I got had limited information:

[ERROR] Error parsing annotation.
  line 50 of file:/home/roger/workspace/HyperJaxbSandbox/sandbox.xsd

The line number is correct.

Then I removed the 'public' from the annotation and that worked too. I think I just got confused there when trying to solve the above issue, and I may well have had classpath issues around that time as you suggested.

Your samples have optional arguments, ie you obviously don't get an error when you leave an argument off. But I do, incl on your samples. So I need to upgrade to the latest I guess. I'll do that next.

In case it's relevant (should have said this before):
jaxb 2.1.12
jaxb2_commons_* 0.2.GA
java 1.5.0-16

Thanks again
Roger

Aleksei Valikov wrote:
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

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@jaxb.dev.java.net
For additional commands, e-mail: users-help@jaxb.dev.java.net