Use case:
Wanna insert custom annotation to fields in java class generated by JAXB
Problem:
Using Annotate plugin + JAXB [1], am able to successfully insert custom annotations but they are getting inserted at getter method rather than field. Morphia (mongo DB) annotations (that i actually want to insert) however can annotate only java fields [2].
My test xsd:
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" jaxb:version="2.1"
xmlns:annox="http://annox.dev.java.net" jaxb:extensionBindingPrefixes="annox">
<xsd:element name="hoo" type="External" />
<xsd:complexType name="External">
<xsd:sequence>
<xsd:element name="bar" type="xsd:string" />
<xsd:element name="hoobar" type="xsd:string" />
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
My test binding xjb:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<jaxb:bindings
version="2.1"
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:annox="http://annox.dev.java.net" jaxb:extensionBindingPrefixes="annox">
<jaxb:bindings schemaLocation="external.xsd" node="/xs:schema">
<jaxb:bindings node="xs:complexType[@name='External']/xs:sequence/xs:element[@name='bar']">
<annox:annotate>
<annox:annotate
annox:class="java.lang.SuppressWarnings"
impl="com.acme.foo.MyFieldBridge">
</annox:annotate>
</annox:annotate>
</jaxb:bindings>
My generated java snippet:
@XmlElement(required = true)
protected String bar;
@XmlElement(required = true)
protected String hoobar;
/**
* Gets the value of the bar property.
*
* @return
* possible object is
* {@link String }
*
*/
@SuppressWarnings({
})
public String getBar() {
return bar;
}
As you can see, i want to annotate "bar" field. Please advise. Ask for more if needed.
[1] Generate @Indexed annotation using Jaxb or HyperJaxb
[2] For sample see @Id annotation of Morphia
To create your own Java Annotation you must use @interface Annotation_name, this will create a new Java Annotation for you. The @interface will describe the new annotation type declaration. After giving a name to your Annotation, you will need to create a block of statements inside which you may declare some variables.
Java Custom annotations or Java User-defined annotations are easy to create and use. The @interface element is used to declare an annotation. For example: @interface MyAnnotation{}
xsd is the XML schema you will use as input to the JAXB binding compiler, and from which schema-derived JAXB Java classes will be generated.
If class is annotated with @XmlType(name="") , it is mapped to an anonymous type otherwise, the class name maps to a complex type name. The XmlName() annotation element can be used to customize the name. Properties and fields that are mapped to elements are mapped to a content model within a complex type.
Ok, you figured it out yourself. Use <annox:annotate target="field">
to annotate a field. Other options are:
See the documentation.
Just one more thing: you need to put the field
attribute to the outer <annox:annotate>
tag:
<annox:annotate target="field">
<annox:annotate annox:class="java.lang.SuppressWarnings"/>
</annox:annotate>
Putting it to the same tag as the annox:class
attribute resides might not work. That happend to me.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With