Skip to content

Commit

Permalink
Support void and Void class lookups in ReflectionUtils (#4049)
Browse files Browse the repository at this point in the history
Both the primitive type `void` and the wrapper type `Void` are now 
supported in the internal `ReflectionUtils` to allow `String` to `Class`
conversion in parameterized tests.

Fixes #4048.

---------

Co-authored-by: Marc Philipp <[email protected]>
  • Loading branch information
bjmi and marcphilipp authored Oct 8, 2024
1 parent dceac6e commit 0f27653
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ JUnit repository on GitHub.

* Introduce `ReflectionSupport.makeAccessible(Field)` for third-party use rather than
calling the internal `ReflectionUtils.makeAccessible(Field)` method directly.
* Support both the primitive type `void` and the wrapper type `Void` in the internal
`ReflectionUtils` to support `String` to `Class` conversion in parameterized tests.


[[release-notes-5.12.0-M1-junit-jupiter]]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ public enum HierarchyTraversalMode {
long.class,
float.class,
double.class,
void.class,

boolean[].class,
byte[].class,
Expand Down Expand Up @@ -213,6 +214,7 @@ public enum HierarchyTraversalMode {
Long.class,
Float.class,
Double.class,
Void.class,
String.class,

Boolean[].class,
Expand Down Expand Up @@ -246,7 +248,7 @@ public enum HierarchyTraversalMode {

classNameToTypeMap = Collections.unmodifiableMap(classNamesToTypes);

Map<Class<?>, Class<?>> primitivesToWrappers = new IdentityHashMap<>(8);
Map<Class<?>, Class<?>> primitivesToWrappers = new IdentityHashMap<>(9);

primitivesToWrappers.put(boolean.class, Boolean.class);
primitivesToWrappers.put(byte.class, Byte.class);
Expand All @@ -256,6 +258,7 @@ public enum HierarchyTraversalMode {
primitivesToWrappers.put(long.class, Long.class);
primitivesToWrappers.put(float.class, Float.class);
primitivesToWrappers.put(double.class, Double.class);
primitivesToWrappers.put(void.class, Void.class);

primitiveToWrapperMap = Collections.unmodifiableMap(primitivesToWrappers);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ void convertsStringsToPrimitiveWrapperTypes() {

@ParameterizedTest(name = "[{index}] {0}")
@ValueSource(classes = { char.class, boolean.class, short.class, byte.class, int.class, long.class, float.class,
double.class })
double.class, void.class })
void throwsExceptionForNullToPrimitiveTypeConversion(Class<?> type) {
assertThatExceptionOfType(ArgumentConversionException.class) //
.isThrownBy(() -> convert(null, type)) //
Expand Down Expand Up @@ -261,8 +261,10 @@ void convertsStringToPath() {
@Test
void convertsStringToClass() {
assertConverts("java.lang.Integer", Class.class, Integer.class);
assertConverts("java.lang.Void", Class.class, Void.class);
assertConverts("java.lang.Thread$State", Class.class, State.class);
assertConverts("byte", Class.class, byte.class);
assertConverts("void", Class.class, void.class);
assertConverts("char[]", Class.class, char[].class);
assertConverts("java.lang.Long[][]", Class.class, Long[][].class);
assertConverts("[[[I", Class.class, int[][][].class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,7 @@ void isAssignableTo() {
// Wrappers to Primitives
assertTrue(ReflectionUtils.isAssignableTo(Integer.class, int.class));
assertTrue(ReflectionUtils.isAssignableTo(Boolean.class, boolean.class));
assertTrue(ReflectionUtils.isAssignableTo(Void.class, void.class));

// Widening Conversions from Wrappers to Primitives
assertTrue(ReflectionUtils.isAssignableTo(Integer.class, long.class));
Expand Down Expand Up @@ -759,6 +760,7 @@ void loadClass() {
@Test
void tryToLoadClass() {
assertThat(ReflectionUtils.tryToLoadClass(Integer.class.getName())).isEqualTo(success(Integer.class));
assertThat(ReflectionUtils.tryToLoadClass(Void.class.getName())).isEqualTo(success(Void.class));
}

@Test
Expand All @@ -770,6 +772,7 @@ void tryToLoadClassTrimsClassName() {
@Test
void tryToLoadClassForPrimitive() {
assertThat(ReflectionUtils.tryToLoadClass(int.class.getName())).isEqualTo(success(int.class));
assertThat(ReflectionUtils.tryToLoadClass(void.class.getName())).isEqualTo(success(void.class));
}

@Test
Expand Down

0 comments on commit 0f27653

Please sign in to comment.