Skip to content

Commit 42f2370

Browse files
committed
Some corrections is property source support
1 parent 6b7cae9 commit 42f2370

File tree

2 files changed

+23
-9
lines changed

2 files changed

+23
-9
lines changed
Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
package analyzer;
22

3+
import org.springframework.context.annotation.PropertySource;
4+
5+
import java.lang.annotation.Annotation;
36
import java.lang.reflect.Field;
47
import java.lang.reflect.InvocationHandler;
8+
import java.util.Arrays;
59
import java.util.Map;
10+
import java.util.Optional;
611

712
public class ConfigurationManager {
813

@@ -14,18 +19,27 @@ public ConfigurationManager(ClassLoader classLoader, Class userConfigurationClas
1419
this.userConfigurationClass = userConfigurationClass;
1520
}
1621

17-
public void patchPropertySourceAnnotation() throws IllegalAccessException, NoSuchFieldException, ClassNotFoundException {
22+
public void patchPropertySourceAnnotation() throws Exception {
1823

1924
Class<?> proxyClass = classLoader.loadClass("java.lang.reflect.Proxy");
2025
Field hField = proxyClass.getDeclaredField("h");
2126
hField.setAccessible(true);
2227

23-
InvocationHandler o = (InvocationHandler) (hField.get(userConfigurationClass.getAnnotations()[2]));
24-
25-
Class<?> annotationInvocationHandlerClass = classLoader.loadClass("sun.reflect.annotation.AnnotationInvocationHandler");
26-
Field memberValuesField = annotationInvocationHandlerClass.getDeclaredField("memberValues");
27-
memberValuesField.setAccessible(true);
28-
Map<String, Object> memberValues = (Map<String, Object>) (memberValuesField.get(o));
29-
memberValues.put("value", "classpath:fakeapplication.properties");
28+
//Annotation annotationProxy = userConfigurationClass.getAnnotations()[2];
29+
Optional<Annotation> propertySourceAnnotation =
30+
Arrays.stream(userConfigurationClass.getAnnotations())
31+
.filter(el -> el.annotationType() == PropertySource.class)
32+
.findFirst();
33+
if (propertySourceAnnotation.isPresent()) {
34+
InvocationHandler annotationInvocationHandler = (InvocationHandler) (hField.get(propertySourceAnnotation.get()));
35+
36+
Class<?> annotationInvocationHandlerClass =
37+
classLoader.loadClass("sun.reflect.annotation.AnnotationInvocationHandler");
38+
Field memberValuesField = annotationInvocationHandlerClass.getDeclaredField("memberValues");
39+
memberValuesField.setAccessible(true);
40+
41+
Map<String, Object> memberValues = (Map<String, Object>) (memberValuesField.get(annotationInvocationHandler));
42+
memberValues.put("value", "classpath:fakeapplication.properties");
43+
}
3044
}
3145
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
@SpringBootApplication
1313
public class SpringAnalysisRunner {
1414

15-
public static void main(String[] args) throws ClassNotFoundException, IOException, NoSuchFieldException, IllegalAccessException {
15+
public static void main(String[] args) throws Exception {
1616
//String arg0 = "D:\\Projects\\spring-starter-lesson-28\\build\\classes\\java\\main";
1717
//String arg1 = "com.dmdev.spring.config.ApplicationConfiguration";
1818
//String arg2 = "D:\\Projects\\spring-starter-lesson-28\\build\\resources\\main\\application.properties";

0 commit comments

Comments
 (0)