Skip to content

Commit e18d674

Browse files
committed
Fix NoClassDefFoundError when kotlin-reflect is missing
Signed-off-by: wonyongg <[email protected]>
1 parent 33897bb commit e18d674

File tree

3 files changed

+33
-2
lines changed

3 files changed

+33
-2
lines changed

spring-beans/src/main/java/org/springframework/beans/BeanUtils.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
* @author Rob Harrop
7070
* @author Sam Brannen
7171
* @author Sebastien Deleuze
72+
* @author Wonyong Hwang
7273
*/
7374
public abstract class BeanUtils {
7475

@@ -186,7 +187,7 @@ public static <T> T instantiateClass(Constructor<T> ctor, @Nullable Object... ar
186187
Assert.notNull(ctor, "Constructor must not be null");
187188
try {
188189
ReflectionUtils.makeAccessible(ctor);
189-
if (KotlinDetector.isKotlinType(ctor.getDeclaringClass())) {
190+
if (KotlinDetector.isKotlinType(ctor.getDeclaringClass()) && KotlinDetector.isKotlinReflectPresent()) {
190191
return KotlinDelegate.instantiateClass(ctor, args);
191192
}
192193
else {
@@ -279,7 +280,7 @@ else if (ctors.length == 0) {
279280
*/
280281
public static <T> @Nullable Constructor<T> findPrimaryConstructor(Class<T> clazz) {
281282
Assert.notNull(clazz, "Class must not be null");
282-
if (KotlinDetector.isKotlinType(clazz)) {
283+
if (KotlinDetector.isKotlinType(clazz) && KotlinDetector.isKotlinReflectPresent()) {
283284
return KotlinDelegate.findPrimaryConstructor(clazz);
284285
}
285286
if (clazz.isRecord()) {

spring-beans/src/test/java/org/springframework/beans/BeanUtilsTests.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
* @author Chris Beams
6262
* @author Sebastien Deleuze
6363
* @author Sam Brannen
64+
* @author Wonyong Hwang
6465
* @since 19.05.2003
6566
*/
6667
class BeanUtilsTests {
@@ -533,6 +534,20 @@ void resolveMultipleRecordePackagePrivateConstructor() throws NoSuchMethodExcept
533534
.isEqualTo(RecordWithMultiplePackagePrivateConstructors.class.getDeclaredConstructor(String.class, String.class));
534535
}
535536

537+
@Test
538+
void instantiateClassWithJavaTypeWorksNormally() throws NoSuchMethodException {
539+
Constructor<TestBean> ctor = TestBean.class.getDeclaredConstructor();
540+
TestBean instance = BeanUtils.instantiateClass(ctor);
541+
assertThat(instance).isNotNull();
542+
assertThat(instance.getClass()).isEqualTo(TestBean.class);
543+
}
544+
545+
@Test
546+
void findPrimaryConstructorWithJavaTypeReturnsNull() {
547+
Constructor<?> primaryConstructor = BeanUtils.findPrimaryConstructor(TestBean.class);
548+
assertThat(primaryConstructor).isNull();
549+
}
550+
536551
private void assertSignatureEquals(Method desiredMethod, String signature) {
537552
assertThat(BeanUtils.resolveSignature(signature, MethodSignatureBean.class)).isEqualTo(desiredMethod);
538553
}

spring-beans/src/test/kotlin/org/springframework/beans/BeanUtilsKotlinTests.kt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import org.junit.jupiter.api.Test
2323
* Kotlin tests for [BeanUtils].
2424
*
2525
* @author Sebastien Deleuze
26+
* @author Wonyong Hwang
2627
*/
2728
@Suppress("unused", "UNUSED_PARAMETER")
2829
class BeanUtilsKotlinTests {
@@ -192,6 +193,20 @@ class BeanUtilsKotlinTests {
192193
assertThat(names).isEmpty()
193194
}
194195

196+
@Test
197+
fun `instantiateClass with Kotlin type works correctly`() {
198+
val ctor = Foo::class.java.getDeclaredConstructor(String::class.java, Int::class.java)
199+
val foo = BeanUtils.instantiateClass(ctor, "test", 42)
200+
assertThat(foo.param1).isEqualTo("test")
201+
assertThat(foo.param2).isEqualTo(42)
202+
}
203+
204+
@Test
205+
fun `findPrimaryConstructor with Kotlin type works correctly`() {
206+
val primaryConstructor = BeanUtils.findPrimaryConstructor(Foo::class.java)
207+
assertThat(primaryConstructor).isNotNull()
208+
assertThat(primaryConstructor!!.parameterCount).isEqualTo(2)
209+
}
195210

196211
class Foo(val param1: String, val param2: Int)
197212

0 commit comments

Comments
 (0)