in johnzon-jsonschema/src/main/java/org/apache/johnzon/jsonschema/generator/PojoGenerator.java [67:173]
public Map<String, String> generate() {
if (isEnum) {
return nested;
}
final String name = configuration.getPackageName() + '.' + configuration.getClassName();
final String path = name.replace('.', '/') + ".java";
attributes.sort(comparing(a -> a.javaName));
if (!attributes.isEmpty()) {
imports.add(Objects.class.getName());
}
final String content = "" +
"package " + configuration.getPackageName() + ";\n" +
"\n" +
(imports.isEmpty() ? "" : imports.stream()
.sorted()
.map(it -> "import " + it + ";")
.collect(joining("\n", "", "\n\n"))) +
beforeClassDeclaration() +
"public class " + configuration.getClassName() + afterClassName() + " {\n" +
(attributes.isEmpty() ?
("" +
" @Override\n" +
" public int hashCode() {\n" +
" return 0;\n" +
" }\n" +
"\n" +
" @Override\n" +
" public boolean equals(final Object other) {\n" +
" return other instanceof " + configuration.getClassName() + ";\n" +
" }\n") :
(attributes.stream()
.map(a -> "" +
(configuration.isAddJsonbProperty() && !Objects.equals(a.javaName, a.jsonName) ?
" @JsonbProperty(\"" + a.jsonName.replace("\"", "\\\"") + "\")\n" :
"") +
" private " + a.type + " " + a.javaName + ";")
.collect(joining("\n", "", "\n\n")) +
(configuration.isAddAllArgsConstructor() ?
" public " + configuration.getClassName() + "() {\n" +
" // no-op\n" +
" }\n" +
"\n" +
" public " + configuration.getClassName() + "(" +
attributes.stream()
.map(a -> "final " + a.type + " " + a.javaName)
.collect(joining(
",\n" + IntStream.range(
0,
" public (".length() +
configuration.getClassName().length())
.mapToObj(i -> " ")
.collect(joining()),
"",
") {\n" +
attributes.stream()
.map(a -> " this." + a.getJavaName() + " = " + a.javaName + ";\n")
.collect(joining()) +
" }\n\n")) :
"") +
attributes.stream()
.map(a -> {
final String marker = Character.toUpperCase(a.javaName.charAt(0)) + a.javaName.substring(1);
return "" +
" public " + a.type + " get" + Character.toUpperCase(a.javaName.charAt(0)) + a.javaName.substring(1) + "() {\n" +
" return " + a.javaName + ";\n" +
" }\n" +
"\n" +
" public " +
(configuration.isFluentSetters() ? configuration.getClassName() : "void") +
" set" + marker + "(final " + a.type + " " + a.javaName + ") {\n" +
" this." + a.javaName + " = " + a.javaName + ";\n" +
(configuration.isFluentSetters() ? " return this;\n" : "") +
" }\n" +
"";
})
.collect(joining("\n", "", "\n")) +
" @Override\n" +
" public int hashCode() {\n" +
" return Objects.hash(\n" +
attributes.stream()
.map(a -> a.javaName)
.collect(joining(",\n ", " ", ");\n")) +
" }\n" +
"\n" +
" @Override\n" +
" public boolean equals(final Object __other) {\n" +
" if (!(__other instanceof " + configuration.getClassName() + ")) {\n" +
" return false;\n" +
" }\n" +
" final " + configuration.getClassName() + " __otherCasted = (" + configuration.getClassName() + ") __other;\n" +
" return " + attributes.stream()
.map(a -> a.javaName)
.map(it -> "Objects.equals(" + it + ", __otherCasted." + it + ")")
.collect(joining(" &&\n ")) + ";\n" +
" }\n"
)) +
beforeClassEnd() +
"}\n";
if (nested.isEmpty()) {
return singletonMap(path, content);
}
return Stream.concat(
nested.entrySet().stream(),
Stream.of(new AbstractMap.SimpleImmutableEntry<>(path, content)))
.collect(toMap(Map.Entry::getKey, Map.Entry::getValue, (a, b) -> a, () -> new TreeMap<>(String::compareTo)));
}