Skip to content

Commit 38fb0d0

Browse files
committed
Minor changes fixing issues found in review
1 parent 569e581 commit 38fb0d0

File tree

2 files changed

+36
-23
lines changed

2 files changed

+36
-23
lines changed

utbot-framework/src/main/kotlin/org/utbot/framework/codegen/model/constructor/tree/CgVariableConstructor.kt

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ import org.utbot.framework.codegen.model.tree.CgValue
2929
import org.utbot.framework.codegen.model.tree.CgVariable
3030
import org.utbot.framework.codegen.model.util.at
3131
import org.utbot.framework.codegen.model.util.canBeSetFrom
32-
import org.utbot.framework.codegen.model.util.fieldThisIsGetterFor
33-
import org.utbot.framework.codegen.model.util.fieldThisIsSetterFor
32+
import org.utbot.framework.codegen.model.util.fieldThatIsGotWith
33+
import org.utbot.framework.codegen.model.util.fieldThatIsSetWith
3434
import org.utbot.framework.codegen.model.util.inc
3535
import org.utbot.framework.codegen.model.util.isAccessibleFrom
3636
import org.utbot.framework.codegen.model.util.lessThan
@@ -218,11 +218,11 @@ internal class CgVariableConstructor(val context: CgContext) :
218218
}
219219
is UtExecutableCallModel -> {
220220
val call = createCgExecutableCallFromUtExecutableCall(statementModel)
221-
val callOrAccess: CgStatement = replaceCgExecutableCallWithFieldAccessIfNeeded(call)
222-
if (callOrAccess is CgExecutableCall)
223-
+callOrAccess // smart-cast => CgExecutableCall.unaryPlus()
221+
val equivalentFieldAccess = replaceCgExecutableCallWithFieldAccessIfNeeded(call)
222+
if (equivalentFieldAccess != null)
223+
+equivalentFieldAccess
224224
else
225-
+callOrAccess // CgStatement.unaryPlus()
225+
+call
226226
}
227227
}
228228
}
@@ -246,7 +246,6 @@ internal class CgVariableConstructor(val context: CgContext) :
246246
val initExpr = if (isPrimitiveWrapperOrString(type)) {
247247
cgLiteralForWrapper(params)
248248
} else {
249-
// TODO: if instantiation chain could be a setter call, we need to replace it in Kotlin
250249
createCgExecutableCallFromUtExecutableCall(executableCall)
251250
}
252251
newVar(type, model, baseName) {
@@ -272,21 +271,35 @@ internal class CgVariableConstructor(val context: CgContext) :
272271
return cgCall
273272
}
274273

275-
private fun replaceCgExecutableCallWithFieldAccessIfNeeded(call: CgExecutableCall): CgStatement {
276-
if (call !is CgMethodCall || context.codegenLanguage != CodegenLanguage.KOTLIN)
277-
return call
274+
/**
275+
* If executable is getter/setter that should be syntactically replaced with field access
276+
* (e.g., getter/setter generated by Kotlin in Kotlin code), this method returns [CgStatement]
277+
* with which [call] should be replaced.
278+
*
279+
* Otherwise, returns null.
280+
*/
281+
private fun replaceCgExecutableCallWithFieldAccessIfNeeded(call: CgExecutableCall): CgStatement? {
282+
when (context.codegenLanguage) {
283+
CodegenLanguage.JAVA -> return null
284+
CodegenLanguage.KOTLIN -> {
285+
if (call !is CgMethodCall)
286+
return null
278287

279-
val caller = call.caller ?: return call
288+
val caller = call.caller ?: return null
280289

281-
caller.type.fieldThisIsSetterFor(call.executableId)?.let {
282-
return CgAssignment(caller[it], call.arguments.single())
283-
}
284-
caller.type.fieldThisIsGetterFor(call.executableId)?.let {
285-
require(call.arguments.isEmpty())
286-
return caller[it]
287-
}
290+
caller.type.fieldThatIsSetWith(call.executableId)?.let {
291+
return CgAssignment(caller[it], call.arguments.single())
292+
}
293+
caller.type.fieldThatIsGotWith(call.executableId)?.let {
294+
require(call.arguments.isEmpty()) {
295+
"Method $call was detected as getter for $it, but its arguments list isn't empty"
296+
}
297+
return caller[it]
298+
}
288299

289-
return call
300+
return null
301+
}
302+
}
290303
}
291304

292305
/**

utbot-framework/src/main/kotlin/org/utbot/framework/codegen/model/util/ClassIdUtil.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,11 @@ infix fun ClassId.isAccessibleFrom(packageName: String): Boolean {
3636
/**
3737
* Returns field of [this], such that [methodId] is a getter for it (or null if methodId doesn't represent a getter)
3838
*/
39-
internal fun ClassId.fieldThisIsGetterFor(methodId: MethodId): FieldId? =
40-
allDeclaredFieldIds.firstOrNull { !it.isStatic && it.getter == methodId }
39+
internal fun ClassId.fieldThatIsGotWith(methodId: MethodId): FieldId? =
40+
allDeclaredFieldIds.singleOrNull { !it.isStatic && it.getter == methodId }
4141

4242
/**
4343
* Returns field of [this], such that [methodId] is a setter for it (or null if methodId doesn't represent a setter)
4444
*/
45-
internal fun ClassId.fieldThisIsSetterFor(methodId: MethodId): FieldId? =
46-
allDeclaredFieldIds.firstOrNull { !it.isStatic && it.setter == methodId }
45+
internal fun ClassId.fieldThatIsSetWith(methodId: MethodId): FieldId? =
46+
allDeclaredFieldIds.singleOrNull { !it.isStatic && it.setter == methodId }

0 commit comments

Comments
 (0)