Skip to content

Improve UtBotBeanFactoryPostProcessor to get more bean definitions #2119

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

Closed
wants to merge 3 commits into from
Closed
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
Expand Up @@ -61,7 +61,6 @@ import org.utbot.framework.plugin.api.TypeReplacementMode.*
import org.utbot.framework.plugin.api.util.allDeclaredFieldIds
import org.utbot.framework.plugin.api.util.fieldId
import org.utbot.framework.plugin.api.util.isSubtypeOf
import org.utbot.framework.plugin.api.util.objectClassId
import org.utbot.framework.plugin.api.util.utContext
import org.utbot.framework.process.OpenModulesContainer
import soot.SootField
Expand Down Expand Up @@ -1276,7 +1275,7 @@ class SpringApplicationContext(
private val springInjectedClasses: Set<ClassId>
get() {
if (!areInjectedClassesInitialized) {
springInjectedClassesStorage += beanQualifiedNames
_springInjectedClasses += beanQualifiedNames
.map { fqn -> utContext.classLoader.loadClass(fqn) }
.filterNot { it.isAbstract || it.isInterface || it.isLocalClass || it.isMemberClass && !it.isStatic }
.mapTo(mutableSetOf()) { it.id }
Expand All @@ -1286,15 +1285,15 @@ class SpringApplicationContext(
areInjectedClassesInitialized = true
}

return springInjectedClassesStorage
return _springInjectedClasses
}

// This is a service field to model the lazy behavior of [springInjectedClasses].
// Do not call it outside the getter.
//
// Actually, we should just call [springInjectedClasses] with `by lazy`, but we had problems
// with a strange `kotlin.UNINITIALIZED_VALUE` in `speculativelyCannotProduceNullPointerException` method call.
private val springInjectedClassesStorage = mutableSetOf<ClassId>()
private val _springInjectedClasses = mutableSetOf<ClassId>()

override val typeReplacementMode: TypeReplacementMode =
if (shouldUseImplementors) KnownImplementor else NoImplementors
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.utbot.spring.postProcessors

import org.springframework.beans.factory.BeanCreationException
import org.springframework.beans.factory.annotation.AnnotatedBeanDefinition
import org.springframework.beans.factory.config.BeanFactoryPostProcessor
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory
Expand Down Expand Up @@ -28,26 +29,18 @@ object UtBotBeanFactoryPostProcessor : BeanFactoryPostProcessor, PriorityOrdered
println("Finished post-processing bean factory in UtBot")
}

private fun findBeanClassNames(beanFactory: ConfigurableListableBeanFactory): ArrayList<String> {
val beanClassNames = ArrayList<String>()
for (beanDefinitionName in beanFactory.beanDefinitionNames) {
val beanDefinition = beanFactory.getBeanDefinition(beanDefinitionName)

if (beanDefinition is AnnotatedBeanDefinition) {
val factoryMethodMetadata = beanDefinition.factoryMethodMetadata
if (factoryMethodMetadata != null) {
beanClassNames.add(factoryMethodMetadata.returnTypeName)
}
} else {
var className = beanDefinition.beanClassName
if (className == null) {
className = beanFactory.getBean(beanDefinitionName).javaClass.name
}
className?.let { beanClassNames.add(it) }
private fun findBeanClassNames(beanFactory: ConfigurableListableBeanFactory): List<String> {
return beanFactory.beanDefinitionNames.mapNotNull {
try {
beanFactory.getBeanDefinition(it).beanClassName
//TODO: avoid getting bean here, change the interface to find method and return type
// and obtain required information from PsiClass in UtBot after that.
?: beanFactory.getBean(it).javaClass.name
} catch (e: BeanCreationException) {
//logger.warn { "Failed to get bean: $it" }
null
}
}

return beanClassNames
}.filterNot { it.startsWith("org.utbot.spring") }
}

private fun destroyBeanDefinitions(beanFactory: ConfigurableListableBeanFactory) {
Expand Down