Skip to content

Commit 10a5879

Browse files
committed
Consistently use class literals for primitive types
To improve consistency and avoid confusion regarding primitive types and their wrapper types, this commit ensures that we always use class literals for primitive types. For example, instead of using the `Void.TYPE` constant, we now consistently use `void.class`.
1 parent 6d75184 commit 10a5879

File tree

7 files changed

+26
-21
lines changed

7 files changed

+26
-21
lines changed

junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/descriptor/LifecycleMethodUtils.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
package org.junit.jupiter.engine.descriptor;
1212

1313
import static org.junit.platform.commons.util.AnnotationUtils.findAnnotatedMethods;
14-
import static org.junit.platform.commons.util.ReflectionUtils.returnsVoid;
14+
import static org.junit.platform.commons.util.ReflectionUtils.returnsPrimitiveVoid;
1515

1616
import java.lang.annotation.Annotation;
1717
import java.lang.reflect.Method;
@@ -96,7 +96,7 @@ private static void assertNonStatic(Class<? extends Annotation> annotationType,
9696
}
9797

9898
private static void assertVoid(Class<? extends Annotation> annotationType, Method method) {
99-
if (!returnsVoid(method)) {
99+
if (!returnsPrimitiveVoid(method)) {
100100
throw new JUnitException(String.format("@%s method '%s' must not return a value.",
101101
annotationType.getSimpleName(), method.toGenericString()));
102102
}

junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/discovery/predicates/IsTestableMethod.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
import static org.junit.platform.commons.util.ReflectionUtils.isAbstract;
1515
import static org.junit.platform.commons.util.ReflectionUtils.isPrivate;
1616
import static org.junit.platform.commons.util.ReflectionUtils.isStatic;
17-
import static org.junit.platform.commons.util.ReflectionUtils.returnsVoid;
17+
import static org.junit.platform.commons.util.ReflectionUtils.returnsPrimitiveVoid;
1818

1919
import java.lang.annotation.Annotation;
2020
import java.lang.reflect.Method;
@@ -26,11 +26,11 @@
2626
abstract class IsTestableMethod implements Predicate<Method> {
2727

2828
private final Class<? extends Annotation> annotationType;
29-
private final boolean mustReturnVoid;
29+
private final boolean mustReturnPrimitiveVoid;
3030

31-
IsTestableMethod(Class<? extends Annotation> annotationType, boolean mustReturnVoid) {
31+
IsTestableMethod(Class<? extends Annotation> annotationType, boolean mustReturnPrimitiveVoid) {
3232
this.annotationType = annotationType;
33-
this.mustReturnVoid = mustReturnVoid;
33+
this.mustReturnPrimitiveVoid = mustReturnPrimitiveVoid;
3434
}
3535

3636
@Override
@@ -45,7 +45,7 @@ public boolean test(Method candidate) {
4545
if (isAbstract(candidate)) {
4646
return false;
4747
}
48-
if (returnsVoid(candidate) != this.mustReturnVoid) {
48+
if (returnsPrimitiveVoid(candidate) != this.mustReturnPrimitiveVoid) {
4949
return false;
5050
}
5151

junit-platform-commons/src/main/java/org/junit/platform/commons/util/ReflectionUtils.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -341,8 +341,14 @@ public static boolean isInnerClass(Class<?> clazz) {
341341
return !isStatic(clazz) && clazz.isMemberClass();
342342
}
343343

344-
public static boolean returnsVoid(Method method) {
345-
return method.getReturnType().equals(Void.TYPE);
344+
/**
345+
* Determine if the return type of the supplied method is primitive {@code void}.
346+
*
347+
* @param method the method to test; never {@code null}
348+
* @return {@code true} if the method's return type is {@code void}
349+
*/
350+
public static boolean returnsPrimitiveVoid(Method method) {
351+
return method.getReturnType() == void.class;
346352
}
347353

348354
/**

junit-platform-engine/src/main/java/org/junit/platform/engine/support/hierarchical/ForkJoinPoolHierarchicalTestExecutorService.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,9 @@ private ForkJoinPool createForkJoinPool(ParallelExecutionConfiguration configura
9494
}
9595

9696
private static Optional<Constructor<ForkJoinPool>> sinceJava9Constructor() {
97-
return Try.call(() -> ForkJoinPool.class.getDeclaredConstructor(Integer.TYPE, ForkJoinWorkerThreadFactory.class,
98-
UncaughtExceptionHandler.class, Boolean.TYPE, Integer.TYPE, Integer.TYPE, Integer.TYPE, Predicate.class,
99-
Long.TYPE, TimeUnit.class)) //
97+
return Try.call(() -> ForkJoinPool.class.getDeclaredConstructor(int.class, ForkJoinWorkerThreadFactory.class,
98+
UncaughtExceptionHandler.class, boolean.class, int.class, int.class, int.class, Predicate.class, long.class,
99+
TimeUnit.class)) //
100100
.toOptional();
101101
}
102102

platform-tests/src/test/java/org/junit/platform/commons/util/ClassLoaderUtilsTests.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,7 @@ void getLocationFromNullFails() {
108108

109109
@Test
110110
void getLocationFromVariousObjectsArePresent() {
111-
assertTrue(ClassLoaderUtils.getLocation(void.class).isPresent());
112-
assertTrue(ClassLoaderUtils.getLocation(byte.class).isPresent());
111+
assertTrue(ClassLoaderUtils.getLocation(getClass()).isPresent());
113112
assertTrue(ClassLoaderUtils.getLocation(this).isPresent());
114113
assertTrue(ClassLoaderUtils.getLocation("").isPresent());
115114
assertTrue(ClassLoaderUtils.getLocation(0).isPresent());

platform-tests/src/test/java/org/junit/platform/commons/util/ReflectionUtilsTests.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -176,13 +176,13 @@ void isNotFinal() throws Exception {
176176
}
177177

178178
@Test
179-
void returnsVoid() throws Exception {
179+
void returnsPrimitiveVoid() throws Exception {
180180
Class<?> clazz = ClassWithVoidAndNonVoidMethods.class;
181-
assertTrue(ReflectionUtils.returnsVoid(clazz.getDeclaredMethod("voidMethod")));
181+
assertTrue(ReflectionUtils.returnsPrimitiveVoid(clazz.getDeclaredMethod("voidMethod")));
182182

183-
assertFalse(ReflectionUtils.returnsVoid(clazz.getDeclaredMethod("methodReturningVoidReference")));
184-
assertFalse(ReflectionUtils.returnsVoid(clazz.getDeclaredMethod("methodReturningObject")));
185-
assertFalse(ReflectionUtils.returnsVoid(clazz.getDeclaredMethod("methodReturningPrimitive")));
183+
assertFalse(ReflectionUtils.returnsPrimitiveVoid(clazz.getDeclaredMethod("methodReturningVoidReference")));
184+
assertFalse(ReflectionUtils.returnsPrimitiveVoid(clazz.getDeclaredMethod("methodReturningObject")));
185+
assertFalse(ReflectionUtils.returnsPrimitiveVoid(clazz.getDeclaredMethod("methodReturningPrimitive")));
186186
}
187187

188188
@Test

platform-tests/src/test/java/org/junit/platform/engine/support/descriptor/MethodSourceTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ void getJavaMethodFromStringShouldFindVoidMethod() throws Exception {
239239

240240
@Test
241241
void getJavaMethodFromStringShouldFindMethodWithParameter() throws Exception {
242-
var testMethod = getClass().getDeclaredMethod("method3", Integer.TYPE);
242+
var testMethod = getClass().getDeclaredMethod("method3", int.class);
243243
var source = MethodSource.from(getClass().getName(), testMethod.getName(), testMethod.getParameterTypes());
244244

245245
assertThat(source.getJavaMethod()).isEqualTo(testMethod);
@@ -254,7 +254,7 @@ void getJavaMethodFromStringShouldThrowExceptionIfParameterTypesAreNotSupplied()
254254

255255
@Test
256256
void getJavaMethodFromStringShouldThrowExceptionIfParameterTypesDoNotMatch() {
257-
var source = MethodSource.from(getClass().getName(), "method3", Double.TYPE);
257+
var source = MethodSource.from(getClass().getName(), "method3", double.class);
258258

259259
assertThrows(PreconditionViolationException.class, source::getJavaMethod);
260260
}

0 commit comments

Comments
 (0)