下面列出了怎么用org.jsonschema2pojo.GenerationConfig的API类实例代码及写法,或者点击链接到github查看源代码。
public IstioTypeAnnotator(GenerationConfig generationConfig) {
super(generationConfig);
String className = DONEABLE_CLASS_NAME;
try {
doneableClass = new JCodeModel()._class(className);
className = OBJECT_META_CLASS_NAME;
objectMetaClass = new JCodeModel()._class(className);
} catch (JClassAlreadyExistsException e) {
throw new IllegalStateException("Couldn't load " + className);
}
}
public CustomTypeAnnotator(GenerationConfig generationConfig) {
//called once for each json schema defined
super(generationConfig);
//load into a table the custom json schema fields
for (int j = 0; j < schemaCustomFields.length; j++) {
JsonObject jo = new JsonObject(schemaCustomFields[j]);
String fieldName = jo.getString("fieldname");
Object fieldValue = jo.getValue("fieldvalue");
JsonObject annotation = jo.getJsonObject("annotation");
if(annotationLookUp.get(fieldName, fieldValue) == null){
annotationLookUp.put(fieldName , fieldValue, annotation);
log.info("Loading custom field " + fieldName + " with value " + fieldValue + " with annotation " + annotation.encode());
}
}
}
public KubernetesTypeAnnotator(GenerationConfig generationConfig) {
super(generationConfig);
}
/**
* Generates a Java Bean class for the given bean info. The bean info should
* have a name, package, and JSON Schema. This information will be used to
* generate a POJO.
* @param bean
* @param info
* @param codeWriter
* @throws IOException
*/
private void generateJavaBean(CodegenJavaBean bean, CodegenInfo info, IndexedCodeWriter codeWriter) throws IOException {
JCodeModel codeModel = new JCodeModel();
GenerationConfig config = new DefaultGenerationConfig() {
@Override
public boolean isUsePrimitives() {
return false;
}
@Override
public boolean isIncludeHashcodeAndEquals() {
return false;
}
@Override
public boolean isIncludeAdditionalProperties() {
return false;
}
@Override
public boolean isIncludeToString() {
return false;
}
};
SchemaMapper schemaMapper = new SchemaMapper(
new JaxRsRuleFactory(config, new Jackson2Annotator(config), new SchemaStore() {
@Override
public Schema create(Schema parent, String path, String refFragmentPathDelimiters) {
String beanClassname = schemaRefToFQCN(path);
for (CodegenJavaBean cgBean : info.getBeans()) {
String cgBeanFQCN = cgBean.getPackage() + "." + StringUtils.capitalize(cgBean.getName());
if (beanClassname.equals(cgBeanFQCN)) {
Schema schema = new Schema(classnameToUri(beanClassname), cgBean.get$schema(), null);
JType jclass = codeModel._getClass(beanClassname);
if (jclass == null) {
jclass = codeModel.directClass(beanClassname);
}
schema.setJavaType(jclass);
return schema;
}
}
System.out.println("!!!!! :: " + beanClassname);
// TODO if we get here, we probably want to return an empty schema
return super.create(parent, path, refFragmentPathDelimiters);
}
}),
new SchemaGenerator());
String source = mapper.writeValueAsString(bean.get$schema());
schemaMapper.generate(codeModel, bean.getName(), bean.getPackage(), source);
codeModel.build(codeWriter);
}
/**
* Constructor.
*/
public JaxRsRuleFactory(GenerationConfig generationConfig, Annotator annotator, SchemaStore schemaStore) {
super(generationConfig, annotator, schemaStore);
}
/**
* Returns a generation config with the supplied parameters. If any of these
* parameters are supplied null, it will use the value defined in the
* default configuration
*
* @param generateBuilders
* Enables or disables
* {@link GenerationConfig#isGenerateBuilders()}
* @param includeAdditionalProperties
* Enables or disables
* {@link GenerationConfig#isIncludeAdditionalProperties()}
* @param includeDynamicAccessors
* Enables or disables
* {@link GenerationConfig#isIncludeDynamicAccessors()}
* @param useLongIntegers
* Enables or disables
* {@link GenerationConfig#isUseLongIntegers()}
* @return The GenerationConfig
*/
public static GenerationConfig getGenerationConfig(Boolean generateBuilders, Boolean includeAdditionalProperties,
Boolean includeDynamicAccessors, Boolean useLongIntegers) {
return new DefaultGenerationConfig() {
@Override
public boolean isGenerateBuilders() { // set config option by
// overriding method
if (generateBuilders != null) {
return generateBuilders;
} else {
return true;
}
}
@Override
public boolean isIncludeAdditionalProperties() {
if (includeAdditionalProperties != null) {
return includeAdditionalProperties;
} else {
return false;
}
}
@Override
public boolean isIncludeDynamicAccessors() {
if (includeDynamicAccessors != null) {
return includeDynamicAccessors;
} else {
return false;
}
}
@Override
public boolean isUseLongIntegers() {
if (useLongIntegers != null) {
return useLongIntegers;
} else {
return super.isUseLongIntegers();
}
}
};
}
/**
* Returns a configuration for the JSON Schema 2 POJO that is in line with
* the defaults used in the plugin so far
*
* @return Default Generation Config
*/
public static GenerationConfig getDefaultGenerationConfig() {
return getGenerationConfig(true, false, false, false);
}