在 Java 中生成 JAXB 类时添加 toString、hashCode、equals [英] Add toString, hashCode, equals while generating JAXB classes in Java

查看:25
本文介绍了在 Java 中生成 JAXB 类时添加 toString、hashCode、equals的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Java 以编程方式从 XSD 文件生成 JAXB 类.我使用以下代码片段来实现这一点:

I'm trying to generate JAXB classes from an XSD file programmatically, using Java. I've used the following code snippet to achieve that:

....
import java.io.File;
import java.io.IOException;
import org.xml.sax.InputSource;
import com.sun.codemodel.JCodeModel;
import com.sun.tools.xjc.api.S2JJAXBModel;
import com.sun.tools.xjc.api.SchemaCompiler;
import com.sun.tools.xjc.api.XJC;
....
....
public static void generateJaxb(String schemaPath,
                                    String outputDirectory,
                                        String packageName) throws DataLoadingException
{
    try {
        // Setup schema compiler
        SchemaCompiler sc = XJC.createSchemaCompiler();
        sc.forcePackageName(packageName);

        // Setup SAX InputSource
        File schemaFile = new File(schemaPath);
        InputSource is = new InputSource(schemaFile.toURI().toString());

        // Parse & build
        sc.parseSchema(is);
        S2JJAXBModel model = sc.bind();

        JCodeModel jCodeModel = model.generateCode(null, null);
        jCodeModel.build(new File(outputDirectory));
    } catch (IOException exec) {
        LOGGER.error("Error while generating JAXB classes: " + exec);
    }
}

生成的类只包含字段的 getter 方法.但是,我还想包括 hashCodeequalssetter 方法.如何在生成代码时做到这一点?

The generated classes contain only the getter methods for the fields. But, I want to include the hashCode, equals and setter methods as well. How to do that while generating the code?

推荐答案

在 GitHub 网站上,您会找到 JAXB2 基础项目,它提供了一组通用的 JAXB 实用程序插件,其中包括 4 个应该解决您要实现的目标:

On the GitHub website, you will find the JAXB2 Basics project, which provides a common set of JAXB utility plugins, including 4 that should address what you are trying to achieve:

  1. Equals 插件
  2. HashCode 插件
  3. Setters 插件
  4. ToString 插件

还有其他插件可用,它们涵盖了 Java 域对象的类似常见方面.

There are other plugins available that cover similar common aspects of Java domain objects.

XML Schema 配置角度来看,您将添加如下所示的引用:

From an XML Schema configuration perspective, you will add references as shown here:

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
    xmlns:basic="http://jaxb2-commons.dev.java.net/basic"
    xmlns:equals="http://jaxb2-commons.dev.java.net/basic/equals"
    xmlns:hashCode="http://jaxb2-commons.dev.java.net/basic/hashCode"
    xmlns:toString="http://jaxb2-commons.dev.java.net/basic/toString"
    jaxb:extensionBindingPrefixes="basic equals hashCode toString">
    <!-- ... -->
</xs:schema>

还有其他可用选项,例如定义在生成 equals( that ) 实现、toString() 实现等时应忽略的对象属性.

There are additional options available, such as defining object properties that should be ignored when generating an equals( that ) implementation, a toString() implementation, etc.

Java 的角度来看,插件通常让生成的类实现一个interface;例如,生成的包含 equals( that ) 实现的类将实现 [Equals][6] 接口.

From a Java perspective, the plugins generally have the generated classes implement an interface; as an example, generated classes that include an equals( that ) implementation will implement the [Equals][6] interface.

插件使用的设计方法通常会产生两种实现方式:

The design approach used by the plugins usually generates 2 flavors of implementation:

  1. 简单/标准的实现,例如 equals( that ) 方法(使用 Equals 插件 时).
  2. 更复杂的实现,包括 locatorstrategy 参数,允许您实现自定义处理(如果您愿意).对于这些,您将看到一个方法签名,例如:equals(thisLocator, thatLocator, that, strategy).
  1. A simple/standard implementation, such as an equals( that ) method (when using the Equals Plugin).
  2. A more complex implementation that includes locator and strategy parameters, which allows you to implement custom handling (if you wish). For these, you will see a method signature such as: equals( thisLocator, thatLocator, that, strategy).

构建/运行时

从运行时的角度来看,您必须包含 JAXB2 Basics Runtime jar 并提供选项参数,例如:-Xequals-XhashCode-XtoString.提供了使用 AntMaven 中的 JAXB2 基础知识的示例,如果您使用其中任何一个来执行构建,并且 JAXB2 基础用户指南.

Build/Runtime

From a runtime perspective, you must include the JAXB2 Basics Runtime jar and provide option parameters such as: -Xequals, -XhashCode, or -XtoString. There are examples provided for using the JAXB2 Basics from Ant and Maven, if you are using either of those to perform builds and more build-related details are provided in the JAXB2 Basics User Guide.

这篇关于在 Java 中生成 JAXB 类时添加 toString、hashCode、equals的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆