@@ -52,11 +52,12 @@ private void processClassMethods(Class<?> clazz) throws Exception {
52
52
private void registerComponent (
53
53
Method method
54
54
) throws InvocationTargetException , IllegalAccessException {
55
- if (!COMPONENTS .containsKey (method .getReturnType ())) {
56
- COMPONENTS .put (method .getReturnType (), method .invoke (null ));
57
- LOGGER .info ("Loaded component {}" , method .getReturnType ().getName ());
55
+ Class <?> returnType = method .getReturnType ();
56
+ if (!COMPONENTS .containsKey (returnType )) {
57
+ COMPONENTS .put (returnType , method .invoke (null ));
58
+ LOGGER .info ("Loaded component {}" , returnType .getName ());
58
59
} else {
59
- LOGGER .error ("Component {} already loaded" , method . getReturnType () .getName ());
60
+ LOGGER .error ("Component {} already loaded" , returnType .getName ());
60
61
}
61
62
}
62
63
@@ -78,27 +79,29 @@ private static void injectFields(Object component) {
78
79
}
79
80
80
81
private static void injectField (Object component , Field field ) {
81
- if (COMPONENTS .containsKey (field .getType ())) {
82
- Object dependency = COMPONENTS .get (field .getType ());
83
- if (dependency != null ) {
84
- try {
85
- field .setAccessible (true );
86
- field .set (component , dependency );
87
- LOGGER .info (
88
- "Injected component {} into {}" ,
89
- dependency .getClass ().getName (), field .getType ()
90
- );
91
- } catch (IllegalAccessException e ) {
92
- throw new RuntimeException (
93
- "Failed to inject dependency into field: " + field .getName (),
94
- e
95
- );
96
- }
97
- }
82
+ Class <?> fieldType = field .getType ();
83
+ if (COMPONENTS .containsKey (fieldType )) {
84
+ injectDependency (component , field , COMPONENTS .get (fieldType ));
98
85
} else {
99
- LOGGER .error (
100
- "No object {} was found in field {}" , field .getType (), field .getName ()
101
- );
86
+ LOGGER .error ("No object {} was found in field {}" , fieldType , field .getName ());
87
+ }
88
+ }
89
+
90
+ private static void injectDependency (Object component , Field field , Object dependency ) {
91
+ if (dependency != null ) {
92
+ try {
93
+ field .setAccessible (true );
94
+ field .set (component , dependency );
95
+ LOGGER .info (
96
+ "Injected component {} into {}" , dependency .getClass ().getName (),
97
+ field .getType ()
98
+ );
99
+ } catch (IllegalAccessException e ) {
100
+ throw new RuntimeException (
101
+ "Failed to inject dependency into field: " + field .getName (),
102
+ e
103
+ );
104
+ }
102
105
}
103
106
}
104
107
}
0 commit comments