Skip to content

Fix #34760: ignore Kotlin DefaultConstructorMarker in BeanUtils#getParameterNames #34860

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import java.util.Set;

import kotlin.jvm.JvmClassMappingKt;
import kotlin.jvm.internal.DefaultConstructorMarker;
import kotlin.reflect.KClass;
import kotlin.reflect.KFunction;
import kotlin.reflect.KParameter;
Expand Down Expand Up @@ -659,7 +660,13 @@ public static MethodParameter getWriteMethodParameter(PropertyDescriptor pd) {
ConstructorProperties cp = ctor.getAnnotation(ConstructorProperties.class);
@Nullable String[] paramNames = (cp != null ? cp.value() : parameterNameDiscoverer.getParameterNames(ctor));
Assert.state(paramNames != null, () -> "Cannot resolve parameter names for constructor " + ctor);
Assert.state(paramNames.length == ctor.getParameterCount(),

// The generated param "DefaultConstructorMarker" is used to avoid collision of signatures
// and should be filtered out
long realParamsCount = Arrays.stream(ctor.getParameters())
.filter(p -> !DefaultConstructorMarker.class.equals(p.getType()))
.count();
Assert.state(paramNames.length == realParamsCount,
() -> "Invalid number of parameter names: " + paramNames.length + " for constructor " + ctor);
return paramNames;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,67 @@ class BeanUtilsKotlinTests {
assertThat(instance).isEqualTo(ConstructorWithNullablePrimitiveValueClass(null))
}

@Test
fun `getParameterNames filters out DefaultConstructorMarker with Foo`() {
val ctor = BeanUtils.findPrimaryConstructor(Foo::class.java)!!
val names = BeanUtils.getParameterNames(ctor)
assertThat(names).containsExactly("param1", "param2")
}


@Test
fun `getParameterNames filters out DefaultConstructorMarker with Bar`() {
val ctor = BeanUtils.findPrimaryConstructor(Bar::class.java)!!
val names = BeanUtils.getParameterNames(ctor)
assertThat(names).containsExactly("param1", "param2")
}

@Test
fun `getParameterNames filters out DefaultConstructorMarker with Baz`() {
val ctor = BeanUtils.findPrimaryConstructor(Baz::class.java)!!
val names = BeanUtils.getParameterNames(ctor)
assertThat(names).containsExactly("param1", "param2")
}

@Test
fun `getParameterNames filters out DefaultConstructorMarker with Qux`() {
val ctor = BeanUtils.findPrimaryConstructor(Qux::class.java)!!
val names = BeanUtils.getParameterNames(ctor)
assertThat(names).containsExactly("param1", "param2")
}

@Test
fun `getParameterNames filters out DefaultConstructorMarker with ConstructorWithValueClass`() {
val ctor = BeanUtils.findPrimaryConstructor(ConstructorWithValueClass::class.java)!!
assertThat(ctor).isNotNull()
val names = BeanUtils.getParameterNames(ctor)
assertThat(names).containsExactly("value")
}

@Test
fun `getParameterNames filters out DefaultConstructorMarker with ConstructorWithNullableValueClass`() {
val ctor = BeanUtils.findPrimaryConstructor(ConstructorWithNullableValueClass::class.java)!!
assertThat(ctor).isNotNull()
val names = BeanUtils.getParameterNames(ctor)
assertThat(names).containsExactly("value")
}

@Test
fun `getParameterNames filters out DefaultConstructorMarker with ConstructorWithPrimitiveValueClass`() {
val ctor = BeanUtils.findPrimaryConstructor(ConstructorWithPrimitiveValueClass::class.java)!!
assertThat(ctor).isNotNull()
val names = BeanUtils.getParameterNames(ctor)
assertThat(names).containsExactly("value")
}

@Test
fun `getParameterNames filters out DefaultConstructorMarker with ConstructorWithNullablePrimitiveValueClass`() {
val ctor = BeanUtils.findPrimaryConstructor(ConstructorWithNullablePrimitiveValueClass::class.java)!!
assertThat(ctor).isNotNull()
val names = BeanUtils.getParameterNames(ctor)
assertThat(names).containsExactly("value")
}


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

Expand Down