Skip to content

Commit

Permalink
Revert "Performance optimization by using Method.getParameterCount() …
Browse files Browse the repository at this point in the history
…where possible" (#1862)
  • Loading branch information
TimvdLippe committed Jan 27, 2020
1 parent 2994de4 commit 34790ec
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ private <T> T withParams(Class<T> cls, Object... params) {
List<Constructor<?>> matchingConstructors = new LinkedList<Constructor<?>>();
try {
for (Constructor<?> constructor : cls.getDeclaredConstructors()) {
if (constructorParamsMatch(constructor, params)) {
Class<?>[] types = constructor.getParameterTypes();
if (paramsMatch(types, params)) {
evaluateConstructor(matchingConstructors, constructor);
}
}
Expand Down Expand Up @@ -115,11 +116,10 @@ private InstantiationException multipleMatchingConstructors(Class<?> cls, List<C
), null);
}

private static boolean constructorParamsMatch(Constructor<?> constructor, Object[] params) {
if (params.length != constructor.getParameterCount()) {
private static boolean paramsMatch(Class<?>[] types, Object[] params) {
if (params.length != types.length) {
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: 3 additions & 4 deletions src/main/java/org/mockito/internal/exceptions/Reporter.java
Original file line number Diff line number Diff line change
Expand Up @@ -784,17 +784,16 @@ public static MockitoException invalidArgumentPositionRangeAtInvocationTime(Invo
}

private static StringBuilder possibleArgumentTypesOf(InvocationOnMock invocation) {
Method method = invocation.getMethod();
if (method.getParameterCount() == 0) {
Class<?>[] parameterTypes = invocation.getMethod().getParameterTypes();
if (parameterTypes.length == 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 (method.isVarArgs() && i == parameterTypesLength - 1) {
if (invocation.getMethod().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
Original file line number Diff line number Diff line change
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()) && m1.getParameterCount() == m2.getParameterCount()) {
if (m1.getName() != null && m1.getName().equals(m2.getName())) {
/* Avoid unnecessary cloning */
Class<?>[] params1 = m1.getParameterTypes();
Class<?>[] params2 = m2.getParameterTypes();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ private static Class<?> getArgumentType(ArgumentMatcher<?> argumentMatcher) {
* {@link ArgumentMatcher#matches(Object)}
*/
private static boolean isMatchesMethod(Method method) {
if (method.getParameterCount() != 1) {
if (method.getParameterTypes().length != 1) {
return false;
}
if (method.isBridge()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,10 @@ private void validateArgumentTypeCompatibility(Invocation invocation, int argume
}

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

private boolean wantedArgumentPositionIsValidForInvocation(InvocationOnMock invocation, int argumentPosition) {
Expand Down
Original file line number Diff line number Diff line change
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.getParameterCount() == 1
&& method.getParameterTypes().length == 1
&& method.getParameterTypes()[0] == method.getDeclaringClass();
}
}
Original file line number Diff line number Diff line change
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.getParameterCount() - constructorA.getParameterCount();
int argLengths = constructorB.getParameterTypes().length - constructorA.getParameterTypes().length;
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.getParameterCount() == 0) {
if(constructor.getParameterTypes().length == 0) {
throw new MockitoException("the field " + field.getName() + " of type " + field.getType() + " has no parameterized constructor");
}
}
Expand Down

0 comments on commit 34790ec

Please sign in to comment.