在 Java 中生成 JAXB 类时,添加到字符串、哈希码等于
2022-09-03 01:33:19
我正在尝试使用Java以编程方式从XSD文件生成JAXB类。我使用以下代码片段来实现这一点:
....
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
hashCode
equals
setter