/** * Instantiate a Kotlin class using the provided constructor. * @param ctor the constructor of the Kotlin class to instantiate * @param args the constructor arguments to apply * (use {@code null} for unspecified parameter if needed) */ public static <T> T instantiateClass(Constructor<T> ctor, Object... args) throws IllegalAccessException, InvocationTargetException, InstantiationException { KFunction<T> kotlinConstructor = ReflectJvmMapping.getKotlinFunction(ctor); if (kotlinConstructor == null) { return ctor.newInstance(args); } List<KParameter> parameters = kotlinConstructor.getParameters(); Map<KParameter, Object> argParameters = new HashMap<>(parameters.size()); Assert.isTrue(args.length <= parameters.size(), "Number of provided arguments should be less of equals than number of constructor parameters"); for (int i = 0 ; i < args.length ; i++) { if (!(parameters.get(i).isOptional() && args[i] == null)) { argParameters.put(parameters.get(i), args[i]); } } return kotlinConstructor.callBy(argParameters); }
/** * Instantiate a Kotlin class using the provided constructor. * @param ctor the constructor of the Kotlin class to instantiate * @param args the constructor arguments to apply * (use {@code null} for unspecified parameter if needed) */ public static <T> T instantiateClass(Constructor<T> ctor, Object... args) throws IllegalAccessException, InvocationTargetException, InstantiationException { KFunction<T> kotlinConstructor = ReflectJvmMapping.getKotlinFunction(ctor); if (kotlinConstructor == null) { return ctor.newInstance(args); } List<KParameter> parameters = kotlinConstructor.getParameters(); Map<KParameter, Object> argParameters = new HashMap<>(parameters.size()); Assert.isTrue(args.length <= parameters.size(), "Number of provided arguments should be less of equals than number of constructor parameters"); for (int i = 0 ; i < args.length ; i++) { if (!(parameters.get(i).isOptional() && args[i] == null)) { argParameters.put(parameters.get(i), args[i]); } } return kotlinConstructor.callBy(argParameters); }
.collect(Collectors.toList()) .get(index); return (parameter.getType().isMarkedNullable() || parameter.isOptional());
.collect(Collectors.toList()) .get(index); return (parameter.getType().isMarkedNullable() || parameter.isOptional());
if (parameter.isOptional() && !isPresent.test(parameter)) { mask = mask | (1 << (index % Integer.SIZE));
Class<Object> type = parameter.getType().getType(); if (it.isOptional() && params[index] == null) { if (type.isPrimitive()) {
/** * 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; }
.collect(Collectors.toList()) .get(index); return (parameter.getType().isMarkedNullable() || parameter.isOptional());