Skip to content

Commit 33cf8ad

Browse files
Avoid useless setField calls when value is default (#2443)
* Avoid useless setField calls when value is default * In case of @InjectMock set field values, even if they are defaults --------- Co-authored-by: IlyaMuravjov <muravjovilya@gmail.com>
1 parent b67debe commit 33cf8ad

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,7 @@ open class CgMethodConstructor(val context: CgContext) : CgContextOwner by conte
323323
} else {
324324
this.resultModel = resultModel
325325
val expected = variableConstructor.getOrCreateVariable(resultModel, "expected")
326+
emptyLineIfNeeded()
326327
assertEquality(expected, actual)
327328
}
328329
}

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

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ import org.utbot.framework.plugin.api.UtPrimitiveModel
4141
import org.utbot.framework.plugin.api.UtReferenceModel
4242
import org.utbot.framework.plugin.api.UtStatementCallModel
4343
import org.utbot.framework.plugin.api.UtVoidModel
44+
import org.utbot.framework.plugin.api.util.booleanClassId
45+
import org.utbot.framework.plugin.api.util.booleanWrapperClassId
4446
import org.utbot.framework.plugin.api.util.classClassId
4547
import org.utbot.framework.plugin.api.util.defaultValueModel
4648
import org.utbot.framework.plugin.api.util.jField
@@ -51,6 +53,8 @@ import org.utbot.framework.plugin.api.util.isArray
5153
import org.utbot.framework.plugin.api.util.isEnum
5254
import org.utbot.framework.plugin.api.util.isPrimitiveWrapperOrString
5355
import org.utbot.framework.plugin.api.util.isStatic
56+
import org.utbot.framework.plugin.api.util.primitiveWrappers
57+
import org.utbot.framework.plugin.api.util.primitives
5458
import org.utbot.framework.plugin.api.util.stringClassId
5559
import org.utbot.framework.plugin.api.util.supertypeOfAnonymousClass
5660
import org.utbot.framework.plugin.api.util.wrapperByPrimitive
@@ -171,12 +175,13 @@ open class CgVariableConstructor(val context: CgContext) :
171175

172176
for ((fieldId, fieldModel) in model.fields) {
173177
val variableForField = getOrCreateVariable(fieldModel)
174-
setFieldValue(obj, fieldId, variableForField)
178+
if (!variableForField.hasDefaultValue())
179+
setFieldValue(obj, fieldId, variableForField)
175180
}
176181
return obj
177182
}
178183

179-
fun setFieldValue(obj: CgValue, fieldId: FieldId, variableForField: CgValue){
184+
fun setFieldValue(obj: CgValue, fieldId: FieldId, valueForField: CgValue) {
180185
val field = fieldId.jField
181186
val fieldFromVariableSpecifiedType = obj.type.findFieldByIdOrNull(fieldId)
182187

@@ -187,14 +192,27 @@ open class CgVariableConstructor(val context: CgContext) :
187192
// branchRegisterRequest.byteBuffer = heapByteBuffer;
188193
// byteBuffer is field of type ByteBuffer and upper line is incorrect
189194
val canFieldBeDirectlySetByVariableAndFieldTypeRestrictions =
190-
fieldFromVariableSpecifiedType != null && fieldFromVariableSpecifiedType.type.id == variableForField.type
195+
fieldFromVariableSpecifiedType != null && fieldFromVariableSpecifiedType.type.id == valueForField.type
191196
if (canFieldBeDirectlySetByVariableAndFieldTypeRestrictions && fieldId.canBeSetFrom(context, obj.type)) {
192197
// TODO: check if it is correct to use declaringClass of a field here
193198
val fieldAccess = if (field.isStatic) CgStaticFieldAccess(fieldId) else CgFieldAccess(obj, fieldId)
194-
fieldAccess `=` variableForField
199+
fieldAccess `=` valueForField
195200
} else {
196201
// composite models must not have info about static fields, hence only non-static fields are set here
197-
+utilsClassId[setField](obj, fieldId.declaringClass.name, fieldId.name, variableForField)
202+
+utilsClassId[setField](obj, fieldId.declaringClass.name, fieldId.name, valueForField)
203+
}
204+
}
205+
206+
private fun CgValue.hasDefaultValue(): Boolean {
207+
if (this !is CgLiteral) {
208+
return false;
209+
}
210+
211+
return when {
212+
this.value == null -> true
213+
(this.type == booleanClassId || this.type == booleanWrapperClassId) && this.value == false -> true
214+
(this.type in primitives || this.type in primitiveWrappers) && this.value == 0 -> true
215+
else -> false
198216
}
199217
}
200218

0 commit comments

Comments
 (0)