Skip to content

Commit 6bcdb30

Browse files
Use getters and setters in java code generation #2534 (#2537)
1 parent a3f8d90 commit 6bcdb30

File tree

3 files changed

+14
-2
lines changed

3 files changed

+14
-2
lines changed

utbot-framework/src/main/kotlin/org/utbot/framework/codegen/tree/CgMethodConstructor.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,11 @@ import org.utbot.framework.codegen.tree.CgComponents.getStatementConstructorBy
6464
import org.utbot.framework.codegen.tree.CgComponents.getTestFrameworkManagerBy
6565
import org.utbot.framework.codegen.tree.CgComponents.getVariableConstructorBy
6666
import org.utbot.framework.codegen.util.canBeReadFrom
67+
import org.utbot.framework.codegen.util.canBeReadViaGetterFrom
6768
import org.utbot.framework.codegen.util.canBeSetFrom
6869
import org.utbot.framework.codegen.util.equalTo
6970
import org.utbot.framework.codegen.util.escapeControlChars
71+
import org.utbot.framework.codegen.util.getter
7072
import org.utbot.framework.codegen.util.inc
7173
import org.utbot.framework.codegen.util.length
7274
import org.utbot.framework.codegen.util.lessThan
@@ -1154,6 +1156,10 @@ open class CgMethodConstructor(val context: CgContext) : CgContextOwner by conte
11541156
// and is accessible from current package
11551157
if (variable.type.hasField(this) && canBeReadFrom(context, variable.type)) {
11561158
if (jField.isStatic) CgStaticFieldAccess(this) else CgFieldAccess(variable, this)
1159+
} else if (context.codegenLanguage == CodegenLanguage.JAVA &&
1160+
!jField.isStatic && canBeReadViaGetterFrom(context)
1161+
) {
1162+
CgMethodCall(variable, getter, emptyList())
11571163
} else {
11581164
utilsClassId[getFieldValue](variable, this.declaringClass.name, this.name)
11591165
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,15 @@ import org.utbot.framework.codegen.tree.CgComponents.getNameGeneratorBy
1414
import org.utbot.framework.codegen.tree.CgComponents.getStatementConstructorBy
1515
import org.utbot.framework.codegen.util.at
1616
import org.utbot.framework.codegen.util.canBeSetFrom
17+
import org.utbot.framework.codegen.util.canBeSetViaSetterFrom
1718
import org.utbot.framework.codegen.util.fieldThatIsGotWith
1819
import org.utbot.framework.codegen.util.fieldThatIsSetWith
1920
import org.utbot.framework.codegen.util.inc
2021
import org.utbot.framework.codegen.util.isAccessibleFrom
2122
import org.utbot.framework.codegen.util.lessThan
2223
import org.utbot.framework.codegen.util.nullLiteral
2324
import org.utbot.framework.codegen.util.resolve
25+
import org.utbot.framework.codegen.util.setter
2426
import org.utbot.framework.plugin.api.BuiltinClassId
2527
import org.utbot.framework.plugin.api.ClassId
2628
import org.utbot.framework.plugin.api.FieldId
@@ -209,6 +211,10 @@ open class CgVariableConstructor(val context: CgContext) :
209211
// TODO: check if it is correct to use declaringClass of a field here
210212
val fieldAccess = if (field.isStatic) CgStaticFieldAccess(fieldId) else CgFieldAccess(obj, fieldId)
211213
fieldAccess `=` valueForField
214+
} else if (context.codegenLanguage == CodegenLanguage.JAVA &&
215+
!field.isStatic && fieldId.canBeSetViaSetterFrom(context)
216+
) {
217+
+CgMethodCall(obj, fieldId.setter, listOf(valueForField))
212218
} else {
213219
// composite models must not have info about static fields, hence only non-static fields are set here
214220
+utilsClassId[setField](obj, fieldId.declaringClass.name, fieldId.name, valueForField)

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ fun FieldId.isAccessibleFrom(packageName: String, callerClassId: ClassId): Boole
3636
return isClassAccessible && isAccessibleFromPackageByModifiers
3737
}
3838

39-
private fun FieldId.canBeReadViaGetterFrom(context: CgContext): Boolean =
39+
internal fun FieldId.canBeReadViaGetterFrom(context: CgContext): Boolean =
4040
declaringClass.allMethods.contains(getter) && getter.isAccessibleFrom(context.testClassPackageName)
4141

4242
/**
@@ -52,7 +52,7 @@ internal fun FieldId.canBeReadFrom(context: CgContext, callerClassId: ClassId):
5252
return isAccessibleFrom(context.testClassPackageName, callerClassId)
5353
}
5454

55-
private fun FieldId.canBeSetViaSetterFrom(context: CgContext): Boolean =
55+
internal fun FieldId.canBeSetViaSetterFrom(context: CgContext): Boolean =
5656
declaringClass.allMethods.contains(setter) && setter.isAccessibleFrom(context.testClassPackageName)
5757

5858
/**

0 commit comments

Comments
 (0)