Skip to content

Commit b3db25f

Browse files
tepa46EgorkaKulikov
authored andcommitted
Added processing of beans from xml file
1 parent 42f2370 commit b3db25f

File tree

3 files changed

+86
-7
lines changed

3 files changed

+86
-7
lines changed

utbot-spring-analyzer/src/main/java/analyzer/ConfigurationManager.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package analyzer;
22

3+
import org.springframework.context.annotation.ImportResource;
34
import org.springframework.context.annotation.PropertySource;
45

56
import java.lang.annotation.Annotation;
@@ -19,16 +20,15 @@ public ConfigurationManager(ClassLoader classLoader, Class userConfigurationClas
1920
this.userConfigurationClass = userConfigurationClass;
2021
}
2122

22-
public void patchPropertySourceAnnotation() throws Exception {
23-
23+
private void patchAnnotation(Class<?> type, String path) throws Exception {
2424
Class<?> proxyClass = classLoader.loadClass("java.lang.reflect.Proxy");
2525
Field hField = proxyClass.getDeclaredField("h");
2626
hField.setAccessible(true);
2727

2828
//Annotation annotationProxy = userConfigurationClass.getAnnotations()[2];
2929
Optional<Annotation> propertySourceAnnotation =
3030
Arrays.stream(userConfigurationClass.getAnnotations())
31-
.filter(el -> el.annotationType() == PropertySource.class)
31+
.filter(el -> el.annotationType() == type)
3232
.findFirst();
3333
if (propertySourceAnnotation.isPresent()) {
3434
InvocationHandler annotationInvocationHandler = (InvocationHandler) (hField.get(propertySourceAnnotation.get()));
@@ -39,7 +39,15 @@ public void patchPropertySourceAnnotation() throws Exception {
3939
memberValuesField.setAccessible(true);
4040

4141
Map<String, Object> memberValues = (Map<String, Object>) (memberValuesField.get(annotationInvocationHandler));
42-
memberValues.put("value", "classpath:fakeapplication.properties");
42+
memberValues.put("value", path);
4343
}
4444
}
45+
46+
public void patchPropertySourceAnnotation() throws Exception {
47+
patchAnnotation(PropertySource.class, "classpath:fakeapplication.properties");
48+
}
49+
50+
public void patchImportResourceAnnotation() throws Exception {
51+
patchAnnotation(ImportResource.class, "classpath:fakeapplication.xml");
52+
}
4553
}

utbot-spring-analyzer/src/main/java/analyzer/SpringAnalysisRunner.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import org.springframework.boot.builder.SpringApplicationBuilder;
55
import org.springframework.context.ApplicationContextException;
66

7-
import java.io.IOException;
87
import java.net.URL;
98
import java.net.URLClassLoader;
109
import java.nio.file.Path;
@@ -13,17 +12,23 @@
1312
public class SpringAnalysisRunner {
1413

1514
public static void main(String[] args) throws Exception {
16-
//String arg0 = "D:\\Projects\\spring-starter-lesson-28\\build\\classes\\java\\main";
15+
//String arg0 = "/Users/kirillshishin/IdeaProjects/spring-starter-lesson-28/build/classes/java/main";
1716
//String arg1 = "com.dmdev.spring.config.ApplicationConfiguration";
18-
//String arg2 = "D:\\Projects\\spring-starter-lesson-28\\build\\resources\\main\\application.properties";
17+
//String arg2 = "/Users/kirillshishin/IdeaProjects/spring-starter-lesson-28/src/main/resources/application.properties";
18+
//String arg3 = "/Users/kirillshishin/IdeaProjects/spring-starter-lesson-28/src/main/resources/application.xml";
1919

2020
ClassLoader classLoader = new URLClassLoader(new URL[]{Path.of(args[0]).toUri().toURL()});
2121
Class<?> userConfigurationClass = classLoader.loadClass(args[1]);
2222

2323
ConfigurationManager configurationManager = new ConfigurationManager(classLoader, userConfigurationClass);
2424
PropertiesAnalyzer propertiesAnalyzer = new PropertiesAnalyzer(args[2]);
25+
XmlBeanAnalyzer xmlBeanAnalyzer = new XmlBeanAnalyzer(args[3]);
26+
27+
xmlBeanAnalyzer.rewriteXmlFile();
28+
// call action to update project tree
2529

2630
configurationManager.patchPropertySourceAnnotation();
31+
configurationManager.patchImportResourceAnnotation();
2732

2833
SpringApplicationBuilder app = new SpringApplicationBuilder(SpringAnalysisRunner.class)
2934
.sources(TestApplicationConfiguration.class, userConfigurationClass);
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package analyzer;
2+
3+
import org.w3c.dom.Document;
4+
import org.w3c.dom.Element;
5+
import org.w3c.dom.NodeList;
6+
7+
import javax.xml.parsers.DocumentBuilder;
8+
import javax.xml.parsers.DocumentBuilderFactory;
9+
import javax.xml.transform.Result;
10+
import javax.xml.transform.Source;
11+
import javax.xml.transform.Transformer;
12+
import javax.xml.transform.TransformerException;
13+
import javax.xml.transform.TransformerFactory;
14+
import javax.xml.transform.dom.DOMSource;
15+
import javax.xml.transform.stream.StreamResult;
16+
import java.nio.file.Path;
17+
import java.nio.file.Paths;
18+
19+
20+
public class XmlBeanAnalyzer {
21+
//change to auto path detect
22+
private final String PATH = "utbot-spring-analyzer/src/main/resources";
23+
private final String xmlFilePath;
24+
private final String fakeXmlFilePath;
25+
26+
27+
public XmlBeanAnalyzer(String xmlFilePath) {
28+
this.xmlFilePath = xmlFilePath;
29+
this.fakeXmlFilePath = getFakePath(xmlFilePath);
30+
}
31+
32+
private String getFakePath(String filePath) {
33+
Path path = Paths.get(filePath);
34+
return Paths.get(PATH, "fake" + path.getFileName()).toString();
35+
}
36+
37+
public void rewriteXmlFile() throws Exception {
38+
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
39+
DocumentBuilder builder = factory.newDocumentBuilder();
40+
Document doc = builder.parse(xmlFilePath);
41+
42+
deletePropertyPlaceholder(doc);
43+
writeXmlFile(doc);
44+
}
45+
46+
private void deletePropertyPlaceholder(Document doc) {
47+
NodeList elements = doc.getElementsByTagName("context:property-placeholder");
48+
int elementsCount = elements.getLength();
49+
50+
//https://stackoverflow.com/questions/26618400/how-to-use-multiple-property-placeholder-in-a-spring-xml-file
51+
for (int i = 0; i < elementsCount; i++) {
52+
Element element = (Element) elements.item(i);
53+
element.getParentNode().removeChild(element);
54+
}
55+
doc.normalize();
56+
}
57+
58+
private void writeXmlFile(Document doc) throws TransformerException {
59+
Source source = new DOMSource(doc);
60+
Result dest = new StreamResult(fakeXmlFilePath);
61+
62+
TransformerFactory tFactory = TransformerFactory.newInstance();
63+
Transformer tFormer = tFactory.newTransformer();
64+
tFormer.transform(source, dest);
65+
}
66+
}

0 commit comments

Comments
 (0)