Skip to content

Commit 93b8b92

Browse files
committed
chore: readability refactoring
1 parent c44f316 commit 93b8b92

File tree

2 files changed

+75
-50
lines changed

2 files changed

+75
-50
lines changed

annotations/src/main/java/com/javadiscord/jdi/core/processor/validator/EventListenerValidator.java

Lines changed: 46 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -405,37 +405,55 @@ public boolean hasZeroArgsConstructor(Class<?> clazz) {
405405
private boolean validateMethods(Class<?> clazz) {
406406
Method[] methods = clazz.getMethods();
407407
for (Method method : methods) {
408-
for (Map.Entry<Class<? extends Annotation>, String[]> entry : EXPECTED_PARAM_TYPES_MAP
409-
.entrySet()) {
410-
Class<? extends Annotation> annotationClass = entry.getKey();
411-
if (method.isAnnotationPresent(annotationClass)) {
412-
String[] expectedParamTypes = entry.getValue();
413-
if (method.getParameterCount() > 0) {
414-
Class<?>[] paramTypes = method.getParameterTypes();
415-
for (Class<?> type : paramTypes) {
416-
boolean isExpectedType = false;
417-
for (String expectedType : expectedParamTypes) {
418-
if (type.getName().equals(expectedType)) {
419-
isExpectedType = true;
420-
break;
421-
}
422-
}
423-
if (!isExpectedType) {
424-
LOGGER.error("Unexpected parameter found: {}", type.getName());
425-
return false;
426-
} else {
427-
LOGGER.trace("Loaded {}", clazz.getName());
428-
}
429-
}
430-
} else if (method.getParameterCount() != 0) {
431-
LOGGER.error(
432-
"{} does not have the expected parameter types", method.getName()
433-
);
434-
return false;
435-
}
408+
if (!validateMethodAnnotations(method)) {
409+
return false;
410+
}
411+
}
412+
return true;
413+
}
414+
415+
private boolean validateMethodAnnotations(Method method) {
416+
for (Map.Entry<Class<? extends Annotation>, String[]> entry : EXPECTED_PARAM_TYPES_MAP
417+
.entrySet()) {
418+
Class<? extends Annotation> annotationClass = entry.getKey();
419+
if (method.isAnnotationPresent(annotationClass)) {
420+
if (!validateMethodParameters(method, entry.getValue())) {
421+
return false;
436422
}
437423
}
438424
}
439425
return true;
440426
}
427+
428+
private boolean validateMethodParameters(Method method, String[] expectedParamTypes) {
429+
if (method.getParameterCount() > 0) {
430+
return checkParameterTypes(method, expectedParamTypes);
431+
} else if (method.getParameterCount() != 0) {
432+
LOGGER.error("{} does not have the expected parameter types", method.getName());
433+
return false;
434+
}
435+
return true;
436+
}
437+
438+
private boolean checkParameterTypes(Method method, String[] expectedParamTypes) {
439+
Class<?>[] paramTypes = method.getParameterTypes();
440+
for (Class<?> type : paramTypes) {
441+
if (!isExpectedType(type, expectedParamTypes)) {
442+
LOGGER.error("Unexpected parameter found: {}", type.getName());
443+
return false;
444+
} else {
445+
LOGGER.trace("Loaded {}", method.getDeclaringClass().getName());
446+
}
447+
}
448+
return true;
449+
}
450+
451+
private boolean isExpectedType(Class<?> type, String[] expectedParamTypes) {
452+
for (String expectedType : expectedParamTypes) {
453+
if (type.getName().equals(expectedType)) {
454+
return true;
455+
}
456+
}
457+
return false;
458+
}
441459
}

annotations/src/main/java/com/javadiscord/jdi/core/processor/validator/SlashCommandValidator.java

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -28,34 +28,41 @@ public class SlashCommandValidator {
2828
}
2929

3030
public boolean validate(Method method) {
31-
for (Map.Entry<Class<? extends Annotation>, String[]> entry : EXPECTED_PARAM_TYPES_MAP
32-
.entrySet()) {
31+
for (Map.Entry<Class<? extends Annotation>, String[]> entry : EXPECTED_PARAM_TYPES_MAP.entrySet()) {
3332
Class<? extends Annotation> annotationClass = entry.getKey();
3433
if (method.isAnnotationPresent(annotationClass)) {
35-
String[] expectedParamTypes = entry.getValue();
36-
if (method.getParameterCount() > 0) {
37-
Class<?>[] paramTypes = method.getParameterTypes();
38-
for (Class<?> type : paramTypes) {
39-
boolean isExpectedType = false;
40-
for (String expectedType : expectedParamTypes) {
41-
if (type.getName().equals(expectedType)) {
42-
isExpectedType = true;
43-
break;
44-
}
45-
}
46-
if (!isExpectedType) {
47-
LOGGER.error("Unexpected parameter found: {}", type.getName());
48-
return false;
49-
}
50-
}
51-
} else if (method.getParameterCount() != 0) {
52-
LOGGER.error(
53-
"{} does not have the expected parameter types", method.getName()
54-
);
34+
if (!validateMethodParameters(method, entry.getValue())) {
5535
return false;
5636
}
5737
}
5838
}
5939
return true;
6040
}
41+
42+
private boolean validateMethodParameters(Method method, String[] expectedParamTypes) {
43+
if (method.getParameterCount() > 0) {
44+
return checkParameterTypes(method, expectedParamTypes);
45+
}
46+
return true;
47+
}
48+
49+
private boolean checkParameterTypes(Method method, String[] expectedParamTypes) {
50+
Class<?>[] paramTypes = method.getParameterTypes();
51+
for (Class<?> type : paramTypes) {
52+
if (!isExpectedType(type, expectedParamTypes)) {
53+
LOGGER.error("Unexpected parameter found: {}", type.getName());
54+
return false;
55+
}
56+
}
57+
return true;
58+
}
59+
60+
private boolean isExpectedType(Class<?> type, String[] expectedParamTypes) {
61+
for (String expectedType : expectedParamTypes) {
62+
if (type.getName().equals(expectedType)) {
63+
return true;
64+
}
65+
}
66+
return false;
67+
}
6168
}

0 commit comments

Comments
 (0)