Skip to content

Commit

Permalink
Merge pull request #45 from SchwarzIT/feature/fix_missing_import
Browse files Browse the repository at this point in the history
Fix: generated classes can include a class without import
  • Loading branch information
michael-gut-ergon authored Aug 2, 2021
2 parents 1edc9e2 + 5b71f35 commit 3bbd93a
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ object EntityFactory {

parseStaticsFromStructure(cblEntityElement) {
if (it.getAnnotation(GenerateAccessor::class.java) != null) {
content.generateAccessors.add(CblGenerateAccessorHolder(content.sourceClazzSimpleName, it))
content.generateAccessors.add(CblGenerateAccessorHolder(content.sourceClazzTypeName, it))
}
it.getAnnotation(DocIdSegment::class.java)?.apply {
docIdSegments.add(DocIdSegmentHolder(this, it))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ import javax.lang.model.element.VariableElement
import javax.lang.model.type.TypeMirror
import kotlin.coroutines.Continuation

class CblGenerateAccessorHolder(private val className: String, val element: Element) {
class CblGenerateAccessorHolder(private val typeName: TypeName, val element: Element) {

fun accessorFunSpec(): FunSpec? {

if (element.kind == ElementKind.METHOD) {
var methodBuilder = FunSpec.builder(element.simpleName.toString()).addAnnotation(JvmStatic::class)
val methodBuilder = FunSpec.builder(element.simpleName.toString()).addAnnotation(JvmStatic::class)

(element as? ExecutableElement)?.apply {
val callParams = arrayListOf<String>()
Expand All @@ -25,11 +25,20 @@ class CblGenerateAccessorHolder(private val className: String, val element: Elem
methodBuilder.addModifiers(KModifier.SUSPEND)
} else {
callParams.add(it.simpleName.toString())
methodBuilder.addParameter(it.simpleName.toString(), evaluateTypeName(it.asType(), it.getAnnotation(Nullable::class.java) != null))
methodBuilder.addParameter(
it.simpleName.toString(),
evaluateTypeName(it.asType(), it.getAnnotation(Nullable::class.java) != null)
)
}
}

methodBuilder.addCode(CodeBlock.of("return %N.%N(${callParams.joinToString()})", className, element.simpleName.toString()))
methodBuilder.addCode(
CodeBlock.of(
"return %T.%N(${callParams.joinToString()})" + System.lineSeparator(),
typeName,
element.simpleName.toString()
)
)
}
return methodBuilder.build()
}
Expand All @@ -46,7 +55,12 @@ class CblGenerateAccessorHolder(private val className: String, val element: Elem

fun accessorPropertySpec(): PropertySpec? {
if (element.kind == ElementKind.FIELD) {
return PropertySpec.builder(element.simpleName.toString(), evaluateTypeName(element.asType(), element.getAnnotation(Nullable::class.java) != null)).addAnnotation(JvmField::class).initializer("%N.%N", className, element.simpleName.toString()).build()
return PropertySpec.builder(
element.simpleName.toString(),
evaluateTypeName(element.asType(), element.getAnnotation(Nullable::class.java) != null)
).addAnnotation(JvmField::class)
.initializer("%T.%N", typeName, element.simpleName.toString())
.build()
}
return null
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ abstract class BaseEntityHolder {
val sourceClazzSimpleName: String
get() = (sourceElement as Symbol.ClassSymbol).simpleName.toString()

val sourceClazzTypeName: TypeName
get() = ClassName(`package`, sourceClazzSimpleName)

open val entitySimpleName: String
get() = sourceClazzSimpleName + "Entity"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import kaufland.com.coachbasebinderapi.mapify.Mapify
import kaufland.com.coachbasebinderapi.mapify.Mapifyable
import kaufland.com.coachbasebinderapi.mapify.Mapper
import kaufland.com.demo.entity.ProductEntity
import kaufland.com.demo.entity.TestClass
import java.io.Serializable
import java.math.BigDecimal

Expand Down

0 comments on commit 3bbd93a

Please sign in to comment.