Handle parameters of Kotlin extension methods correctly

The EXTENSION_RECEIVER parameter of Kotlin's extension
methods appear as normal method parameters to Java and
thus require a name. The synthetic name "$receiver" is
used here, as it is not a valid Kotlin identifier,
but valid in Java.

Issue: SPR-16119
This commit is contained in:
diesieben07
2017-10-26 15:09:32 +02:00
committed by sdeleuze
parent 2be2aa7b96
commit 0e49e32188
2 changed files with 17 additions and 2 deletions

View File

@@ -75,11 +75,15 @@ public class KotlinReflectionParameterNameDiscoverer implements ParameterNameDis
private String[] getParameterNames(List<KParameter> parameters) {
List<KParameter> filteredParameters = parameters
.stream()
.filter(p -> KParameter.Kind.VALUE.equals(p.getKind()))
// Extension receivers of extension methods must be included as they appear as normal method parameters in Java
.filter(p -> KParameter.Kind.VALUE.equals(p.getKind()) || KParameter.Kind.EXTENSION_RECEIVER.equals(p.getKind()))
.collect(Collectors.toList());
String[] parameterNames = new String[filteredParameters.size()];
for (int i = 0; i < filteredParameters.size(); i++) {
String name = filteredParameters.get(i).getName();
KParameter parameter = filteredParameters.get(i);
// extension receivers are not explicitly named, but require a name for Java interoperability
// $receiver is not a valid Kotlin identifier, but valid in Java, so it can be used here
String name = KParameter.Kind.EXTENSION_RECEIVER.equals(parameter.getKind()) ? "$receiver" : parameter.getName();
if (name == null) {
return null;
}