使用XJC生成XSD类会产生布尔值的死代码

时间:2016-07-14 17:26:19

标签: xsd xjc

我有以下代表各种元数据项类型的XDS:

<xs:simpleType name="BooleanTypeKey">
  <xs:restriction base="xs:string">
    <xs:enumeration value="isValue1" />
    <xs:enumeration value="isValue2" />
    <xs:enumeration value="isValue3" />
  </xs:restriction>
</xs:simpleType>

<xs:complexType name="BooleanType">
  <xs:simpleContent>
    <xs:extension base="xs:boolean">
      <xs:attribute name="key" type="BooleanTypeKey" use="required" />
    </xs:extension>
  </xs:simpleContent>
</xs:complexType>

使用以下xjc生成我的类:

<build>
  <plugins>
    <plugin>
      <groupId>org.codehaus.mojo</groupId>
      <artifactId>jaxb2-maven-plugin</artifactId>
      <version>1.6</version>
      <executions>
        <execution>
          <id>myId</id>
          <goals>
            <goal>xjc</goal>
          </goals>
          <configuration>
            <packageName>com.myorg.packagename.model</packageName>
            <schemaDirectory>/src/main/resources/xsd</schemaDirectory>
            <bindingDirectory>/src/main/resources/xjb</bindingDirectory>
            <outputDirectory>${project.build.directory}/generated/jaxb</outputDirectory>
            <extension>true</extension>
            <enableIntrospection>true</enableIntrospection>
            <arguments>-Xequals -xhasCode -xtoString</arguments>
          </configuration>
        </execution>
      </executions>
      <dependencies>
        <dependency>
          <groupId>org.jvnet.jaxb2_commons</groupId>
          <artifactId>jaxb2-basics</artifactId>
          <version>0.6.4</version>
        </dependency>
      </dependencies>
    </plugin>
  </plugins>
</build>

这将为BooleanType对象生成以下类:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "BooleanType", propOrder = {
    "value"
})
public class BooleanType implements Equals, HashCode, ToString
{
  @XmlValue
  protected Boolean value;
  @XmlAttribute(name = "key", required = true)
  protected BooleanTypeKey key;

  //... getters and setters ...

  public boolean equals(ObjectLocator thisLocator, ObjectLocator thatLocator, Object object, EqualsStrategy strategy) {

    //... basic object checks ...

    final BooleanType that = ((BooleanType) object);
    {
// This is NOT part of the generated code
// Why does the next 2 lines look like they do??? It should only read:
// boolean lhsValue = this.isValue();
// The fact that there is a check for *true*
//     (constant value check... really???)
// Not only is this completely useless, but it causes dead code production (the else part (false)).
      boolean lhsValue;
      lhsValue = (true?this.isValue():false);
      boolean rhsValue;
      rhsValue = (true?that.isValue():false);
      //...more code ...
    }
    //...more code ...
  }
}

有人可以告诉我,如果我可以避免生成这样的代码,我使用的是错误的xjc版本吗?一个版本太旧了?

谢谢,

0 个答案:

没有答案
相关问题