Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit33cf8ad

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 parentb67debe commit33cf8ad

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
importorg.utbot.framework.plugin.api.UtReferenceModel
4242
importorg.utbot.framework.plugin.api.UtStatementCallModel
4343
importorg.utbot.framework.plugin.api.UtVoidModel
44+
importorg.utbot.framework.plugin.api.util.booleanClassId
45+
importorg.utbot.framework.plugin.api.util.booleanWrapperClassId
4446
importorg.utbot.framework.plugin.api.util.classClassId
4547
importorg.utbot.framework.plugin.api.util.defaultValueModel
4648
importorg.utbot.framework.plugin.api.util.jField
@@ -51,6 +53,8 @@ import org.utbot.framework.plugin.api.util.isArray
5153
importorg.utbot.framework.plugin.api.util.isEnum
5254
importorg.utbot.framework.plugin.api.util.isPrimitiveWrapperOrString
5355
importorg.utbot.framework.plugin.api.util.isStatic
56+
importorg.utbot.framework.plugin.api.util.primitiveWrappers
57+
importorg.utbot.framework.plugin.api.util.primitives
5458
importorg.utbot.framework.plugin.api.util.stringClassId
5559
importorg.utbot.framework.plugin.api.util.supertypeOfAnonymousClass
5660
importorg.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-
funsetFieldValue(obj:CgValue,fieldId:FieldId,variableForField:CgValue){
184+
funsetFieldValue(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)elseCgFieldAccess(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+
privatefun CgValue.hasDefaultValue():Boolean {
207+
if (this!isCgLiteral) {
208+
returnfalse;
209+
}
210+
211+
returnwhen {
212+
this.value==null->true
213+
(this.type== booleanClassId||this.type== booleanWrapperClassId)&&this.value==false->true
214+
(this.typein primitives||this.typein primitiveWrappers)&&this.value==0->true
215+
else->false
198216
}
199217
}
200218

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp