10
10
11
11
package org .junit .jupiter .params ;
12
12
13
- import static org .junit .platform .commons .util .CollectionUtils .getFirstElement ;
14
-
15
13
import java .lang .reflect .Constructor ;
16
- import java .util .Optional ;
17
14
18
15
import org .junit .jupiter .api .extension .ExtensionContext ;
19
16
import org .junit .platform .commons .JUnitException ;
20
- import org .junit .platform .commons .PreconditionViolationException ;
21
17
import org .junit .platform .commons .util .Preconditions ;
22
18
import org .junit .platform .commons .util .ReflectionUtils ;
23
19
@@ -28,46 +24,46 @@ class ParameterizedTestSpiInstantiator {
28
24
29
25
static <T > T instantiate (Class <T > spiInterface , Class <? extends T > implementationClass ,
30
26
ExtensionContext extensionContext ) {
27
+
31
28
return extensionContext .getExecutableInvoker () //
32
29
.invoke (findConstructor (spiInterface , implementationClass ));
33
30
}
34
31
35
32
/**
36
- * Find the "best" constructor for the supplied class.
33
+ * Find the "best" constructor for the supplied implementation class.
37
34
*
38
- * <p>For backward compatibility, it first checks for a default constructor
39
- * which takes precedence over any other constructor. If no default
40
- * constructor is found, it checks for a single constructor and returns it.
35
+ * <p>For backward compatibility, it first checks for a single constructor
36
+ * and returns that. If there are multiple constructors, it checks for a
37
+ * default constructor which takes precedence over any other constructors.
38
+ * Otherwise, this method throws an exception stating that it failed to
39
+ * find a suitable constructor.
41
40
*/
41
+ @ SuppressWarnings ("unchecked" )
42
42
private static <T , V extends T > Constructor <? extends V > findConstructor (Class <T > spiInterface ,
43
43
Class <V > implementationClass ) {
44
44
45
45
Preconditions .condition (!ReflectionUtils .isInnerClass (implementationClass ),
46
46
() -> String .format ("The %s [%s] must be either a top-level class or a static nested class" ,
47
47
spiInterface .getSimpleName (), implementationClass .getName ()));
48
48
49
- return findDefaultConstructor (implementationClass ) //
50
- .orElseGet (() -> findSingleConstructor (spiInterface , implementationClass ));
51
- }
52
-
53
- @ SuppressWarnings ("unchecked" )
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
- }
49
+ Constructor <?>[] constructors = implementationClass .getDeclaredConstructors ();
58
50
59
- private static <T , V extends T > Constructor <V > findSingleConstructor (Class <T > spiInterface ,
60
- Class <V > implementationClass ) {
61
-
62
- try {
63
- return ReflectionUtils .getDeclaredConstructor (implementationClass );
51
+ // Single constructor?
52
+ if (constructors .length == 1 ) {
53
+ return (Constructor <V >) constructors [0 ];
64
54
}
65
- catch (PreconditionViolationException ex ) {
66
- String message = String .format (
67
- "Failed to find constructor for %s [%s]. "
68
- + "Please ensure that a no-argument or a single constructor exists." ,
69
- spiInterface .getSimpleName (), implementationClass .getName ());
70
- throw new JUnitException (message );
55
+ // Find default constructor.
56
+ for (Constructor <?> constructor : constructors ) {
57
+ if (constructor .getParameterCount () == 0 ) {
58
+ return (Constructor <V >) constructor ;
59
+ }
71
60
}
61
+ // Otherwise...
62
+ String message = String .format (
63
+ "Failed to find constructor for %s [%s]. "
64
+ + "Please ensure that a no-argument or a single constructor exists." ,
65
+ spiInterface .getSimpleName (), implementationClass .getName ());
66
+ throw new JUnitException (message );
72
67
}
68
+
73
69
}
0 commit comments