Skip to content

Commit 630c765

Browse files
authored
Allowed local classes in type storages (#1632)
1 parent cf68dcb commit 630c765

File tree

4 files changed

+40
-8
lines changed

4 files changed

+40
-8
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package org.utbot.examples.objects
2+
3+
import org.junit.jupiter.api.Test
4+
import org.utbot.testcheckers.eq
5+
import org.utbot.testing.UtValueTestCaseChecker
6+
7+
class LocalClassExampleTest : UtValueTestCaseChecker(testClass = LocalClassExample::class) {
8+
@Test
9+
fun testLocalClassFieldExample() {
10+
check(
11+
LocalClassExample::localClassFieldExample,
12+
eq(1),
13+
{ y, r -> r == y + 42 }
14+
)
15+
}
16+
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,10 +178,10 @@ val SootClass.isAppropriate
178178
get() = !isInappropriate
179179

180180
/**
181-
* Returns true if the class is abstract, interface, local or if the class has UtClassMock annotation, false otherwise.
181+
* Returns true if the class is abstract, interface, or if the class has UtClassMock annotation, false otherwise.
182182
*/
183183
val SootClass.isInappropriate
184-
get() = isAbstract || isInterface || isLocal || findMockAnnotationOrNull != null
184+
get() = isAbstract || isInterface || findMockAnnotationOrNull != null
185185

186186
private val isLocalRegex = ".*\\$\\d+[\\p{L}\\p{M}0-9][\\p{L}\\p{M}0-9]*".toRegex()
187187

utbot-framework/src/main/kotlin/org/utbot/engine/types/TypeResolver.kt

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import org.utbot.engine.isArtificialEntity
1717
import org.utbot.engine.isInappropriate
1818
import org.utbot.engine.isJavaLangObject
1919
import org.utbot.engine.isLambda
20-
import org.utbot.engine.isLocal
2120
import org.utbot.engine.isOverridden
2221
import org.utbot.engine.isUtMock
2322
import org.utbot.engine.makeArrayType
@@ -136,15 +135,15 @@ class TypeResolver(private val typeRegistry: TypeRegistry, private val hierarchy
136135
fun constructTypeStorage(type: Type, possibleTypes: Collection<Type>): TypeStorage {
137136
val concretePossibleTypes = possibleTypes
138137
.map { (if (it is ArrayType) it.baseType else it) to it.numDimensions }
139-
.filterNot { (baseType, numDimensions) -> isInappropriateOrArrayOfMocksOrLocals(numDimensions, baseType) }
138+
.filterNot { (baseType, numDimensions) -> isInappropriateOrArrayOfMocks(numDimensions, baseType) }
140139
.mapTo(mutableSetOf()) { (baseType, numDimensions) ->
141140
if (numDimensions == 0) baseType else baseType.makeArrayType(numDimensions)
142141
}
143142

144143
return TypeStorage.constructTypeStorageUnsafe(type, concretePossibleTypes).removeInappropriateTypes()
145144
}
146145

147-
private fun isInappropriateOrArrayOfMocksOrLocals(numDimensions: Int, baseType: Type?): Boolean {
146+
private fun isInappropriateOrArrayOfMocks(numDimensions: Int, baseType: Type?): Boolean {
148147
if (baseType !is RefType) {
149148
return false
150149
}
@@ -158,12 +157,12 @@ class TypeResolver(private val typeRegistry: TypeRegistry, private val hierarchy
158157
}
159158

160159
if (numDimensions == 0 && baseSootClass.isInappropriate) {
161-
// interface, abstract class, or local, or mock could not be constructed
160+
// interface, abstract class, or mock could not be constructed
162161
return true
163162
}
164163

165-
if (numDimensions > 0 && (baseSootClass.isLocal || baseSootClass.findMockAnnotationOrNull != null)) {
166-
// array of mocks or locals could not be constructed, but array of interfaces or abstract classes could be
164+
if (numDimensions > 0 && baseSootClass.findMockAnnotationOrNull != null) {
165+
// array of mocks could not be constructed, but array of interfaces or abstract classes could be
167166
return true
168167
}
169168

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package org.utbot.examples.objects;
2+
3+
public class LocalClassExample {
4+
int localClassFieldExample(int y) {
5+
class LocalClass {
6+
final int x;
7+
8+
public LocalClass(int x) {
9+
this.x = x;
10+
}
11+
}
12+
13+
LocalClass localClass = new LocalClass(42);
14+
15+
return localClass.x + y;
16+
}
17+
}

0 commit comments

Comments
 (0)