Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Performance optimization by using Method.getParameterCount() where possible #1849

Merged
merged 1 commit into from Dec 31, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -40,8 +40,7 @@ private <T> T withParams(Class<T> cls, Object... params) {
List<Constructor<?>> matchingConstructors = new LinkedList<Constructor<?>>();
try {
for (Constructor<?> constructor : cls.getDeclaredConstructors()) {
Class<?>[] types = constructor.getParameterTypes();
if (paramsMatch(types, params)) {
if (constructorParamsMatch(constructor, params)) {
evaluateConstructor(matchingConstructors, constructor);
}
}
Expand Down Expand Up @@ -116,10 +115,11 @@ private InstantiationException multipleMatchingConstructors(Class<?> cls, List<C
), null);
}

private static boolean paramsMatch(Class<?>[] types, Object[] params) {
if (params.length != types.length) {
private static boolean constructorParamsMatch(Constructor<?> constructor, Object[] params) {
if (params.length != constructor.getParameterCount()) {
return false;
}
Class<?>[] types = constructor.getParameterTypes();
for (int i = 0; i < params.length; i++) {
if (params[i] == null) {
if (types[i].isPrimitive()) {
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/org/mockito/internal/exceptions/Reporter.java
Expand Up @@ -783,16 +783,17 @@ public static MockitoException invalidArgumentPositionRangeAtInvocationTime(Invo
}

private static StringBuilder possibleArgumentTypesOf(InvocationOnMock invocation) {
Class<?>[] parameterTypes = invocation.getMethod().getParameterTypes();
if (parameterTypes.length == 0) {
Method method = invocation.getMethod();
if (method.getParameterCount() == 0) {
return new StringBuilder("the method has no arguments.\n");
}

Class<?>[] parameterTypes = method.getParameterTypes();
StringBuilder stringBuilder = new StringBuilder("the possible argument indexes for this method are :\n");
for (int i = 0, parameterTypesLength = parameterTypes.length; i < parameterTypesLength; i++) {
stringBuilder.append(" [").append(i);

if (invocation.getMethod().isVarArgs() && i == parameterTypesLength - 1) {
if (method.isVarArgs() && i == parameterTypesLength - 1) {
stringBuilder.append("+] ").append(parameterTypes[i].getComponentType().getSimpleName()).append(" <- Vararg").append("\n");
} else {
stringBuilder.append("] ").append(parameterTypes[i].getSimpleName()).append("\n");
Expand Down
Expand Up @@ -111,7 +111,7 @@ public boolean hasSameMethod(Invocation candidate) {
Method m1 = invocation.getMethod();
Method m2 = candidate.getMethod();

if (m1.getName() != null && m1.getName().equals(m2.getName())) {
if (m1.getName() != null && m1.getName().equals(m2.getName()) && m1.getParameterCount() == m2.getParameterCount()) {
/* Avoid unnecessary cloning */
Class<?>[] params1 = m1.getParameterTypes();
Class<?>[] params2 = m2.getParameterTypes();
Expand Down
Expand Up @@ -59,7 +59,7 @@ private static Class<?> getArgumentType(ArgumentMatcher<?> argumentMatcher) {
* {@link ArgumentMatcher#matches(Object)}
*/
private static boolean isMatchesMethod(Method method) {
if (method.getParameterTypes().length != 1) {
if (method.getParameterCount() != 1) {
return false;
}
if (method.isBridge()) {
Expand Down
Expand Up @@ -101,10 +101,9 @@ private void validateArgumentTypeCompatibility(Invocation invocation, int argume
}

private boolean wantedArgIndexIsVarargAndSameTypeAsReturnType(Method method, int argumentPosition) {
Class<?>[] parameterTypes = method.getParameterTypes();
return method.isVarArgs() &&
argumentPosition == /* vararg index */ parameterTypes.length - 1 &&
method.getReturnType().isAssignableFrom(parameterTypes[argumentPosition]);
argumentPosition == /* vararg index */ method.getParameterCount() - 1 &&
method.getReturnType().isAssignableFrom(method.getParameterTypes()[argumentPosition]);
}

private boolean wantedArgumentPositionIsValidForInvocation(InvocationOnMock invocation, int argumentPosition) {
Expand Down
Expand Up @@ -24,7 +24,7 @@ public static boolean isToStringMethod(Method method) {
public static boolean isCompareToMethod(Method method) {
return Comparable.class.isAssignableFrom(method.getDeclaringClass())
&& "compareTo".equals(method.getName())
&& method.getParameterTypes().length == 1
&& method.getParameterCount() == 1
&& method.getParameterTypes()[0] == method.getDeclaringClass();
}
}
Expand Up @@ -229,7 +229,7 @@ static class ParameterizedConstructorInstantiator implements ConstructorInstanti
private final ConstructorArgumentResolver argResolver;
private final Comparator<Constructor<?>> byParameterNumber = new Comparator<Constructor<?>>() {
public int compare(Constructor<?> constructorA, Constructor<?> constructorB) {
int argLengths = constructorB.getParameterTypes().length - constructorA.getParameterTypes().length;
int argLengths = constructorB.getParameterCount() - constructorA.getParameterCount();
if (argLengths == 0) {
int constructorAMockableParamsSize = countMockableParams(constructorA);
int constructorBMockableParamsSize = countMockableParams(constructorB);
Expand Down Expand Up @@ -287,7 +287,7 @@ public FieldInitializationReport instantiate() {
}

private void checkParameterized(Constructor<?> constructor, Field field) {
if(constructor.getParameterTypes().length == 0) {
if (constructor.getParameterCount() == 0) {
throw new MockitoException("the field " + field.getName() + " of type " + field.getType() + " has no parameterized constructor");
}
}
Expand Down