Skip to content

Commit f080f49

Browse files
committed
Codestyle & minor changes
1 parent fb6f19b commit f080f49

File tree

12 files changed

+34
-34
lines changed

12 files changed

+34
-34
lines changed

utbot-core/src/main/kotlin/org/utbot/common/KClassUtil.kt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,12 @@ import java.lang.reflect.Method
77

88
val Class<*>.packageName: String get() = `package`?.name?:""
99

10-
fun Class<*>.findDirectAccessedField(fieldName: String): Field? = generateSequence(this) { it.superclass }
10+
/**
11+
* Returns the Field that would be used by compiler when you try to direct access [fieldName] from code, i.e.
12+
* when you write `${this.name}.$fieldName`.
13+
* If there is no field named [fieldName] in the class, null is returned
14+
*/
15+
fun Class<*>.findDirectAccessedFieldOrNull(fieldName: String): Field? = generateSequence(this) { it.superclass }
1116
.flatMap { it.declaredFields.asSequence() }
1217
.firstOrNull { it.name == fieldName }
1318

utbot-framework-api/src/main/kotlin/org/utbot/framework/plugin/api/Api.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import org.utbot.framework.plugin.api.util.charClassId
2020
import org.utbot.framework.plugin.api.util.constructor
2121
import org.utbot.framework.plugin.api.util.doubleClassId
2222
import org.utbot.framework.plugin.api.util.executableId
23-
import org.utbot.framework.plugin.api.util.fieldOrNull
23+
import org.utbot.framework.plugin.api.util.fieldByIdOrNull
2424
import org.utbot.framework.plugin.api.util.floatClassId
2525
import org.utbot.framework.plugin.api.util.id
2626
import org.utbot.framework.plugin.api.util.intClassId
@@ -856,7 +856,7 @@ open class FieldId(val declaringClass: ClassId, val name: String) {
856856
return result
857857
}
858858

859-
override fun toString() = declaringClass.fieldOrNull(this).toString()
859+
override fun toString() = declaringClass.fieldByIdOrNull(this).toString()
860860
}
861861

862862
inline fun <T> withReflection(block: () -> T): T {

utbot-framework-api/src/main/kotlin/org/utbot/framework/plugin/api/util/IdUtil.kt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -285,17 +285,17 @@ val ClassId.isMap: Boolean
285285
val ClassId.isIterableOrMap: Boolean
286286
get() = isIterable || isMap
287287

288-
fun ClassId.fieldOrNull(fieldId: FieldId): Field? {
289-
if (this.isNotSubtypeOf(fieldId.declaringClass))
290-
return null
291-
return try {
292-
fieldId.field
293-
} catch (e: IllegalStateException) {
288+
fun ClassId.fieldById(fieldId: FieldId): Field =
289+
fieldByIdOrNull(fieldId) ?: error("Can't find field ${fieldId.name} in $name")
290+
291+
fun ClassId.fieldByIdOrNull(fieldId: FieldId): Field? =
292+
if (isNotSubtypeOf(fieldId.declaringClass)) {
294293
null
294+
} else {
295+
fieldId.declaringClass.jClass.declaredFields.firstOrNull { it.name == fieldId.name }
295296
}
296-
}
297297

298-
fun ClassId.hasField(fieldId: FieldId): Boolean = fieldOrNull(fieldId) != null
298+
fun ClassId.hasField(fieldId: FieldId): Boolean = fieldByIdOrNull(fieldId) != null
299299

300300
fun ClassId.defaultValueModel(): UtModel = when (this) {
301301
intClassId -> UtPrimitiveModel(0)

utbot-framework/src/main/kotlin/org/utbot/engine/Hierarchy.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,12 @@ class Hierarchy(private val typeRegistry: TypeRegistry) {
2828
type as? RefType ?: error("$type is not a refType")
2929

3030
val realType = typeRegistry.findRealType(type) as RefType
31-
val realFieldDeclaringClass = typeRegistry.findRealType(field.declaringClass.type) as RefType
31+
val realFieldDeclaringType = typeRegistry.findRealType(field.declaringClass.type) as RefType
3232

33-
if (realFieldDeclaringClass.sootClass !in ancestors(realType.sootClass.id))
33+
if (realFieldDeclaringType.sootClass !in ancestors(realType.sootClass.id)) {
3434
error("No such field ${field.subSignature} found in ${realType.sootClass.name}")
35-
return ChunkId("$realFieldDeclaringClass", field.name)
35+
}
36+
return ChunkId("$realFieldDeclaringType", field.name)
3637
}
3738

3839
/**

utbot-framework/src/main/kotlin/org/utbot/engine/Traverser.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ import org.utbot.framework.plugin.api.MethodId
8989
import org.utbot.framework.plugin.api.UtMethod
9090
import org.utbot.framework.plugin.api.classId
9191
import org.utbot.framework.plugin.api.id
92-
import org.utbot.framework.plugin.api.util.field
92+
import org.utbot.framework.plugin.api.util.fieldById
9393
import org.utbot.framework.plugin.api.util.jClass
9494
import org.utbot.framework.plugin.api.util.id
9595
import org.utbot.framework.plugin.api.util.signature
@@ -639,7 +639,7 @@ class Traverser(
639639
SECURITY_FIELD_SIGNATURE -> SecurityManager()
640640
FIELD_FILTER_MAP_FIELD_SIGNATURE -> mapOf(Reflection::class to arrayOf("fieldFilterMap", "methodFilterMap"))
641641
METHOD_FILTER_MAP_FIELD_SIGNATURE -> emptyMap<Class<*>, Array<String>>()
642-
else -> field.fieldId.field.let { it.withAccessibility { it.get(null) } }
642+
else -> declaringClass.id.fieldById(field.fieldId).let { it.withAccessibility { it.get(null) } }
643643
}
644644

645645
private fun isStaticInstanceInMethodResult(id: ClassId, methodResult: MethodResult?) =

utbot-framework/src/main/kotlin/org/utbot/engine/ValueConstructor.kt

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ import org.utbot.framework.plugin.api.UtVoidModel
3636
import org.utbot.framework.plugin.api.isMockModel
3737
import org.utbot.framework.plugin.api.util.constructor
3838
import org.utbot.framework.plugin.api.util.field
39-
import org.utbot.framework.plugin.api.util.fieldOrNull
39+
import org.utbot.framework.plugin.api.util.fieldById
4040
import org.utbot.framework.plugin.api.util.jClass
4141
import org.utbot.framework.plugin.api.util.method
4242
import org.utbot.framework.plugin.api.util.utContext
@@ -213,8 +213,7 @@ class ValueConstructor {
213213
constructedObjects[model] = classInstance
214214

215215
model.fields.forEach { (field, fieldModel) ->
216-
val declaredField =
217-
model.classId.fieldOrNull(field) ?: error("Can't find field: $field for $javaClass")
216+
val declaredField = model.classId.fieldById(field)
218217
val accessible = declaredField.isAccessible
219218

220219
try {
@@ -361,7 +360,6 @@ class ValueConstructor {
361360
val fieldModel = directSetterModel.fieldModel
362361

363362
val field = directSetterModel.fieldId.field
364-
// val field = instance::class.java.findField(directSetterModel.fieldId.name)
365363
val isAccessible = field.isAccessible
366364

367365
try {

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ import org.utbot.framework.codegen.model.tree.CgEqualTo
4343
import org.utbot.framework.codegen.model.tree.CgErrorTestMethod
4444
import org.utbot.framework.codegen.model.tree.CgExecutableCall
4545
import org.utbot.framework.codegen.model.tree.CgExpression
46-
import org.utbot.framework.codegen.model.tree.CgFieldAccess
4746
import org.utbot.framework.codegen.model.tree.CgGetJavaClass
4847
import org.utbot.framework.codegen.model.tree.CgIfStatement
4948
import org.utbot.framework.codegen.model.tree.CgLiteral

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,8 @@ import org.utbot.framework.codegen.model.tree.CgAllocateInitializedArray
1515
import org.utbot.framework.codegen.model.tree.CgDeclaration
1616
import org.utbot.framework.codegen.model.tree.CgEnumConstantAccess
1717
import org.utbot.framework.codegen.model.tree.CgExpression
18-
import org.utbot.framework.codegen.model.tree.CgFieldAccess
1918
import org.utbot.framework.codegen.model.tree.CgGetJavaClass
2019
import org.utbot.framework.codegen.model.tree.CgLiteral
21-
import org.utbot.framework.codegen.model.tree.CgNotNullAssertion
2220
import org.utbot.framework.codegen.model.tree.CgStaticFieldAccess
2321
import org.utbot.framework.codegen.model.tree.CgValue
2422
import org.utbot.framework.codegen.model.tree.CgVariable
@@ -47,7 +45,7 @@ import org.utbot.framework.plugin.api.UtPrimitiveModel
4745
import org.utbot.framework.plugin.api.UtReferenceModel
4846
import org.utbot.framework.plugin.api.UtVoidModel
4947
import org.utbot.framework.plugin.api.util.defaultValueModel
50-
import org.utbot.framework.plugin.api.util.fieldOrNull
48+
import org.utbot.framework.plugin.api.util.fieldByIdOrNull
5149
import org.utbot.framework.plugin.api.util.id
5250
import org.utbot.framework.plugin.api.util.intClassId
5351
import org.utbot.framework.plugin.api.util.isArray
@@ -128,7 +126,7 @@ internal class CgVariableConstructor(val context: CgContext) :
128126

129127
for ((fieldId, fieldModel) in model.fields) {
130128
val variableForField = getOrCreateVariable(fieldModel)
131-
val field = obj.type.fieldOrNull(fieldId)
129+
val field = obj.type.fieldByIdOrNull(fieldId)
132130

133131
// we cannot set field directly if variable declared type does not have such field
134132
// or we cannot directly create variable for field with the specified type (it is private, for example)

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package org.utbot.framework.codegen.model.util
22

3-
import org.utbot.common.findDirectAccessedField
3+
import org.utbot.common.findDirectAccessedFieldOrNull
44
import org.utbot.framework.codegen.model.constructor.tree.CgCallableAccessManager
55
import org.utbot.framework.codegen.model.tree.CgArrayElementAccess
66
import org.utbot.framework.codegen.model.tree.CgDecrement
@@ -76,10 +76,11 @@ fun stringLiteral(string: String) = CgLiteral(stringClassId, string)
7676

7777
// non-static fields
7878
operator fun CgExpression.get(fieldId: FieldId): CgFieldAccess =
79-
if (type.jClass.findDirectAccessedField(fieldId.name) != fieldId.field)
79+
if (type.jClass.findDirectAccessedFieldOrNull(fieldId.name) != fieldId.field) {
8080
CgFieldAccess(CgTypeCast(fieldId.declaringClass, this), fieldId)
81-
else
81+
} else {
8282
CgFieldAccess(this, fieldId)
83+
}
8384

8485
// static fields
8586
// TODO: unused receiver

utbot-framework/src/main/kotlin/org/utbot/framework/concrete/MockValueConstructor.kt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ import org.utbot.framework.plugin.api.isMockModel
3232
import org.utbot.framework.plugin.api.util.constructor
3333
import org.utbot.framework.plugin.api.util.executableId
3434
import org.utbot.framework.plugin.api.util.field
35-
import org.utbot.framework.plugin.api.util.fieldOrNull
3635
import org.utbot.framework.plugin.api.util.jClass
3736
import org.utbot.framework.plugin.api.util.method
3837
import org.utbot.framework.plugin.api.util.utContext
@@ -46,6 +45,7 @@ import org.mockito.Mockito
4645
import org.mockito.stubbing.Answer
4746
import org.objectweb.asm.Type
4847
import org.utbot.common.withAccessibility
48+
import org.utbot.framework.plugin.api.util.fieldById
4949

5050
/**
5151
* Constructs values (including mocks) from models.
@@ -170,8 +170,7 @@ class MockValueConstructor(
170170
}
171171

172172
model.fields.forEach { (field, fieldModel) ->
173-
val declaredField =
174-
model.classId.fieldOrNull(field) ?: error("Can't find field: $field for $javaClass")
173+
val declaredField = model.classId.fieldById(field)
175174
val accessible = declaredField.isAccessible
176175
declaredField.isAccessible = true
177176

utbot-framework/src/test/kotlin/org/utbot/examples/UtModelTestCaseChecker.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,8 +165,8 @@ internal abstract class UtModelTestCaseChecker(
165165
/**
166166
* Finds field model in [UtCompositeModel] and [UtAssembleModel]. For assemble model supports direct field access only.
167167
*/
168-
protected fun UtModel.findField(fieldName: String, declarationClass: ClassId = this.classId): UtModel =
169-
findField(FieldId(declarationClass, fieldName))
168+
protected fun UtModel.findField(fieldName: String, declaringClass: ClassId = this.classId): UtModel =
169+
findField(FieldId(declaringClass, fieldName))
170170

171171
/**
172172
* Finds field model in [UtCompositeModel] and [UtAssembleModel]. For assemble model supports direct field access only.

utbot-framework/src/test/kotlin/org/utbot/examples/enums/ClassWithEnumTest.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import org.utbot.framework.plugin.api.FieldId
1212
import org.utbot.framework.plugin.api.util.id
1313
import org.junit.jupiter.api.Disabled
1414
import org.junit.jupiter.api.Test
15-
import org.utbot.framework.plugin.api.ClassId
1615
import org.utbot.framework.plugin.api.util.field
1716

1817
class ClassWithEnumTest : UtValueTestCaseChecker(testClass = ClassWithEnum::class) {

0 commit comments

Comments
 (0)