Skip to content

Commit e3c319e

Browse files
CaelmBleidddenis-fokin
authored andcommitted
Remove access for jClass in UtMockFieldInfo (#1525)
(cherry picked from commit 4c642c5)
1 parent 4eb9a87 commit e3c319e

File tree

1 file changed

+34
-8
lines changed
  • utbot-framework/src/main/kotlin/org/utbot/engine

1 file changed

+34
-8
lines changed

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

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -188,12 +188,28 @@ class Mocker(
188188
if (isOverriddenClass(type)) return false // never mock overriden classes
189189
if (type.isInaccessibleViaReflection) return false // never mock classes that we can't process with reflection
190190
if (isMakeSymbolic(mockInfo)) return true // support for makeSymbolic
191-
if (type.sootClass.isArtificialEntity) return false // never mock artificial types, i.e. Maps$lambda_computeValue_1__7
192-
if (!isEngineClass(type) && (type.sootClass.isInnerClass || type.sootClass.isLocal || type.sootClass.isAnonymous)) return false // there is no reason (and maybe no possibility) to mock such classes
193-
if (!isEngineClass(type) && type.sootClass.isPrivate) return false // could not mock private classes (even if it is in mock always list)
191+
192+
val sootClass = type.sootClass
193+
194+
if (sootClass.isArtificialEntity) return false // never mock artificial types, i.e. Maps$lambda_computeValue_1__7
195+
196+
if (!isEngineClass(type)) {
197+
// there is no reason (and maybe no possibility) to mock such classes
198+
if (sootClass.isInnerClass || sootClass.isLocal || sootClass.isAnonymous) {
199+
return false
200+
}
201+
202+
// could not mock private classes (even if it is in mock always list)
203+
if (sootClass.isPrivate) {
204+
return false
205+
}
206+
}
207+
194208
if (mockAlways(type)) return true // always mock randoms and loggers
209+
195210
if (mockInfo is UtFieldMockInfo) {
196-
val declaringClass = mockInfo.fieldId.declaringClass
211+
val fieldId = mockInfo.fieldId
212+
val declaringClass = fieldId.declaringClass
197213
val sootDeclaringClass = Scene.v().getSootClass(declaringClass.name)
198214

199215
if (sootDeclaringClass.isArtificialEntity || sootDeclaringClass.isOverridden) {
@@ -203,12 +219,22 @@ class Mocker(
203219
return false
204220
}
205221

206-
return when {
207-
declaringClass.packageName.startsWith("java.lang") -> false
208-
!mockInfo.fieldId.type.isRefType -> false // mocks are allowed for ref fields only
209-
else -> return strategy.eligibleToMock(mockInfo.fieldId.type, classUnderTest) // if we have a field with Integer type, we should not mock it
222+
val sootField = sootDeclaringClass
223+
.fields
224+
.firstOrNull { it.name == fieldId.name && it.declaringClass.name == sootDeclaringClass.name }
225+
?: error("Unexpected $fieldId is provided into shouldMock function")
226+
227+
val sootFieldType = sootField.type
228+
229+
if (sootFieldType !is RefType) {
230+
return false
210231
}
232+
233+
return strategy.eligibleToMock(sootFieldType.id, classUnderTest)
211234
}
235+
236+
// Note that eligibleToMock can use information retrieved from jClass
237+
// Therefore, such classes should be already processed at this point
212238
return strategy.eligibleToMock(type.id, classUnderTest) // strategy to decide
213239
}
214240

0 commit comments

Comments
 (0)