[workspace model codegen] Add check that extension field has a type of another entity

We don't support extension fields for other types of fields except for entities

GitOrigin-RevId: 41fd290a91fb34c851103d928fb2bf5b7ab80178
This commit is contained in:
Mikhail Mazurkevich
2023-03-23 11:58:32 +00:00
committed by intellij-monorepo-bot
parent 1fce86f8bb
commit c200315703
3 changed files with 21 additions and 5 deletions
@@ -6,15 +6,14 @@ import com.intellij.workspaceModel.codegen.engine.CodeGenerator
import com.intellij.workspaceModel.codegen.engine.GeneratedCode
import com.intellij.workspaceModel.codegen.engine.GenerationProblem
import com.intellij.workspaceModel.codegen.engine.GenerationResult
import com.intellij.workspaceModel.codegen.writer.generateBuilderCode
import com.intellij.workspaceModel.codegen.writer.generateCompanionObject
import com.intellij.workspaceModel.codegen.writer.generateExtensionCode
import com.intellij.workspaceModel.codegen.writer.implWsCode
import com.intellij.workspaceModel.codegen.writer.*
class CodeGeneratorImpl : CodeGenerator {
override fun generate(module: CompiledObjModule): GenerationResult {
val problems = ArrayList<GenerationProblem>()
val reporter = ProblemReporter { problems.add(it) }
checkExtensionFields(module, reporter)
val objClassToBuilderInterface = module.types.associateWith {
val builderInterface = it.generateBuilderCode(reporter)
builderInterface
@@ -0,0 +1,16 @@
// Copyright 2000-2023 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.intellij.workspaceModel.codegen.writer
import com.intellij.workspaceModel.codegen.deft.meta.CompiledObjModule
import com.intellij.workspaceModel.codegen.engine.GenerationProblem
import com.intellij.workspaceModel.codegen.engine.ProblemLocation
import com.intellij.workspaceModel.codegen.engine.impl.ProblemReporter
internal fun checkExtensionFields(module: CompiledObjModule, reporter: ProblemReporter) {
module.extensions.forEach { extProperty ->
if (!extProperty.valueType.isRefType()) {
reporter.reportProblem(GenerationProblem("Extension property is supposed to be a reference to another entity only.",
GenerationProblem.Level.ERROR, ProblemLocation.Property(extProperty)))
}
}
}
@@ -3,6 +3,7 @@ package com.intellij.workspaceModel.codegen.writer
import com.intellij.workspaceModel.codegen.deft.meta.ObjClass
import com.intellij.workspaceModel.codegen.deft.meta.OwnProperty
import com.intellij.workspaceModel.codegen.deft.meta.ValueType
import com.intellij.workspaceModel.codegen.writer.fields.javaType
import com.intellij.workspaceModel.storage.url.VirtualFileUrl
val ObjClass<*>.refsFields: List<OwnProperty<*, *>>
@@ -18,7 +19,7 @@ fun ValueType<*>.getRefType(): ValueType.ObjRef<*> = when (this) {
is ValueType.ObjRef<*> -> this
is ValueType.Optional<*> -> type.getRefType()
is ValueType.Collection<*, *> -> elementType.getRefType()
else -> error("Unsupported type of requester, should be called only if `isRefType` is true")
else -> error("${this.javaType} isn't a reference type. The method has to be called only for the reference type of fields")
}
fun ValueType<*>.isRefType(): Boolean = when (this) {