Skip to content

Commit 868301b

Browse files
authored
Spring analyzer improvements (fix uncaught exception, add shadowJar task, fix dynamic resources not being found on the classpath) (#1984)
1 parent d796369 commit 868301b

File tree

7 files changed

+37
-31
lines changed

7 files changed

+37
-31
lines changed

utbot-spring-analyzer/build.gradle.kts

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
1+
import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar
2+
import com.github.jengelman.gradle.plugins.shadow.transformers.PropertiesFileTransformer
3+
14
plugins {
25
id("org.springframework.boot") version "2.7.8"
36
id("io.spring.dependency-management") version "1.1.0"
7+
id("com.github.johnrengelman.shadow") version "7.1.2"
48
id("java")
9+
application
510
}
611

712
java {
@@ -12,4 +17,22 @@ java {
1217
dependencies {
1318
implementation("org.springframework.boot:spring-boot-starter")
1419
implementation("org.springframework.boot:spring-boot-starter-web")
15-
}
20+
}
21+
22+
application {
23+
mainClass.set("org.utbot.spring.ApplicationRunnerKt")
24+
}
25+
26+
// see more details about this task -- https://github.com/spring-projects/spring-boot/issues/1828
27+
tasks.withType(ShadowJar::class.java) {
28+
isZip64 = true
29+
// Required for Spring
30+
mergeServiceFiles()
31+
append("META-INF/spring.handlers")
32+
append("META-INF/spring.schemas")
33+
append("META-INF/spring.tooling")
34+
transform(PropertiesFileTransformer().apply {
35+
paths = listOf("META-INF/spring.factories")
36+
mergeStrategy = "append"
37+
})
38+
}

utbot-spring-analyzer/src/main/kotlin/org/utbot/spring/analyzers/SpringApplicationAnalyzer.kt

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import org.utbot.spring.configurators.PropertiesConfigurator
55
import org.utbot.spring.configurators.XmlFilesConfigurator
66
import org.utbot.spring.config.TestApplicationConfiguration
77
import org.springframework.boot.builder.SpringApplicationBuilder
8-
import org.utbot.spring.postProcessors.UtBotSpringShutdownException
8+
import org.springframework.context.ApplicationContextException
99
import org.utbot.spring.utils.ConfigurationManager
1010
import java.net.URL
1111
import java.net.URLClassLoader
@@ -41,10 +41,13 @@ class SpringApplicationAnalyzer(
4141
try {
4242
app.build()
4343
app.run()
44-
} catch (e: UtBotSpringShutdownException) {
44+
} catch (e: ApplicationContextException) {
45+
// UtBotBeanFactoryPostProcessor destroys bean definitions
46+
// to prevent Spring application from actually starting and
47+
// that causes it to throw ApplicationContextException
4548
println("Bean analysis finished successfully")
46-
}finally {
49+
} finally {
4750
fakeFileManager.deleteTempFiles()
4851
}
4952
}
50-
}
53+
}

utbot-spring-analyzer/src/main/kotlin/org/utbot/spring/postProcessors/UtBotBeanFactoryPostProcessor.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ class UtBotBeanFactoryPostProcessor : BeanFactoryPostProcessor, PriorityOrdered
2929
destroyBeanDefinitions(beanFactory)
3030

3131
println("Finished post-processing bean factory in UtBot")
32-
throw UtBotSpringShutdownException("Finished post-processing bean factory in UtBot")
3332
}
3433

3534
private fun findBeanClassNames(beanFactory: ConfigurableListableBeanFactory): ArrayList<String> {
@@ -84,4 +83,4 @@ class UtBotBeanFactoryPostProcessor : BeanFactoryPostProcessor, PriorityOrdered
8483
println("Storing bean information failed with exception $e")
8584
}
8685
}
87-
}
86+
}

utbot-spring-analyzer/src/main/kotlin/org/utbot/spring/postProcessors/UtBotSpringShutdownException.kt

Lines changed: 0 additions & 7 deletions
This file was deleted.

utbot-spring-analyzer/src/main/kotlin/org/utbot/spring/utils/ConfigurationManager.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ class ConfigurationManager(private val classLoader: ClassLoader, private val use
1414
fun clearImportResourceAnnotation() = patchAnnotation(ImportResource::class, null)
1515

1616
fun patchPropertySourceAnnotation(userPropertiesFileName: Path) =
17-
patchAnnotation(PropertySource::class, String.format("classpath:%s", "fake_$userPropertiesFileName"))
17+
patchAnnotation(PropertySource::class, String.format("file:%s", "fake_$userPropertiesFileName"))
1818

1919
fun patchImportResourceAnnotation(userXmlFilePath: Path) =
20-
patchAnnotation(ImportResource::class, String.format("classpath:%s", "fake_$userXmlFilePath"))
20+
patchAnnotation(ImportResource::class, String.format("file:%s", "fake_$userXmlFilePath"))
2121

2222
private fun patchAnnotation(annotationClass: KClass<*>, newValue: String?) {
2323
val proxyClass = classLoader.loadClass("java.lang.reflect.Proxy")
@@ -53,4 +53,4 @@ class ConfigurationManager(private val classLoader: ClassLoader, private val use
5353
memberValues["value"] = list.toTypedArray()
5454
}
5555
}
56-
}
56+
}

utbot-spring-analyzer/src/main/kotlin/org/utbot/spring/utils/PathsUtils.kt

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,5 @@ import kotlin.io.path.Path
66
object PathsUtils {
77
const val EMPTY_PATH = ""
88

9-
fun createFakeFilePath(fileName: String): String =
10-
Path(buildResourcesPath, "fake_${Path(fileName).fileName}").toString()
11-
12-
private val buildResourcesPath: String
13-
get() {
14-
val resourcesMarker =
15-
this.javaClass.classLoader.getResource("resources_marker.txt")
16-
?: error("Resources marker file is not found")
17-
18-
return Path(File(resourcesMarker.toURI()).path).parent.toString()
19-
}
20-
21-
}
9+
fun createFakeFilePath(fileName: String): String = "fake_${Path(fileName).fileName}"
10+
}

utbot-spring-analyzer/src/main/resources/resources_marker.txt

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)