|
View:
New views
4 Messages
—
Rating Filter:
Alert me
|
|
|
Using annox to generate annotations: works for me in only limited case
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. Second question is curiosity. XJC fails to parse unless I add 'public' to 'myName' I notice the examples don't have this. Why not? With luck the questions are related and I've missed something important. Thanks for the help Roger --------------------------------------------------------------------- To unsubscribe, e-mail: users-unsubscribe@... For additional commands, e-mail: users-help@... |
|
|
Re: Using annox to generate annotations: works for me in only limited caseHi 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@... For additional commands, e-mail: users-help@... |
|
|
Re: Using annox to generate annotations: works for me in only limited caseI 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, <?xml version="1.0" encoding="UTF-8"?> <schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.example.org/sandbox" xmlns:tns="http://www.example.org/sandbox" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" jaxb:version="2.0" xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc" jaxb:extensionBindingPrefixes="xjc hj annox" xmlns:hj="http://hyperjaxb3.jvnet.org/ejb/schemas/customizations" xmlns:annox="http://annox.dev.java.net" xmlns:md="http://annox.dev.java.net/nz.co.senanque.validationengine" elementFormDefault="qualified"> <xsd:annotation> <xsd:appinfo> <jaxb:globalBindings generateIsSetMethod="false"/> <jaxb:schemaBindings> <jaxb:package name="nz.co.senanque.madura.sandbox"/> </jaxb:schemaBindings> </xsd:appinfo> </xsd:annotation> <!-- ... --> <complexType name="Customer"> <sequence> <element name="id" type="long"> <xsd:annotation> <xsd:appinfo> <hj:id> <hj:generated-value strategy="AUTO"/> </hj:id> </xsd:appinfo> </xsd:annotation> </element> <element name="version" type="long"> <annotation> <appinfo> <hj:version/> </appinfo> </annotation> </element> <element name="business" type="tns:IndustryType"/> <element name="key"> <simpleType> <restriction base="string"> <maxLength value="5"></maxLength> </restriction> </simpleType> </element> <element name="name"> <xsd:annotation> <xsd:appinfo> <annox:annotate> <md:Label labelName="xxx"/> <md:Regex regex="a*b"/> <md:Description name="this is a description"/> <md:MapField name="whatever"/> </annox:annotate> </xsd:appinfo> </xsd:annotation> <simpleType> <restriction base="string"> <maxLength value="30"></maxLength> <pattern value="xyz"></pattern> </restriction> </simpleType> </element> <element name="address" type="string"> <xsd:annotation> <xsd:appinfo> <annox:annotate> <md:Label labelName="xxx"/> <!-- <md:Range minValue="xxx" maxValue="yyy"/> <md:BeanValidator bean="xxx" param="yyy"/> <md:Description name="this is a description"/> --> </annox:annotate> </xsd:appinfo> </xsd:annotation> </element> <element name="invoices" type="tns:Invoice" maxOccurs="unbounded" minOccurs="0"></element> </sequence> </complexType> <complexType name="Invoice"> <sequence> <element name="id" type="string" nillable="false"></element> <element name="parentid" type="string" nillable="false"></element> <element name="description" type="string"></element> <element name="amount" type="double"></element> <element name="outstanding" type="double"></element> <element name="date" type="date"></element> </sequence> </complexType> <element name="Session" type="tns:Session"></element> <complexType name="Session"> <sequence> <element name="user" type="string" nillable="false"></element> <element name="started" type="dateTime"></element> <element name="customers" type="tns:Customer" maxOccurs="unbounded" minOccurs="0"></element> </sequence> </complexType> <xsd:simpleType name="IndustryType"> <xsd:restriction base="xsd:string"> <xsd:enumeration value="Ag"/> <xsd:enumeration value="fish"/> <xsd:enumeration value="finance"/> </xsd:restriction> </xsd:simpleType> </schema> |
|
|
Re: Using annox to generate annotations: works for me in only limited caseHi,
> 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). You don't need Hyperjaxb3, you only need jaxb2-basics-annote dependencies on your classpath. > 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. Try building with mvn -X, this might give more information. > 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 Quite old. Current development version is 0.5.2-SNAPSHOT, so it's been a while. Bye, /lexi --------------------------------------------------------------------- To unsubscribe, e-mail: users-unsubscribe@... For additional commands, e-mail: users-help@... |
| Free embeddable forum powered by Nabble | Forum Help |