com.zeoflow.depot.vo.Constructor.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of depot-compiler Show documentation
Show all versions of depot-compiler Show documentation
The Depot persistence library provides an abstraction layer over SQLite to allow for more robust database access while using the full power of SQLite.
The newest version!
/*
* Copyright (C) 2021 ZeoFlow SRL
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.zeoflow.depot.vo
import com.zeoflow.depot.ext.L
import com.zeoflow.depot.ext.T
import com.zeoflow.depot.compiler.processing.XExecutableElement
import com.zeoflow.depot.compiler.processing.isConstructor
import com.zeoflow.depot.compiler.processing.isMethod
import com.squareup.javapoet.CodeBlock
/**
* For each Entity / Pojo we process has a constructor. It might be the empty constructor or a
* constructor with fields. It can also be a static factory method, such as in the case of an
* AutoValue Pojo.
*/
data class Constructor(val element: XExecutableElement, val params: List) {
fun hasField(field: Field): Boolean {
return params.any {
when (it) {
is Param.FieldParam -> it.field === field
is Param.EmbeddedParam -> it.embedded.field === field
is Param.RelationParam -> it.relation.field === field
}
}
}
fun writeConstructor(outVar: String, args: String, builder: CodeBlock.Builder) {
when {
element.isConstructor() -> {
builder.addStatement(
"$L = new $T($L)", outVar,
element.enclosingElement.className, args
)
}
element.isMethod() -> {
// TODO when we generate Kotlin code, we need to handle not having enclosing
// elements.
builder.addStatement(
"$L = $T.$L($L)", outVar,
element.enclosingElement.className,
element.name, args
)
}
else -> throw IllegalStateException("Invalid constructor kind ${element.kindName()}")
}
}
sealed class Param {
abstract fun log(): String
class FieldParam(val field: Field) : Param() {
override fun log(): String = field.getPath()
}
class EmbeddedParam(val embedded: EmbeddedField) : Param() {
override fun log(): String = embedded.field.getPath()
}
class RelationParam(val relation: Relation) : Param() {
override fun log(): String = relation.field.getPath()
}
}
}