1
1
package analyzer ;
2
2
3
+ import org .springframework .context .annotation .PropertySource ;
4
+
5
+ import java .lang .annotation .Annotation ;
3
6
import java .lang .reflect .Field ;
4
7
import java .lang .reflect .InvocationHandler ;
8
+ import java .util .Arrays ;
5
9
import java .util .Map ;
10
+ import java .util .Optional ;
6
11
7
12
public class ConfigurationManager {
8
13
@@ -14,18 +19,27 @@ public ConfigurationManager(ClassLoader classLoader, Class userConfigurationClas
14
19
this .userConfigurationClass = userConfigurationClass ;
15
20
}
16
21
17
- public void patchPropertySourceAnnotation () throws IllegalAccessException , NoSuchFieldException , ClassNotFoundException {
22
+ public void patchPropertySourceAnnotation () throws Exception {
18
23
19
24
Class <?> proxyClass = classLoader .loadClass ("java.lang.reflect.Proxy" );
20
25
Field hField = proxyClass .getDeclaredField ("h" );
21
26
hField .setAccessible (true );
22
27
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
+ }
30
44
}
31
45
}
0 commit comments