Skip to content
This repository was archived by the owner on Nov 23, 2021. It is now read-only.

Commit 5c14356

Browse files
author
Zach Brown
committed
Make the mismatch message grammatically correct
1 parent ccbd762 commit 5c14356

File tree

2 files changed

+35
-5
lines changed

2 files changed

+35
-5
lines changed

src/main/java/com/destroystokyo/debuggery/reflection/ReflectionChain.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,7 @@ private Object reflect(Object instance, Method method, Object[] args) throws Inv
9090
final int paramCount = method.getParameterCount();
9191

9292
if (args.length != paramCount) {
93-
String name = method.getName();
94-
String returnType = method.getReturnType().getSimpleName();
95-
96-
return "Method " + name + " requires " + paramCount + " args and returns a " + returnType + "\n"
97-
+ ReflectionUtil.getFormattedMethodSignature(method);
93+
return ReflectionUtil.getArgMismatchString(method);
9894
}
9995

10096
if (!method.isAccessible()) {

src/main/java/com/destroystokyo/debuggery/reflection/ReflectionUtil.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.lang.reflect.Method;
2323
import java.lang.reflect.Modifier;
2424
import java.util.*;
25+
import java.util.function.Predicate;
2526

2627
public class ReflectionUtil {
2728
private static Map<Class, Map<String, Method>> globalMethodMap = new HashMap<>();
@@ -149,4 +150,37 @@ public static String getFormattedMethodSignature(Method method) {
149150

150151
return builder.toString();
151152
}
153+
154+
/**
155+
* Gets the error message we should send when the input string is missing arguments
156+
*
157+
* @param method method with missing arguments
158+
* @return error message
159+
*/
160+
@Nonnull
161+
public static String getArgMismatchString(Method method) {
162+
final String methodName = method.getName();
163+
final Class returnType = method.getReturnType();
164+
final String returnTypeName = returnType.getSimpleName();
165+
String returnInfo;
166+
167+
if (returnType.equals(Void.TYPE)) {
168+
returnInfo = "returns void.";
169+
} else if (startsWithVowel.test(returnTypeName)) {
170+
returnInfo = "returns an " + returnTypeName;
171+
} else {
172+
returnInfo = "returns a " + returnTypeName;
173+
}
174+
175+
return "Method " + methodName + " requires " + method.getParameterCount() + " args and " + returnInfo + "\n"
176+
+ ReflectionUtil.getFormattedMethodSignature(method);
177+
}
178+
179+
/**
180+
* Tests if a given string starts with a vowel
181+
*/
182+
private static Predicate<String> startsWithVowel = s -> {
183+
s = s.toLowerCase();
184+
return s.startsWith("a") || s.startsWith("e") || s.startsWith("i") || s.startsWith("o") || s.startsWith("u");
185+
};
152186
}

0 commit comments

Comments
 (0)