简体   繁体   English

如何使用jsonschema2pojo生成包含名为“ System”的类的类?

[英]How to generate classes using jsonschema2pojo that include a class called “System”?

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. 我正在尝试使用jsonschema2pojo从第三方JSON模式生成Java类,因此我需要使用类的名称。 One of the classes is called System . 其中一类称为System As a result, for all classes other than that class, there is an error in the toString() method: 结果,对于除该类之外的所有其他类, toString()方法中存在错误:

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. 问题出在对System.identityHashCode()的调用中,因为System类被解析为同一包中的类,而不是java.lang.System类。 The call within the System class that's generated looks like this: System类中生成的调用如下所示:

sb.append(System.class.getName()).append('@').append(Integer.toHexString(java.lang.System.identityHashCode(this))).append('[');

The JSON can be any JSON at all, ie even { "type": "object" } for 2 classes, one called System and another called Foo will cause the error. JSON完全可以是任何JSON,即对于2个类,甚至是{ "type": "object" } ,一个类称为System ,另一个类称为Foo也会导致错误。 I am generating the code using the Java API, like this: 我正在使用Java API生成代码,如下所示:

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? 如何生成将完全合格的java.lang.System类用于所有类的代码?

Actually, this was an issue with Intellij. 实际上,这是Intellij的问题。 The generated classes were correct, I just copied them from a generated directory to a source directory, and Intellij thought it would be helpful and simplify the class as part of the copy. 生成的类是正确的,我只是将它们从生成的目录复制到源目录,而Intellij认为这会有所帮助,并将类简化为副本的一部分。 As a result, this actually broke the generated classes. 结果,这实际上破坏了生成的类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM