Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,26 @@ import com.squareup.kotlinpoet.asTypeName
import com.squareup.kotlinpoet.joinToCode
import kotlin.reflect.KClass

private val PRIMITIVE_ARRAY_FULL_NAMES =
setOf("boolean[]", "byte[]", "char[]", "short[]", "int[]", "long[]", "float[]", "double[]")

// Component type of an object array (e.g. String[]), or null for non-arrays and primitive
// arrays. Primitive arrays (int[], byte[], ...) keep their dedicated Kotlin array class
// (IntArray, ByteArray, ...) via asClassName() and must not be wrapped in Array<T>.
private fun Type.objectArrayComponentType(): Type? =
componentType?.takeUnless { fullName in PRIMITIVE_ARRAY_FULL_NAMES }

@JvmOverloads
fun Type.asTypeName(out: Boolean = false): TypeName = asClassName().let { className ->
if (parameters.isNotEmpty())
className.parameterizedBy(*parameters.map { if (out) it.asOutTypeName() else it.asTypeName() }.toTypedArray()) else className
fun Type.asTypeName(out: Boolean = false): TypeName {
// Object arrays (e.g. String[]) must be rendered as the Kotlin Array<T> type.
objectArrayComponentType()?.let { component ->
return Array::class.asClassName()
.parameterizedBy(if (out) component.asOutTypeName() else component.asTypeName())
}
return asClassName().let { className ->
if (parameters.isNotEmpty())
className.parameterizedBy(*parameters.map { if (out) it.asOutTypeName() else it.asTypeName() }.toTypedArray()) else className
}
}

fun Type.asClassName(): ClassName = when (this.fullName) {
Expand Down Expand Up @@ -67,7 +83,10 @@ private fun Type.enclosingTypeHierarchy(): List<String> {

fun ClassName.asClassStatement() = CodeBlock.of("%T::class.java", this)

fun Type.asClassNameStatement() = asClassName().asClassStatement()
fun Type.asClassNameStatement(): CodeBlock =
// Object arrays need the Kotlin Array<T>::class.java form to resolve to the right runtime class.
objectArrayComponentType()?.let { CodeBlock.of("%T::class.java", asTypeName()) }
?: asClassName().asClassStatement()

fun TypeMappings.getPathClassName(type: Type, model: EntityType) = getPathType(type, model, true).asClassName()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,18 @@ class EntitySerializerTest {
assertCompiles("QEntity", writer.toString())
}

@Test
fun object_array() {
val type = SimpleType(TypeCategory.ENTITY, "Entity", "", "Entity", false, false)
val entityType = EntityType(type)
entityType.addProperty(Property(entityType, "tags", ClassType(TypeCategory.ARRAY, Array<String>::class.java)))
typeMappings.register(entityType, queryTypeFactory.create(entityType))
serializer.serialize(entityType, SimpleSerializerConfig.DEFAULT, JavaWriter(writer))
Assertions.assertTrue(writer.toString().contains("val tags: ArrayPath<Array<String>, String>"))
Assertions.assertTrue(writer.toString().contains("createArray(\"tags\", Array<String>::class.java)"))
assertCompiles("QEntity", writer.toString())
}

@Test
fun include() {
val type = SimpleType(TypeCategory.ENTITY, "Entity", "", "Entity", false, false)
Expand Down
Loading