1
- package analyzer ;
2
-
3
- import org .springframework .beans .BeansException ;
4
- import org .springframework .beans .factory .annotation .AnnotatedBeanDefinition ;
5
- import org .springframework .beans .factory .config .BeanDefinition ;
6
- import org .springframework .beans .factory .config .BeanFactoryPostProcessor ;
7
- import org .springframework .beans .factory .config .ConfigurableListableBeanFactory ;
8
- import org .springframework .beans .factory .support .BeanDefinitionRegistry ;
9
- import org .springframework .core .PriorityOrdered ;
10
- import org .springframework .core .type .MethodMetadata ;
11
-
12
- import java .io .File ;
13
- import java .io .FileWriter ;
14
- import java .util .ArrayList ;
15
- import java .util .Arrays ;
16
-
17
- public class UtBotBeanFactoryPostProcessor implements BeanFactoryPostProcessor , PriorityOrdered {
18
-
19
- @ Override
20
- public void postProcessBeanFactory (ConfigurableListableBeanFactory beanFactory ) throws BeansException {
21
-
22
- System .out .println ("Started post-processing bean factory in UtBot" );
23
-
24
- ArrayList <String > beanClassNames = new ArrayList <>();
25
- for (String beanDefinitionName : beanFactory .getBeanDefinitionNames ()) {
26
- BeanDefinition beanDefinition = beanFactory .getBeanDefinition (beanDefinitionName );
27
-
28
- if (beanDefinition instanceof AnnotatedBeanDefinition ) {
29
- MethodMetadata factoryMethodMetadata = ((AnnotatedBeanDefinition ) beanDefinition ).getFactoryMethodMetadata ();
30
- if (factoryMethodMetadata != null ) {
31
- beanClassNames .add (factoryMethodMetadata .getReturnTypeName ());
32
- }
33
- } else {
34
- String className = beanDefinition .getBeanClassName ();
35
- if (className == null ) {
36
- className = beanFactory .getBean (beanDefinitionName ).getClass ().getName ();
37
- }
38
- beanClassNames .add (className );
39
- }
40
- }
41
- for (String beanDefinitionName : beanFactory .getBeanDefinitionNames ()) {
42
- BeanDefinitionRegistry beanRegistry = (BeanDefinitionRegistry ) beanFactory ;
43
- beanRegistry .removeBeanDefinition (beanDefinitionName );
44
- }
45
-
46
- writeToFile (beanClassNames );
47
- }
48
-
49
- private void writeToFile (ArrayList <String > beanClassNames ) {
50
- try {
51
- File springBeansFile = new File ("SpringBeans.txt" );
52
- FileWriter fileWriter = new FileWriter (springBeansFile );
53
-
54
- Object [] distinctClassNames = beanClassNames .stream ()
55
- .distinct ()
56
- .toArray ();
57
- Arrays .sort (distinctClassNames );
58
-
59
- for (Object beanClassName : distinctClassNames ) {
60
- fileWriter .append (beanClassName .toString ());
61
- fileWriter .append ("\n " );
62
- }
63
-
64
- fileWriter .flush ();
65
- fileWriter .close ();
66
- } catch (Throwable e ) {
67
- System .out .println ("Storing bean information failed" );
68
- } finally {
69
- System .out .println ("Finished post-processing bean factory in UtBot" );
70
- }
71
- }
72
-
73
- @ Override
74
- public int getOrder () {
75
- return PriorityOrdered .HIGHEST_PRECEDENCE ;
76
- }
1
+ package post_processors ;
2
+
3
+ import org .springframework .beans .BeansException ;
4
+ import org .springframework .beans .factory .annotation .AnnotatedBeanDefinition ;
5
+ import org .springframework .beans .factory .config .BeanDefinition ;
6
+ import org .springframework .beans .factory .config .BeanFactoryPostProcessor ;
7
+ import org .springframework .beans .factory .config .ConfigurableListableBeanFactory ;
8
+ import org .springframework .beans .factory .support .BeanDefinitionRegistry ;
9
+ import org .springframework .core .PriorityOrdered ;
10
+ import org .springframework .core .type .MethodMetadata ;
11
+
12
+ import java .io .File ;
13
+ import java .io .FileWriter ;
14
+ import java .util .ArrayList ;
15
+ import java .util .Arrays ;
16
+
17
+ public class UtBotBeanFactoryPostProcessor implements BeanFactoryPostProcessor , PriorityOrdered {
18
+
19
+ @ Override
20
+ public void postProcessBeanFactory (ConfigurableListableBeanFactory beanFactory ) throws BeansException {
21
+
22
+ System .out .println ("Started post-processing bean factory in UtBot" );
23
+
24
+ ArrayList <String > beanClassNames = new ArrayList <>();
25
+ for (String beanDefinitionName : beanFactory .getBeanDefinitionNames ()) {
26
+ BeanDefinition beanDefinition = beanFactory .getBeanDefinition (beanDefinitionName );
27
+
28
+ if (beanDefinition instanceof AnnotatedBeanDefinition ) {
29
+ MethodMetadata factoryMethodMetadata = ((AnnotatedBeanDefinition ) beanDefinition ).getFactoryMethodMetadata ();
30
+ if (factoryMethodMetadata != null ) {
31
+ beanClassNames .add (factoryMethodMetadata .getReturnTypeName ());
32
+ }
33
+ } else {
34
+ String className = beanDefinition .getBeanClassName ();
35
+ if (className == null ) {
36
+ className = beanFactory .getBean (beanDefinitionName ).getClass ().getName ();
37
+ }
38
+ beanClassNames .add (className );
39
+ }
40
+ }
41
+ for (String beanDefinitionName : beanFactory .getBeanDefinitionNames ()) {
42
+ BeanDefinitionRegistry beanRegistry = (BeanDefinitionRegistry ) beanFactory ;
43
+ beanRegistry .removeBeanDefinition (beanDefinitionName );
44
+ }
45
+
46
+ writeToFile (beanClassNames );
47
+ }
48
+
49
+ private void writeToFile (ArrayList <String > beanClassNames ) {
50
+ try {
51
+ File springBeansFile = new File ("SpringBeans.txt" );
52
+ FileWriter fileWriter = new FileWriter (springBeansFile );
53
+
54
+ Object [] distinctClassNames = beanClassNames .stream ()
55
+ .distinct ()
56
+ .toArray ();
57
+ Arrays .sort (distinctClassNames );
58
+
59
+ for (Object beanClassName : distinctClassNames ) {
60
+ fileWriter .append (beanClassName .toString ());
61
+ fileWriter .append ("\n " );
62
+ }
63
+
64
+ fileWriter .flush ();
65
+ fileWriter .close ();
66
+ } catch (Throwable e ) {
67
+ System .out .println ("Storing bean information failed" );
68
+ } finally {
69
+ System .out .println ("Finished post-processing bean factory in UtBot" );
70
+ }
71
+ }
72
+
73
+ @ Override
74
+ public int getOrder () {
75
+ return PriorityOrdered .HIGHEST_PRECEDENCE ;
76
+ }
77
77
}
0 commit comments