use of org.jooq.meta.JavaTypeResolver in project jOOQ by jOOQ.
the class JavaGenerator method generateRecordConstructor.
private void generateRecordConstructor(Definition tableUdtOrEmbeddable, JavaWriter out, Collection<? extends Definition> columns, boolean pojoArgument) {
if (pojoArgument && !generatePojos())
return;
final String className = getStrategy().getJavaClassName(tableUdtOrEmbeddable, Mode.RECORD);
final String pojoNameFull = getStrategy().getFullJavaClassName(tableUdtOrEmbeddable, Mode.POJO);
final String tableIdentifier = !(tableUdtOrEmbeddable instanceof EmbeddableDefinition) ? out.ref(getStrategy().getFullJavaIdentifier(tableUdtOrEmbeddable), 2) : null;
final int degree = columns.size();
// they can still have a pojoArgument constructor
if (pojoArgument || degree > 0 && degree < 256) {
List<String> arguments = new ArrayList<>(degree);
List<String> properties = new ArrayList<>(degree);
for (Definition column : columns) {
final String columnMember = getStrategy().getJavaMemberName(column, Mode.DEFAULT);
final String type = getJavaTypeRef(column, out);
if (scala) {
arguments.add(columnMember + " : " + type);
} else if (kotlin) {
arguments.add(columnMember + ": " + type + "? = null");
} else {
final String nullableAnnotation = column instanceof EmbeddableDefinition ? null : nullableOrNonnullAnnotation(out, column);
arguments.add((nullableAnnotation == null ? "" : "@" + nullableAnnotation + " ") + type + " " + columnMember);
}
properties.add("\"" + escapeString(columnMember) + "\"");
}
out.javadoc("Create a detached, initialised %s", className);
if (scala) {
if (pojoArgument)
out.println("%sdef this(value: %s) = {", visibility(), out.ref(pojoNameFull));
else
out.println("%sdef this([[%s]]) = {", visibility(), arguments);
out.println("this()", tableIdentifier);
out.println();
} else if (kotlin) {
if (pojoArgument)
out.println("%sconstructor(value: %s?): this() {", visibility(), out.ref(pojoNameFull));
else
out.println("%sconstructor([[%s]]): this() {", visibility(), arguments);
} else {
if (generateConstructorPropertiesAnnotationOnRecords())
out.println("@%s({ [[%s]] })", out.ref("java.beans.ConstructorProperties"), properties);
if (pojoArgument)
out.println("%s%s(%s value) {", visibility(), className, out.ref(pojoNameFull));
else
out.println("%s%s([[%s]]) {", visibility(), className, arguments);
if (tableUdtOrEmbeddable instanceof EmbeddableDefinition)
out.println("this();", tableIdentifier);
else
out.println("super(%s);", tableIdentifier);
out.println();
}
if (pojoArgument && degree > 0)
out.println("if (value != null) {");
for (Definition column : columns) {
if (column instanceof EmbeddableDefinition) {
// TODO: Setters of X properties cannot accept X? in Kotlin: https://twitter.com/lukaseder/status/1296371561214234624
if (kotlin)
if (pojoArgument)
out.println("this.%s = %s(%s) ?: %s([[%s]])", getStrategy().getJavaMemberName(column, Mode.POJO), out.ref(getStrategy().getFullJavaClassName(column, Mode.RECORD)), getStrategy().getJavaMemberName(column, Mode.POJO), out.ref(getStrategy().getFullJavaClassName(column, Mode.RECORD)), Collections.nCopies(((EmbeddableDefinition) column).getColumns().size(), "null"));
else
out.println("this.%s = %s ?: %s([[%s]])", getStrategy().getJavaMemberName(column, Mode.POJO), getStrategy().getJavaMemberName(column, Mode.POJO), out.ref(getStrategy().getFullJavaClassName(column, Mode.RECORD)), Collections.nCopies(((EmbeddableDefinition) column).getColumns().size(), "null"));
else // In Scala, the setter call can be ambiguous, e.g. when using KeepNamesGeneratorStrategy
if (scala)
if (pojoArgument)
out.println("this.%s(new %s(value.%s))", getStrategy().getJavaSetterName(column, Mode.RECORD), out.ref(getStrategy().getFullJavaClassName(column, Mode.RECORD)), getStrategy().getJavaGetterName(column, Mode.DEFAULT));
else
out.println("this.%s(%s)", getStrategy().getJavaSetterName(column, Mode.RECORD), getStrategy().getJavaMemberName(column, Mode.DEFAULT));
else if (pojoArgument)
out.println("%s(new %s(value.%s()));", getStrategy().getJavaSetterName(column, Mode.RECORD), out.ref(getStrategy().getFullJavaClassName(column, Mode.RECORD)), generatePojosAsJavaRecordClasses() ? getStrategy().getJavaMemberName(column, Mode.POJO) : getStrategy().getJavaGetterName(column, Mode.DEFAULT));
else
out.println("%s(%s);", getStrategy().getJavaSetterName(column, Mode.RECORD), getStrategy().getJavaMemberName(column, Mode.DEFAULT));
} else {
final TypedElementDefinition<?> t = (TypedElementDefinition<?>) column;
final JavaTypeResolver r = resolver(out, Mode.RECORD);
final boolean isUDT = t.getType(r).isUDT();
final boolean isArray = t.getType(r).isArray();
final boolean isUDTArray = t.getType(r).isUDTArray();
final boolean isArrayOfUDTs = isArrayOfUDTs(t, r);
final String udtType = (isUDT || isArray) ? out.ref(getJavaType(((TypedElementDefinition<?>) column).getType(r), out, Mode.RECORD)) : "";
final String udtArrayElementType = isUDTArray ? out.ref(database.getArray(t.getType(r).getSchema(), t.getType(r).getQualifiedUserType()).getElementType(r).getJavaType(r)) : isArrayOfUDTs ? out.ref(getArrayBaseType(t.getType(r).getJavaType(r))) : "";
if (kotlin) {
if (pojoArgument)
if (isUDTArray)
out.println("this.%s = value.%s?.let { %s(it.map { it?.let { %s(it) } }) }", getStrategy().getJavaMemberName(column, Mode.POJO), getStrategy().getJavaMemberName(column, Mode.POJO), udtType, udtArrayElementType);
else if (isArrayOfUDTs)
out.println("this.%s = value.%s?.let { it.map { it?.let { %s(it) } }.toTypedArray() }", getStrategy().getJavaMemberName(column, Mode.POJO), getStrategy().getJavaMemberName(column, Mode.POJO), udtArrayElementType);
else if (isUDT || isArray)
out.println("this.%s = value.%s?.let { %s(it) }", getStrategy().getJavaMemberName(column, Mode.POJO), getStrategy().getJavaMemberName(column, Mode.POJO), udtType);
else
out.println("this.%s = value.%s", getStrategy().getJavaMemberName(column, Mode.POJO), getStrategy().getJavaMemberName(column, Mode.POJO));
else
out.println("this.%s = %s", getStrategy().getJavaMemberName(column, Mode.POJO), getStrategy().getJavaMemberName(column, Mode.POJO));
} else // In Scala, the setter call can be ambiguous, e.g. when using KeepNamesGeneratorStrategy
if (scala) {
if (pojoArgument)
if (isUDTArray)
out.println("this.%s(if (value.%s == null) null else new %s(value.%s.stream().map { it => new %s(it) }.collect(%s.toList())))", getStrategy().getJavaSetterName(column, Mode.POJO), getStrategy().getJavaGetterName(column, Mode.POJO), udtType, getStrategy().getJavaGetterName(column, Mode.POJO), udtArrayElementType, Collectors.class);
else if (isArrayOfUDTs)
out.println("this.%s(if (value.%s == null) null else value.%s.map { it => new %s(it) })", getStrategy().getJavaSetterName(column, Mode.POJO), getStrategy().getJavaGetterName(column, Mode.POJO), getStrategy().getJavaGetterName(column, Mode.POJO), udtArrayElementType);
else if (isUDT || isArray)
out.println("this.%s(if (value.%s == null) null else new %s(value.%s))", getStrategy().getJavaSetterName(column, Mode.RECORD), getStrategy().getJavaGetterName(column, Mode.POJO), udtType, getStrategy().getJavaGetterName(column, Mode.POJO));
else
out.println("this.%s(value.%s)", getStrategy().getJavaSetterName(column, Mode.RECORD), getStrategy().getJavaGetterName(column, Mode.POJO));
else
out.println("this.%s(%s)", getStrategy().getJavaSetterName(column, Mode.RECORD), getStrategy().getJavaMemberName(column, Mode.POJO));
} else {
if (pojoArgument)
if (isUDTArray)
out.println("%s(value.%s() == null ? null : new %s(value.%s().stream().map(%s::new).collect(%s.toList())));", getStrategy().getJavaSetterName(column, Mode.RECORD), getStrategy().getJavaGetterName(column, Mode.POJO), udtType, generatePojosAsJavaRecordClasses() ? getStrategy().getJavaMemberName(column, Mode.POJO) : getStrategy().getJavaGetterName(column, Mode.POJO), udtArrayElementType, Collectors.class);
else if (isArrayOfUDTs)
out.println("%s(value.%s() == null ? null : %s.of(value.%s()).map(%s::new).toArray(%s[]::new));", getStrategy().getJavaSetterName(column, Mode.RECORD), getStrategy().getJavaGetterName(column, Mode.POJO), Stream.class, generatePojosAsJavaRecordClasses() ? getStrategy().getJavaMemberName(column, Mode.POJO) : getStrategy().getJavaGetterName(column, Mode.POJO), udtArrayElementType, udtArrayElementType);
else if (isUDT || isArray)
out.println("%s(value.%s() == null ? null : new %s(value.%s()));", getStrategy().getJavaSetterName(column, Mode.RECORD), getStrategy().getJavaGetterName(column, Mode.POJO), udtType, generatePojosAsJavaRecordClasses() ? getStrategy().getJavaMemberName(column, Mode.POJO) : getStrategy().getJavaGetterName(column, Mode.POJO));
else
out.println("%s(value.%s());", getStrategy().getJavaSetterName(column, Mode.RECORD), generatePojosAsJavaRecordClasses() ? getStrategy().getJavaMemberName(column, Mode.POJO) : getStrategy().getJavaGetterName(column, Mode.POJO));
else
out.println("%s(%s);", getStrategy().getJavaSetterName(column, Mode.RECORD), getStrategy().getJavaMemberName(column, Mode.POJO));
}
}
}
if (pojoArgument && degree > 0)
out.println("}");
out.println("}");
}
}
Aggregations