@@ -98,12 +98,18 @@ public static <T> T instantiate(Class<T> clazz) throws BeanInstantiationExceptio
98
98
}
99
99
100
100
/**
101
- * Instantiate a class using its no-arg constructor.
101
+ * Instantiate a class using its 'primary' constructor (for Kotlin classes,
102
+ * potentially having default arguments declared) or its default constructor
103
+ * (for regular Java classes, expecting a standard no-arg setup).
102
104
* <p>Note that this method tries to set the constructor accessible
103
105
* if given a non-accessible (that is, non-public) constructor.
104
- * @param clazz class to instantiate
106
+ * @param clazz the class to instantiate
105
107
* @return the new instance
106
- * @throws BeanInstantiationException if the bean cannot be instantiated
108
+ * @throws BeanInstantiationException if the bean cannot be instantiated.
109
+ * The cause may notably indicate a {@link NoSuchMethodException} if no
110
+ * primary/default constructor was found - or an exception thrown from
111
+ * the constructor invocation attempt, including a runtime-generated
112
+ * {@link NoClassDefFoundError} in case of an unresolvable dependency.
107
113
* @see Constructor#newInstance
108
114
*/
109
115
public static <T > T instantiateClass (Class <T > clazz ) throws BeanInstantiationException {
@@ -113,10 +119,7 @@ public static <T> T instantiateClass(Class<T> clazz) throws BeanInstantiationExc
113
119
}
114
120
try {
115
121
Constructor <T > ctor = (KotlinDetector .isKotlinType (clazz ) ?
116
- KotlinDelegate .findPrimaryConstructor (clazz ) : clazz .getDeclaredConstructor ());
117
- if (ctor == null ) {
118
- throw new BeanInstantiationException (clazz , "No default constructor found" );
119
- }
122
+ KotlinDelegate .getPrimaryConstructor (clazz ) : clazz .getDeclaredConstructor ());
120
123
return instantiateClass (ctor );
121
124
}
122
125
catch (NoSuchMethodException ex ) {
@@ -693,10 +696,26 @@ private static void copyProperties(Object source, Object target, @Nullable Class
693
696
private static class KotlinDelegate {
694
697
695
698
/**
696
- * Return the Java constructor corresponding to the Kotlin primary constructor if any.
699
+ * Determine the Java constructor corresponding to the Kotlin primary constructor.
700
+ * @param clazz the {@link Class} of the Kotlin class
701
+ * @throws NoSuchMethodException if no such constructor found
702
+ * @since 5.0.3
703
+ * @see #findPrimaryConstructor
704
+ * @see Class#getDeclaredConstructor
705
+ */
706
+ public static <T > Constructor <T > getPrimaryConstructor (Class <T > clazz ) throws NoSuchMethodException {
707
+ Constructor <T > ctor = findPrimaryConstructor (clazz );
708
+ if (ctor == null ) {
709
+ throw new NoSuchMethodException ();
710
+ }
711
+ return ctor ;
712
+ }
713
+
714
+ /**
715
+ * Retrieve the Java constructor corresponding to the Kotlin primary constructor, if any.
697
716
* @param clazz the {@link Class} of the Kotlin class
698
717
* @see <a href="http://kotlinlang.org/docs/reference/classes.html#constructors">
699
- * http://kotlinlang.org/docs/reference/classes.html#constructors</a>
718
+ * http://kotlinlang.org/docs/reference/classes.html#constructors</a>
700
719
*/
701
720
@ Nullable
702
721
public static <T > Constructor <T > findPrimaryConstructor (Class <T > clazz ) {
@@ -706,8 +725,10 @@ public static <T> Constructor<T> findPrimaryConstructor(Class<T> clazz) {
706
725
return null ;
707
726
}
708
727
Constructor <T > constructor = ReflectJvmMapping .getJavaConstructor (primaryCtor );
709
- Assert .notNull (constructor ,
710
- () -> "Failed to find Java constructor for Kotlin primary constructor: " + clazz .getName ());
728
+ if (constructor == null ) {
729
+ throw new IllegalStateException (
730
+ "Failed to find Java constructor for Kotlin primary constructor: " + clazz .getName ());
731
+ }
711
732
return constructor ;
712
733
}
713
734
catch (UnsupportedOperationException ex ) {
@@ -718,7 +739,8 @@ public static <T> Constructor<T> findPrimaryConstructor(Class<T> clazz) {
718
739
/**
719
740
* Instantiate a Kotlin class using the provided constructor.
720
741
* @param ctor the constructor of the Kotlin class to instantiate
721
- * @param args the constructor arguments to apply (use null for unspecified parameter if needed)
742
+ * @param args the constructor arguments to apply
743
+ * (use {@code null} for unspecified parameter if needed)
722
744
*/
723
745
public static <T > T instantiateClass (Constructor <T > ctor , Object ... args )
724
746
throws IllegalAccessException , InvocationTargetException , InstantiationException {
0 commit comments