@@ -29,8 +29,8 @@ import org.utbot.framework.codegen.model.tree.CgValue
2929import org.utbot.framework.codegen.model.tree.CgVariable
3030import org.utbot.framework.codegen.model.util.at
3131import 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
3434import org.utbot.framework.codegen.model.util.inc
3535import org.utbot.framework.codegen.model.util.isAccessibleFrom
3636import 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 /* *
0 commit comments