Skip to content

Commit

Permalink
Avoid useless setField calls when value is default (#2443)
Browse files Browse the repository at this point in the history
* Avoid useless setField calls when value is default

* In case of @InjectMock set field values, even if they are defaults

---------

Co-authored-by: IlyaMuravjov <[email protected]>
  • Loading branch information
EgorkaKulikov and IlyaMuravjov authored Jul 25, 2023
1 parent b67debe commit 33cf8ad
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,7 @@ open class CgMethodConstructor(val context: CgContext) : CgContextOwner by conte
} else {
this.resultModel = resultModel
val expected = variableConstructor.getOrCreateVariable(resultModel, "expected")
emptyLineIfNeeded()
assertEquality(expected, actual)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ import org.utbot.framework.plugin.api.UtPrimitiveModel
import org.utbot.framework.plugin.api.UtReferenceModel
import org.utbot.framework.plugin.api.UtStatementCallModel
import org.utbot.framework.plugin.api.UtVoidModel
import org.utbot.framework.plugin.api.util.booleanClassId
import org.utbot.framework.plugin.api.util.booleanWrapperClassId
import org.utbot.framework.plugin.api.util.classClassId
import org.utbot.framework.plugin.api.util.defaultValueModel
import org.utbot.framework.plugin.api.util.jField
Expand All @@ -51,6 +53,8 @@ import org.utbot.framework.plugin.api.util.isArray
import org.utbot.framework.plugin.api.util.isEnum
import org.utbot.framework.plugin.api.util.isPrimitiveWrapperOrString
import org.utbot.framework.plugin.api.util.isStatic
import org.utbot.framework.plugin.api.util.primitiveWrappers
import org.utbot.framework.plugin.api.util.primitives
import org.utbot.framework.plugin.api.util.stringClassId
import org.utbot.framework.plugin.api.util.supertypeOfAnonymousClass
import org.utbot.framework.plugin.api.util.wrapperByPrimitive
Expand Down Expand Up @@ -171,12 +175,13 @@ open class CgVariableConstructor(val context: CgContext) :

for ((fieldId, fieldModel) in model.fields) {
val variableForField = getOrCreateVariable(fieldModel)
setFieldValue(obj, fieldId, variableForField)
if (!variableForField.hasDefaultValue())
setFieldValue(obj, fieldId, variableForField)
}
return obj
}

fun setFieldValue(obj: CgValue, fieldId: FieldId, variableForField: CgValue){
fun setFieldValue(obj: CgValue, fieldId: FieldId, valueForField: CgValue) {
val field = fieldId.jField
val fieldFromVariableSpecifiedType = obj.type.findFieldByIdOrNull(fieldId)

Expand All @@ -187,14 +192,27 @@ open class CgVariableConstructor(val context: CgContext) :
// branchRegisterRequest.byteBuffer = heapByteBuffer;
// byteBuffer is field of type ByteBuffer and upper line is incorrect
val canFieldBeDirectlySetByVariableAndFieldTypeRestrictions =
fieldFromVariableSpecifiedType != null && fieldFromVariableSpecifiedType.type.id == variableForField.type
fieldFromVariableSpecifiedType != null && fieldFromVariableSpecifiedType.type.id == valueForField.type
if (canFieldBeDirectlySetByVariableAndFieldTypeRestrictions && fieldId.canBeSetFrom(context, obj.type)) {
// TODO: check if it is correct to use declaringClass of a field here
val fieldAccess = if (field.isStatic) CgStaticFieldAccess(fieldId) else CgFieldAccess(obj, fieldId)
fieldAccess `=` variableForField
fieldAccess `=` valueForField
} else {
// composite models must not have info about static fields, hence only non-static fields are set here
+utilsClassId[setField](obj, fieldId.declaringClass.name, fieldId.name, variableForField)
+utilsClassId[setField](obj, fieldId.declaringClass.name, fieldId.name, valueForField)
}
}

private fun CgValue.hasDefaultValue(): Boolean {
if (this !is CgLiteral) {
return false;
}

return when {
this.value == null -> true
(this.type == booleanClassId || this.type == booleanWrapperClassId) && this.value == false -> true
(this.type in primitives || this.type in primitiveWrappers) && this.value == 0 -> true
else -> false
}
}

Expand Down

0 comments on commit 33cf8ad

Please sign in to comment.