Skip to content

Commit 7e26ed5

Browse files
committed
Reduce Cognitive complexity
1 parent 239b686 commit 7e26ed5

File tree

1 file changed

+27
-24
lines changed

1 file changed

+27
-24
lines changed

annotations/src/main/java/com/javadiscord/jdi/core/processor/loader/ComponentLoader.java

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,12 @@ private void processClassMethods(Class<?> clazz) throws Exception {
5252
private void registerComponent(
5353
Method method
5454
) 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());
5859
} else {
59-
LOGGER.error("Component {} already loaded", method.getReturnType().getName());
60+
LOGGER.error("Component {} already loaded", returnType.getName());
6061
}
6162
}
6263

@@ -78,27 +79,29 @@ private static void injectFields(Object component) {
7879
}
7980

8081
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));
9885
} 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+
}
102105
}
103106
}
104107
}

0 commit comments

Comments
 (0)