Search in sources :

Example 1 with KParameter

use of kotlin.reflect.KParameter in project spring-framework by spring-projects.

the class KotlinReflectionParameterNameDiscoverer method getParameterNames.

@Nullable
private String[] getParameterNames(List<KParameter> parameters) {
    List<KParameter> filteredParameters = parameters.stream().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++) {
        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;
        }
        parameterNames[i] = name;
    }
    return parameterNames;
}
Also used : List(java.util.List) KParameter(kotlin.reflect.KParameter) ReflectJvmMapping(kotlin.reflect.jvm.ReflectJvmMapping) KFunction(kotlin.reflect.KFunction) Nullable(org.springframework.lang.Nullable) Constructor(java.lang.reflect.Constructor) Method(java.lang.reflect.Method) Collectors(java.util.stream.Collectors) KParameter(kotlin.reflect.KParameter) Nullable(org.springframework.lang.Nullable)

Aggregations

Constructor (java.lang.reflect.Constructor)1 Method (java.lang.reflect.Method)1 List (java.util.List)1 Collectors (java.util.stream.Collectors)1 KFunction (kotlin.reflect.KFunction)1 KParameter (kotlin.reflect.KParameter)1 ReflectJvmMapping (kotlin.reflect.jvm.ReflectJvmMapping)1 Nullable (org.springframework.lang.Nullable)1