Skip to content

Fix speculativelyCannotProduceNullPointerException in Spring projects #2402

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 4 commits into from
Jul 12, 2023
Merged
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
Expand Up @@ -61,6 +61,7 @@ import org.utbot.framework.isFromTrustedLibrary
import org.utbot.framework.plugin.api.TypeReplacementMode.*
import org.utbot.framework.plugin.api.util.SpringModelUtils
import org.utbot.framework.plugin.api.util.allDeclaredFieldIds
import org.utbot.framework.plugin.api.util.allSuperTypes
import org.utbot.framework.plugin.api.util.fieldId
import org.utbot.framework.plugin.api.util.isSubtypeOf
import org.utbot.framework.plugin.api.util.utContext
Expand Down Expand Up @@ -1492,6 +1493,10 @@ class SpringApplicationContext(
return springInjectedClassesStorage
}

private val allInjectedTypes: Set<ClassId> by lazy {
springInjectedClasses.flatMap { it.allSuperTypes() }.toSet()
}

// This is a service field to model the lazy behavior of [springInjectedClasses].
// Do not call it outside the getter.
//
Expand Down Expand Up @@ -1525,7 +1530,7 @@ class SpringApplicationContext(
override fun speculativelyCannotProduceNullPointerException(
field: SootField,
classUnderTest: ClassId,
): Boolean = field.fieldId in classUnderTest.allDeclaredFieldIds && field.declaringClass.id !in springInjectedClasses
): Boolean = field.fieldId in classUnderTest.allDeclaredFieldIds && field.type.classId !in allInjectedTypes

override fun preventsFurtherTestGeneration(): Boolean =
super.preventsFurtherTestGeneration() || springContextLoadingResult?.contextLoaded == false
Expand Down