Skip to content

Commit 48cd00d

Browse files
committed
Use ReflectionUtils.isInnerClass and polish implementation
1 parent b4e35c4 commit 48cd00d

File tree

1 file changed

+26
-16
lines changed

1 file changed

+26
-16
lines changed

junit-jupiter-params/src/main/java/org/junit/jupiter/params/ParameterizedTestSpiInstantiator.java

+26-16
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,18 @@
1818
import org.junit.jupiter.api.extension.ExtensionContext;
1919
import org.junit.platform.commons.JUnitException;
2020
import org.junit.platform.commons.PreconditionViolationException;
21-
import org.junit.platform.commons.support.ModifierSupport;
21+
import org.junit.platform.commons.util.Preconditions;
2222
import org.junit.platform.commons.util.ReflectionUtils;
2323

2424
/**
2525
* @since 5.12
2626
*/
2727
class ParameterizedTestSpiInstantiator {
2828

29-
static <T> T instantiate(Class<T> spiClass, Class<? extends T> clazz, ExtensionContext extensionContext) {
30-
return extensionContext.getExecutableInvoker().invoke(findConstructor(spiClass, clazz));
29+
static <T> T instantiate(Class<T> spiInterface, Class<? extends T> implementationClass,
30+
ExtensionContext extensionContext) {
31+
return extensionContext.getExecutableInvoker() //
32+
.invoke(findConstructor(spiInterface, implementationClass));
3133
}
3234

3335
/**
@@ -37,26 +39,34 @@ static <T> T instantiate(Class<T> spiClass, Class<? extends T> clazz, ExtensionC
3739
* which takes precedence over any other constructor. If no default
3840
* constructor is found, it checks for a single constructor and returns it.
3941
*/
42+
private static <T, V extends T> Constructor<? extends V> findConstructor(Class<T> spiInterface,
43+
Class<V> implementationClass) {
44+
45+
Preconditions.condition(!ReflectionUtils.isInnerClass(implementationClass),
46+
() -> String.format("The %s [%s] must be either a top-level class or a static nested class",
47+
spiInterface.getSimpleName(), implementationClass.getName()));
48+
49+
return findDefaultConstructor(implementationClass) //
50+
.orElseGet(() -> findSingleConstructor(spiInterface, implementationClass));
51+
}
52+
4053
@SuppressWarnings("unchecked")
41-
private static <T> Constructor<? extends T> findConstructor(Class<T> spiClass, Class<? extends T> clazz) {
42-
Optional<Constructor<?>> defaultConstructor = getFirstElement(
43-
ReflectionUtils.findConstructors(clazz, it -> it.getParameterCount() == 0));
44-
if (defaultConstructor.isPresent()) {
45-
return (Constructor<? extends T>) defaultConstructor.get();
46-
}
47-
if (ModifierSupport.isNotStatic(clazz)) {
48-
String message = String.format("The %s [%s] must be either a top-level class or a static nested class",
49-
spiClass.getSimpleName(), clazz.getName());
50-
throw new JUnitException(message);
51-
}
54+
private static <T> Optional<Constructor<T>> findDefaultConstructor(Class<T> clazz) {
55+
return getFirstElement(ReflectionUtils.findConstructors(clazz, it -> it.getParameterCount() == 0)) //
56+
.map(it -> (Constructor<T>) it);
57+
}
58+
59+
private static <T, V extends T> Constructor<V> findSingleConstructor(Class<T> spiInterface,
60+
Class<V> implementationClass) {
61+
5262
try {
53-
return ReflectionUtils.getDeclaredConstructor(clazz);
63+
return ReflectionUtils.getDeclaredConstructor(implementationClass);
5464
}
5565
catch (PreconditionViolationException ex) {
5666
String message = String.format(
5767
"Failed to find constructor for %s [%s]. "
5868
+ "Please ensure that a no-argument or a single constructor exists.",
59-
spiClass.getSimpleName(), clazz.getName());
69+
spiInterface.getSimpleName(), implementationClass.getName());
6070
throw new JUnitException(message);
6171
}
6272
}

0 commit comments

Comments
 (0)