Skip to content

Commit 8701582

Browse files
committed
Some pre-merge refactoring
1 parent 1204474 commit 8701582

File tree

10 files changed

+93
-74
lines changed

10 files changed

+93
-74
lines changed

utbot-spring-analyzer/src/main/kotlin/ApplicationRunner.kt

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
package application
22

33
import analyzers.SpringApplicationAnalyzer
4+
import utils.PathsUtils
45

6+
/**
7+
* To run this app, arguments must be passed in the following way:
8+
* args[0] - classpath of current project
9+
* args[1] - fully qualified name of configuration class
10+
* args[2] - `.properties` file paths, separated via `;`, empty string if no files exist
11+
* args[3] - `.xml` configuration file paths
12+
*
13+
* Several items in one arg are separated via `;`.
14+
* If there are no files, empty string should be passed.
15+
*/
516
fun main(args: Array<String>) {
6-
/*
7-
arg2 contains .properties files, arg3 contains .xml files.
8-
If there are several files, they are transmitted via ";".
9-
If there are no files, then an empty string "" is passed
10-
*/
1117

1218
/* FOR EXAMPLE
1319
val arg0 = "/Users/kirillshishin/IdeaProjects/spring-starter-lesson-28/build/classes/java/main"
@@ -19,8 +25,8 @@ fun main(args: Array<String>) {
1925
val springApplicationAnalyzer = SpringApplicationAnalyzer(
2026
applicationPath = args[0],
2127
configurationClassFqn = args[1],
22-
propertyFilesPaths = args[2].split(";"),
23-
xmlConfigurationPaths = args[3].split(";"),
28+
propertyFilesPaths = args[2].split(";").filter { it != PathsUtils.EMPTY_PATH },
29+
xmlConfigurationPaths = args[3].split(";").filter { it != PathsUtils.EMPTY_PATH },
2430
)
2531

2632
springApplicationAnalyzer.analyze()

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class SpringApplicationAnalyzer(
3131

3232
val configurationManager = ConfigurationManager(classLoader, userConfigurationClass)
3333
val propertiesConfigurator = PropertiesConfigurator(propertyFilesPaths, configurationManager)
34-
val xmlFilesConfigurator = XmlFilesConfigurator(xmlConfigurationPaths, configurationManager, fakeFileManager)
34+
val xmlFilesConfigurator = XmlFilesConfigurator(xmlConfigurationPaths, configurationManager)
3535

3636
propertiesConfigurator.configure()
3737
xmlFilesConfigurator.configure()

utbot-spring-analyzer/src/main/kotlin/configurators/PropertiesConfigurator.kt

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package application.configurators
22

33
import utils.ConfigurationManager
4-
import utils.Paths
4+
import utils.PathsUtils
55
import java.io.BufferedReader
66
import java.io.FileReader
77
import kotlin.io.path.Path
@@ -11,21 +11,19 @@ class PropertiesConfigurator(
1111
private val configurationManager: ConfigurationManager
1212
) {
1313

14-
fun configure(){
14+
fun configure() {
1515
configurationManager.clearPropertySourceAnnotation()
1616

17-
for(propertiesFilePath in propertiesFilesPaths) {
18-
if(propertiesFilePath == Paths.EMPTY_PATH)continue
19-
20-
configurationManager.patchPropertySourceAnnotation(Path(propertiesFilePath).fileName)
21-
}
17+
propertiesFilesPaths
18+
.map { Path(it).fileName }
19+
.forEach { fileName -> configurationManager.patchPropertySourceAnnotation(fileName) }
2220
}
2321

2422
fun readProperties(): ArrayList<String> {
2523
val props = ArrayList<String>()
2624

27-
for(propertiesFilePath in propertiesFilesPaths) {
28-
if(propertiesFilePath == Paths.EMPTY_PATH)continue
25+
for (propertiesFilePath in propertiesFilesPaths) {
26+
if (propertiesFilePath == PathsUtils.EMPTY_PATH) continue
2927

3028
val reader = BufferedReader(FileReader(propertiesFilePath))
3129
var line = reader.readLine()

utbot-spring-analyzer/src/main/kotlin/configurators/XmlFilesConfigurator.kt

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,22 @@ package application.configurators
33
import analyzers.XmlConfigurationAnalyzer
44
import application.utils.FakeFileManager
55
import utils.ConfigurationManager
6-
import utils.Paths
6+
import utils.PathsUtils
77
import kotlin.io.path.Path
88

99
class XmlFilesConfigurator(
1010
private val userXmlFilePaths: List<String>,
1111
private val configurationManager: ConfigurationManager,
12-
private val fakeFileManager: FakeFileManager
1312
) {
1413

1514
fun configure() {
1615
configurationManager.clearImportResourceAnnotation()
1716

1817
for (userXmlFilePath in userXmlFilePaths) {
19-
if(userXmlFilePath == Paths.EMPTY_PATH)continue
18+
if(userXmlFilePath == PathsUtils.EMPTY_PATH)continue
2019

2120
val xmlConfigurationAnalyzer =
22-
XmlConfigurationAnalyzer(userXmlFilePath, fakeFileManager.getFakeFilePath(userXmlFilePath))
21+
XmlConfigurationAnalyzer(userXmlFilePath, PathsUtils.createFakeFilePath(userXmlFilePath))
2322

2423
xmlConfigurationAnalyzer.fillFakeApplicationXml()
2524
configurationManager.patchImportResourceAnnotation(Path(userXmlFilePath).fileName)

utbot-spring-analyzer/src/main/kotlin/post_processors/UtBotBeanFactoryPostProcessor.kt

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package post_processors
22

3-
import org.springframework.beans.BeansException
43
import org.springframework.beans.factory.annotation.AnnotatedBeanDefinition
54
import org.springframework.beans.factory.config.BeanFactoryPostProcessor
65
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory
@@ -13,9 +12,26 @@ import java.util.Arrays
1312

1413
class UtBotBeanFactoryPostProcessor : BeanFactoryPostProcessor, PriorityOrdered {
1514

15+
/**
16+
* Sets the priority of post processor to highest to avoid side effects from others.
17+
*/
18+
override fun getOrder(): Int = PriorityOrdered.HIGHEST_PRECEDENCE
19+
1620
override fun postProcessBeanFactory(beanFactory: ConfigurableListableBeanFactory) {
1721
println("Started post-processing bean factory in UtBot")
1822

23+
val beanClassNames = findBeanClassNames(beanFactory)
24+
//TODO: will be replaced with more appropriate IPC approach.
25+
writeToFile(beanClassNames)
26+
27+
// After desired post-processing is completed we destroy bean definitions
28+
// to avoid further possible actions with beans that may be unsafe.
29+
destroyBeanDefinitions(beanFactory)
30+
31+
println("Finished post-processing bean factory in UtBot")
32+
}
33+
34+
private fun findBeanClassNames(beanFactory: ConfigurableListableBeanFactory): ArrayList<String> {
1935
val beanClassNames = ArrayList<String>()
2036
for (beanDefinitionName in beanFactory.beanDefinitionNames) {
2137
val beanDefinition = beanFactory.getBeanDefinition(beanDefinitionName)
@@ -33,12 +49,15 @@ class UtBotBeanFactoryPostProcessor : BeanFactoryPostProcessor, PriorityOrdered
3349
className?.let { beanClassNames.add(it) }
3450
}
3551
}
52+
53+
return beanClassNames
54+
}
55+
56+
private fun destroyBeanDefinitions(beanFactory: ConfigurableListableBeanFactory) {
3657
for (beanDefinitionName in beanFactory.beanDefinitionNames) {
3758
val beanRegistry = beanFactory as BeanDefinitionRegistry
3859
beanRegistry.removeBeanDefinition(beanDefinitionName)
3960
}
40-
41-
writeToFile(beanClassNames)
4261
}
4362

4463
private fun writeToFile(beanClassNames: ArrayList<String>) {
@@ -58,14 +77,10 @@ class UtBotBeanFactoryPostProcessor : BeanFactoryPostProcessor, PriorityOrdered
5877

5978
fileWriter.flush()
6079
fileWriter.close()
80+
81+
println("Storing bean information completed successfully")
6182
} catch (e: Throwable) {
62-
println("Storing bean information failed")
63-
} finally {
64-
println("Finished post-processing bean factory in UtBot")
83+
println("Storing bean information failed with exception $e")
6584
}
6685
}
67-
68-
override fun getOrder(): Int {
69-
return PriorityOrdered.HIGHEST_PRECEDENCE
70-
}
7186
}

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

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,32 +9,15 @@ import kotlin.reflect.KClass
99

1010
class ConfigurationManager(private val classLoader: ClassLoader, private val userConfigurationClass: Class<*>) {
1111

12-
fun clearPropertySourceAnnotation(){
13-
patchAnnotation(
14-
PropertySource::class, null
15-
)
16-
}
12+
fun clearPropertySourceAnnotation() = patchAnnotation(PropertySource::class, null)
1713

18-
fun clearImportResourceAnnotation(){
19-
patchAnnotation(
20-
ImportResource::class, null
21-
)
22-
}
14+
fun clearImportResourceAnnotation() = patchAnnotation(ImportResource::class, null)
2315

2416
fun patchPropertySourceAnnotation(userPropertiesFileName: Path) =
25-
patchAnnotation(
26-
PropertySource::class, String.format(
27-
"classpath:%s", "fake_$userPropertiesFileName"
28-
)
29-
)
17+
patchAnnotation(PropertySource::class, String.format("classpath:%s", "fake_$userPropertiesFileName"))
3018

3119
fun patchImportResourceAnnotation(userXmlFilePath: Path) =
32-
patchAnnotation(
33-
ImportResource::class,
34-
String.format(
35-
"classpath:%s", "fake_$userXmlFilePath"
36-
)
37-
)
20+
patchAnnotation(ImportResource::class, String.format("classpath:%s", "fake_$userXmlFilePath"))
3821

3922
private fun patchAnnotation(annotationClass: KClass<*>, newValue: String?) {
4023
val proxyClass = classLoader.loadClass("java.lang.reflect.Proxy")
Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,34 @@
11
package application.utils
22

3-
import utils.Paths
3+
import utils.PathsUtils
44
import java.io.File
5-
import kotlin.io.path.Path
5+
import java.io.IOException
66

77
class FakeFileManager(private val fakeFilesList: List<String>) {
88

9-
private val buildResourcesPath =
10-
Path(this.javaClass.classLoader.getResource(Paths.GAG_FILE)!!.path).parent.toString()
11-
129
fun createFakeFiles() {
1310
for (fileName in fakeFilesList) {
14-
if(fileName == Paths.EMPTY_FILENAME)continue
15-
val fakeXmlFileAbsolutePath = getFakeFilePath(fileName)
16-
File(fakeXmlFileAbsolutePath).createNewFile()
11+
val fakeXmlFileAbsolutePath = PathsUtils.createFakeFilePath(fileName)
12+
13+
try {
14+
File(fakeXmlFileAbsolutePath).createNewFile()
15+
} catch (e: IOException) {
16+
println("Fake xml file creation failed with exception $e")
17+
}
18+
1719
}
1820
}
1921

2022
fun deleteFakeFiles() {
2123
for (fileName in fakeFilesList) {
22-
if(fileName == Paths.EMPTY_FILENAME)continue
23-
val fakeXmlFileAbsolutePath = getFakeFilePath(fileName)
24-
File(fakeXmlFileAbsolutePath).delete()
25-
}
26-
}
24+
val fakeXmlFileAbsolutePath = PathsUtils.createFakeFilePath(fileName)
25+
26+
try {
27+
File(fakeXmlFileAbsolutePath).delete()
28+
} catch (e: IOException) {
29+
println("Fake xml file deletion failed with exception $e")
30+
}
2731

28-
fun getFakeFilePath(fileName: String): String {
29-
return Path(buildResourcesPath, "fake_${Path(fileName).fileName}").toString()
32+
}
3033
}
3134
}

utbot-spring-analyzer/src/main/kotlin/utils/Paths.kt

Lines changed: 0 additions & 7 deletions
This file was deleted.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package utils
2+
3+
import kotlin.io.path.Path
4+
5+
object PathsUtils {
6+
const val EMPTY_PATH = ""
7+
8+
fun createFakeFilePath(fileName: String): String =
9+
Path(buildResourcesPath, "fake_${Path(fileName).fileName}").toString()
10+
11+
//TODO: it is better to do it without marker files
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(resourcesMarker.path).parent.toString()
19+
}
20+
21+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
DO NOT DELETE THIS FILE!!!

0 commit comments

Comments
 (0)