I am attempting to generate Java classes from a third party JSON schema using jsonschema2pojo, so I need to go with the names of classes. One of the classes is called System
. As a result, for all classes other than that class, there is an error in the toString()
method:
package com.example;
// within a class
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(Status.class.getName()).append('@').append(Integer.toHexString(System.identityHashCode(this))).append('[');
...
}
The issue is in the call to System.identityHashCode()
, because the System class is resolved to be the class in the same package, not the java.lang.System
class. The call within the System
class that's generated looks like this:
sb.append(System.class.getName()).append('@').append(Integer.toHexString(java.lang.System.identityHashCode(this))).append('[');
The JSON can be any JSON at all, i.e. even { "type": "object" }
for 2 classes, one called System
and another called Foo
will cause the error. I am generating the code using the Java API, like this:
String packageName = "com.example";
JCodeModel codeModel = new JCodeModel();
GenerationConfig config = new DefaultGenerationConfig() {
@Override
public boolean isGenerateBuilders() { // set config option by overriding method
return true;
}
};
SchemaMapper mapper = new SchemaMapper(new RuleFactory(config, new Jackson2Annotator(config), new SchemaStore()), new SchemaGenerator());
mapper.generate(codeModel, #NAME_OF_CLASS#, packageName, SOME_URL);
codeModel.build(outputDir);
How can I generate the code such that the fully qualified java.lang.System
class is used for all classes?