Skip to content

Allowed local classes in type storages #1632

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package org.utbot.examples.objects

import org.junit.jupiter.api.Test
import org.utbot.testcheckers.eq
import org.utbot.testing.UtValueTestCaseChecker

class LocalClassExampleTest : UtValueTestCaseChecker(testClass = LocalClassExample::class) {
@Test
fun testLocalClassFieldExample() {
check(
LocalClassExample::localClassFieldExample,
eq(1),
{ y, r -> r == y + 42 }
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -178,10 +178,10 @@ val SootClass.isAppropriate
get() = !isInappropriate

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

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import org.utbot.engine.isArtificialEntity
import org.utbot.engine.isInappropriate
import org.utbot.engine.isJavaLangObject
import org.utbot.engine.isLambda
import org.utbot.engine.isLocal
import org.utbot.engine.isOverridden
import org.utbot.engine.isUtMock
import org.utbot.engine.makeArrayType
Expand Down Expand Up @@ -136,15 +135,15 @@ class TypeResolver(private val typeRegistry: TypeRegistry, private val hierarchy
fun constructTypeStorage(type: Type, possibleTypes: Collection<Type>): TypeStorage {
val concretePossibleTypes = possibleTypes
.map { (if (it is ArrayType) it.baseType else it) to it.numDimensions }
.filterNot { (baseType, numDimensions) -> isInappropriateOrArrayOfMocksOrLocals(numDimensions, baseType) }
.filterNot { (baseType, numDimensions) -> isInappropriateOrArrayOfMocks(numDimensions, baseType) }
.mapTo(mutableSetOf()) { (baseType, numDimensions) ->
if (numDimensions == 0) baseType else baseType.makeArrayType(numDimensions)
}

return TypeStorage.constructTypeStorageUnsafe(type, concretePossibleTypes).removeInappropriateTypes()
}

private fun isInappropriateOrArrayOfMocksOrLocals(numDimensions: Int, baseType: Type?): Boolean {
private fun isInappropriateOrArrayOfMocks(numDimensions: Int, baseType: Type?): Boolean {
if (baseType !is RefType) {
return false
}
Expand All @@ -158,12 +157,12 @@ class TypeResolver(private val typeRegistry: TypeRegistry, private val hierarchy
}

if (numDimensions == 0 && baseSootClass.isInappropriate) {
// interface, abstract class, or local, or mock could not be constructed
// interface, abstract class, or mock could not be constructed
return true
}

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

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.utbot.examples.objects;

public class LocalClassExample {
int localClassFieldExample(int y) {
class LocalClass {
final int x;

public LocalClass(int x) {
this.x = x;
}
}

LocalClass localClass = new LocalClass(42);

return localClass.x + y;
}
}