Skip to content

Commit

Permalink
# implemented possibility to generate open wrappers
Browse files Browse the repository at this point in the history
# generate interface for basedOn Models
  • Loading branch information
sbra0902 committed Nov 25, 2020
1 parent b4ac55d commit 497e740
Show file tree
Hide file tree
Showing 9 changed files with 26 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,6 @@
public @interface MapWrapper {

Class<?> value() default Void.class;

boolean modifierOpen() default false;
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class CoachBaseBinderProcessor : AbstractProcessor() {
var mapWrapperStrings = mapWrappers.map { element -> element.toString() }
var generatedInterfaces = mutableSetOf<String>()

var baseModels = validateAndCreateBaseModelMap(roundEnv.getElementsAnnotatedWith(BaseModel::class.java), mapWrapperStrings)
var baseModels = validateAndCreateBaseModelMap(roundEnv.getElementsAnnotatedWith(BaseModel::class.java), mapWrapperStrings, generatedInterfaces)

validateAndProcess(roundEnv.getElementsAnnotatedWith(Entity::class.java), object : EntityProcessor {
override fun process(element: Element): FileSpec? {
Expand Down Expand Up @@ -114,13 +114,14 @@ class CoachBaseBinderProcessor : AbstractProcessor() {
return true // no further processing of this annotation type
}

private fun validateAndCreateBaseModelMap(elements: Collection<Element>, wrapperString: List<String>) : Map<String, BaseModelHolder>{
private fun validateAndCreateBaseModelMap(elements: Collection<Element>, wrapperString: List<String>, generatedInterfaces: MutableSet<String>) : Map<String, BaseModelHolder>{
val result = HashMap<String, BaseModelHolder>()
for (elem in elements) {
validator.preValidate(elem, mLogger)
if (!mLogger.hasErrors()) {
val baseModel = EntityFactory.createBaseModelHolder(elem, wrapperString)
if(postValidate(baseModel)){
generateInterface(generatedInterfaces, baseModel)
result["${baseModel.`package`}.${baseModel.sourceClazzSimpleName}"] = baseModel
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ class EntityGeneration {
.addFunction(toMap(holder, useSuspend))
.addFunction(BuilderClassGeneration.generateBuilderFun())

for (baseModelHolder in holder.basedOn) {
typeBuilder.addSuperinterface(baseModelHolder.interfaceTypeName)
}

if(holder.modifierOpen){
typeBuilder.addModifiers(KModifier.OPEN)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ class WrapperGeneration {
.superclass(holder.sourceElement!!.asType().asTypeName())
.addFunction(BuilderClassGeneration.generateBuilderFun())

for (baseModelHolder in holder.basedOn) {
typeBuilder.addSuperinterface(baseModelHolder.interfaceTypeName)
}

if(holder.modifierOpen){
typeBuilder.addModifiers(KModifier.OPEN)
}

for (fieldHolder in holder.allFields) {

companionSpec.addProperties(fieldHolder.createFieldConstant())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ object EntityFactory {
}

fun createChildEntityHolder(cblEntityElement: Element, allWrappers: List<String>, allBaseModels: Map<String, BaseModelHolder>): WrapperEntityHolder {

return create(cblEntityElement, WrapperEntityHolder(), allWrappers, allBaseModels) as WrapperEntityHolder
val annotation = cblEntityElement.getAnnotation(MapWrapper::class.java)
return create(cblEntityElement, WrapperEntityHolder(annotation.modifierOpen), allWrappers, allBaseModels) as WrapperEntityHolder
}

private fun create(cblEntityElement: Element, content: BaseEntityHolder, allWrappers: List<String>, allBaseModels: Map<String, BaseModelHolder>): BaseEntityHolder {
Expand All @@ -44,6 +44,7 @@ object EntityFactory {

basedOnValue?.forEach { type ->
allBaseModels[type.toString()]?.let {
content.basedOn.add(it)
content.fieldConstants.putAll(it.fieldConstants)
content.fields.putAll(it.fields)
content.generateAccessors.addAll(it.generateAccessors)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ abstract class BaseEntityHolder {

val generateAccessors : MutableList<CblGenerateAccessorHolder> = ArrayList()

val basedOn : MutableList<BaseModelHolder> = ArrayList()

val allFields: List<CblBaseFieldHolder>
get() {
val allField = ArrayList<CblBaseFieldHolder>()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
package com.kaufland.model.entity

class BaseModelHolder() : BaseEntityHolder()


Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.kaufland.model.entity

class WrapperEntityHolder : BaseEntityHolder() {
class WrapperEntityHolder(val modifierOpen: Boolean) : BaseEntityHolder() {

override val entitySimpleName: String
get() = sourceClazzSimpleName + "Wrapper"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import kaufland.com.coachbasebinderapi.MapWrapper
import schwarz.fwws.shared.model.DWG
import schwarz.fwws.shared.model.Model

@MapWrapper
@MapWrapper(modifierOpen = true)
@Fields(
Field(name = "country", type = String::class),
Field(name = "type", type = String::class, defaultValue = BaseArticle.TYPE, readonly = true),
Expand Down

0 comments on commit 497e740

Please sign in to comment.