.collect(Collectors.toList()) .get(index); return (parameter.getType().isMarkedNullable() || parameter.isOptional());
.collect(Collectors.toList()) .get(index); return (parameter.getType().isMarkedNullable() || parameter.isOptional());
private static boolean parameterMatches(List<KParameter> constructorArguments, KFunction<?> kotlinFunction) { boolean foundInstance = false; int constructorArgIndex = 0; for (KParameter parameter : kotlinFunction.getParameters()) { if (parameter.getKind() == Kind.INSTANCE) { foundInstance = true; continue; } if (constructorArguments.size() <= constructorArgIndex) { return false; } KParameter constructorParameter = constructorArguments.get(constructorArgIndex); if (!constructorParameter.getName().equals(parameter.getName()) || !constructorParameter.getType().equals(parameter.getType())) { return false; } constructorArgIndex++; } return foundInstance; }
/** * Returns {@literal} whether the given {@link MethodParameter} is nullable. Its declaring method can reference a * Kotlin function, property or interface property. * * @return {@literal true} if {@link MethodParameter} is nullable. * @since 2.0.1 */ static boolean isNullable(MethodParameter parameter) { Method method = parameter.getMethod(); if (method == null) { throw new IllegalStateException(String.format("Cannot obtain method from parameter %s!", parameter)); } KFunction<?> kotlinFunction = ReflectJvmMapping.getKotlinFunction(method); if (kotlinFunction == null) { // Fallback to own lookup because there's no public Kotlin API for that kind of lookup until // https://youtrack.jetbrains.com/issue/KT-20768 gets resolved. kotlinFunction = findKFunction(method)// .orElseThrow(() -> new IllegalArgumentException( String.format("Cannot resolve %s to a Kotlin function!", parameter))); } KType type = parameter.getParameterIndex() == -1 // ? kotlinFunction.getReturnType() // : kotlinFunction.getParameters().get(parameter.getParameterIndex() + 1).getType(); return type.isMarkedNullable(); }
/** * Check whether the specified {@link MethodParameter} represents a nullable Kotlin type or an * optional parameter (with a default value in the Kotlin declaration). */ public static boolean isOptional(MethodParameter param) { Method method = param.getMethod(); Constructor<?> ctor = param.getConstructor(); int index = param.getParameterIndex(); if (method != null && index == -1) { KFunction<?> function = ReflectJvmMapping.getKotlinFunction(method); return (function != null && function.getReturnType().isMarkedNullable()); } else { KFunction<?> function = null; if (method != null) { function = ReflectJvmMapping.getKotlinFunction(method); } else if (ctor != null) { function = ReflectJvmMapping.getKotlinFunction(ctor); } if (function != null) { List<KParameter> parameters = function.getParameters(); KParameter parameter = parameters .stream() .filter(p -> KParameter.Kind.VALUE.equals(p.getKind())) .collect(Collectors.toList()) .get(index); return (parameter.getType().isMarkedNullable() || parameter.isOptional()); } } return false; }
if(kotlinFunction != null) { KParameter kParameter = kotlinFunction.getParameters().get(parameterIndex + 1); required = !kParameter.getType().isMarkedNullable(); } else { required = false;
.collect(Collectors.toList()) .get(index); return (parameter.getType().isMarkedNullable() || parameter.isOptional());