How to remove "is" from JAXB-created boolean getters

JAXB generates methods like: isIsBuyNow() and isHasBuyNow() when I want isBuyNow() or hasBuyNow() .

There are many class methods, so it is not possible to customize the names of the generated methods.

Some regex rule for configuring JAXB is the first thing that comes to mind, but I cannot find such a general way to change method names.

+6
source share
3 answers

Of the sounds of it, you only need to change the names of the methods, not the names of the classes. You can also:

  • find an XJC plugin that allows this, or
  • write an XJC plugin that does this

I doubt the first option exists (although you may be lucky), but the second option seems pretty simple. See this page for some posts on how to write your own.

+2
source

You can use an external binding file to customize the name of your property. For JAXB to recognize a method as a property, it must follow the start convention with get or is for boolean types.

schema.xsd

 <?xml version="1.0" encoding="utf-8" ?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:complexType name="root"> <xs:attribute name="is-buy-now" type="xs:boolean"/> </xs:complexType> </xs:schema> 

bindings.xml

In the bind file below, we specified the property name for the XML attribute is-buy-now :

 <jxb:bindings xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:jxb="http://java.sun.com/xml/ns/jaxb" version="2.1"> <jxb:bindings schemaLocation="is.xsd"> <jxb:bindings node="//xs:complexType[@name='root']/xs:attribute[@name='is-buy-now']"> <jxb:property name="buyNow"/> </jxb:bindings> </jxb:bindings> </jxb:bindings> 

XJC challenge

The -b option is used to specify a binding file:

 xjc -d out -b bindings.xml schema.xsd 

Root

As a result, the following class will be created using the isBuyNow and setBuyNow :

 // // This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, vJAXB 2.1.10 in JDK 6 // See <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a> // Any modifications to this file will be lost upon recompilation of the source schema. // Generated on: 2012.02.03 at 05:18:59 AM EST // package generated; import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlAttribute; import javax.xml.bind.annotation.XmlType; /** * <p>Java class for root complex type. * * <p>The following schema fragment specifies the expected content contained within this class. * * <pre> * &lt;complexType name="root"> * &lt;complexContent> * &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType"> * &lt;attribute name="is-buy-now" type="{http://www.w3.org/2001/XMLSchema}boolean" /> * &lt;/restriction> * &lt;/complexContent> * &lt;/complexType> * </pre> * * */ @XmlAccessorType(XmlAccessType.FIELD) @XmlType(name = "root") public class Root { @XmlAttribute(name = "is-buy-now") protected Boolean buyNow; /** * Gets the value of the buyNow property. * * @return * possible object is * {@link Boolean } * */ public Boolean isBuyNow() { return buyNow; } /** * Sets the value of the buyNow property. * * @param value * allowed object is * {@link Boolean } * */ public void setBuyNow(Boolean value) { this.buyNow = value; } } 

UPDATE

If you start with Java classes, you can specify @XmlAccessorType(XmlAccessType.FIELD) to map the fields and the name of your methods of your choice:

 @XmlAccessorType(XmlAccessType.FIELD) public class Root { @XmlAttribute(name="has-buy-now") private boolean buyNow; public boolean hasBuyNow() { return buyNow; } } 
+3
source

Before you go through the plugin route. I would like to emphasize that you are calling a variable name. It must meet Java Bean requirements. And JAXB should only work on this.

therefore, if the variable is boolean, ist should idealize buyNow. The corresponding getter method will become the isBuyNow () you want. It would regenerate the display again. There is no regeex no custom find / replace, as you (rightfully) want :)

+1
source

Source: https://habr.com/ru/post/907540/


All Articles