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);
}
}
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?