Skip to content

Commit 7b67a8c

Browse files
committed
Avoid depending on mutable fields in MethodSelector.equals()
1 parent fa4b78a commit 7b67a8c

File tree

1 file changed

+26
-32
lines changed

1 file changed

+26
-32
lines changed

junit-platform-engine/src/main/java/org/junit/platform/engine/discovery/MethodSelector.java

Lines changed: 26 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ public final class MethodSelector implements DiscoverySelector {
6464
private final String className;
6565
private final String methodName;
6666
private final String parameterTypeNames;
67+
private final @Nullable String declaringClassName;
6768

6869
private volatile @Nullable Class<?> javaClass;
6970

@@ -75,51 +76,51 @@ public final class MethodSelector implements DiscoverySelector {
7576
* @since 1.10
7677
*/
7778
MethodSelector(@Nullable ClassLoader classLoader, String className, String methodName, String parameterTypeNames) {
78-
this.classLoader = classLoader;
79-
this.className = className;
80-
this.methodName = methodName;
81-
this.parameterTypeNames = parameterTypeNames;
79+
this(classLoader, className, methodName, parameterTypeNames, null);
8280
}
8381

8482
MethodSelector(Class<?> javaClass, String methodName, String parameterTypeNames) {
85-
this.classLoader = javaClass.getClassLoader();
83+
this(javaClass.getClassLoader(), javaClass.getName(), methodName, parameterTypeNames, null);
8684
this.javaClass = javaClass;
87-
this.className = javaClass.getName();
88-
this.methodName = methodName;
89-
this.parameterTypeNames = parameterTypeNames;
9085
}
9186

9287
/**
9388
* @since 1.10
9489
*/
9590
MethodSelector(@Nullable ClassLoader classLoader, String className, String methodName, Class<?>... parameterTypes) {
96-
this.classLoader = classLoader;
97-
this.className = className;
98-
this.methodName = methodName;
91+
this(classLoader, className, methodName, ClassUtils.nullSafeToString(Class::getTypeName, parameterTypes), null);
9992
this.parameterTypes = parameterTypes.clone();
100-
this.parameterTypeNames = ClassUtils.nullSafeToString(Class::getTypeName, this.parameterTypes);
10193
}
10294

10395
/**
10496
* @since 1.10
10597
*/
10698
MethodSelector(Class<?> javaClass, String methodName, Class<?>... parameterTypes) {
107-
this.classLoader = javaClass.getClassLoader();
99+
this(javaClass.getClassLoader(), javaClass.getName(), methodName,
100+
ClassUtils.nullSafeToString(Class::getTypeName, parameterTypes), null);
108101
this.javaClass = javaClass;
109-
this.className = javaClass.getName();
110-
this.methodName = methodName;
111102
this.parameterTypes = parameterTypes.clone();
112-
this.parameterTypeNames = ClassUtils.nullSafeToString(Class::getTypeName, this.parameterTypes);
113103
}
114104

115105
MethodSelector(Class<?> javaClass, Method method) {
116-
this.classLoader = javaClass.getClassLoader();
106+
this(javaClass, method, method.getParameterTypes());
107+
}
108+
109+
private MethodSelector(Class<?> javaClass, Method method, Class<?>... parameterTypes) {
110+
this(javaClass.getClassLoader(), javaClass.getName(), method.getName(),
111+
ClassUtils.nullSafeToString(Class::getTypeName, parameterTypes), method.getDeclaringClass().getName());
117112
this.javaClass = javaClass;
118-
this.className = javaClass.getName();
119113
this.javaMethod = method;
120-
this.methodName = method.getName();
121-
this.parameterTypes = method.getParameterTypes();
122-
this.parameterTypeNames = ClassUtils.nullSafeToString(Class::getTypeName, this.parameterTypes);
114+
this.parameterTypes = parameterTypes;
115+
}
116+
117+
private MethodSelector(@Nullable ClassLoader classLoader, String className, String methodName,
118+
String parameterTypeNames, @Nullable String declaringClassName) {
119+
this.classLoader = classLoader;
120+
this.className = className;
121+
this.methodName = methodName;
122+
this.parameterTypeNames = parameterTypeNames;
123+
this.declaringClassName = declaringClassName;
123124
}
124125

125126
/**
@@ -275,17 +276,10 @@ public boolean equals(Object o) {
275276
return false;
276277
}
277278
MethodSelector that = (MethodSelector) o;
278-
var equal = Objects.equals(this.className, that.className)//
279+
return Objects.equals(this.className, that.className)//
279280
&& Objects.equals(this.methodName, that.methodName)//
280-
&& Objects.equals(this.parameterTypeNames, that.parameterTypeNames);
281-
if (equal) {
282-
var thisJavaMethod = this.javaMethod;
283-
var thatJavaMethod = that.javaMethod;
284-
if (thisJavaMethod != null && thatJavaMethod != null) {
285-
return thisJavaMethod.equals(thatJavaMethod);
286-
}
287-
}
288-
return equal;
281+
&& Objects.equals(this.parameterTypeNames, that.parameterTypeNames)//
282+
&& Objects.equals(this.declaringClassName, that.declaringClassName);
289283
}
290284

291285
/**
@@ -294,7 +288,7 @@ public boolean equals(Object o) {
294288
@API(status = STABLE, since = "1.3")
295289
@Override
296290
public int hashCode() {
297-
return Objects.hash(this.className, this.methodName, this.parameterTypeNames);
291+
return Objects.hash(this.className, this.methodName, this.parameterTypeNames, this.declaringClassName);
298292
}
299293

300294
@Override

0 commit comments

Comments
 (0)