@Override @Nullable public String[] getParameterNames(Constructor<?> ctor) { if (ctor.getDeclaringClass().isEnum() || !KotlinDetector.isKotlinType(ctor.getDeclaringClass())) { return null; } try { KFunction<?> function = ReflectJvmMapping.getKotlinFunction(ctor); return (function != null ? getParameterNames(function.getParameters()) : null); } catch (UnsupportedOperationException ex) { return null; } }
/** * 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); }
@Override @Nullable public String[] getParameterNames(Method method) { if (!KotlinDetector.isKotlinType(method.getDeclaringClass())) { return null; } try { KFunction<?> function = ReflectJvmMapping.getKotlinFunction(method); return (function != null ? getParameterNames(function.getParameters()) : null); } catch (UnsupportedOperationException ex) { return null; } }
@Override @Nullable public String[] getParameterNames(Constructor<?> ctor) { if (ctor.getDeclaringClass().isEnum() || !KotlinDetector.isKotlinType(ctor.getDeclaringClass())) { return null; } try { KFunction<?> function = ReflectJvmMapping.getKotlinFunction(ctor); return (function != null ? getParameterNames(function.getParameters()) : null); } catch (UnsupportedOperationException ex) { return null; } }
@Override @Nullable public String[] getParameterNames(Method method) { if (!KotlinDetector.isKotlinType(method.getDeclaringClass())) { return null; } try { KFunction<?> function = ReflectJvmMapping.getKotlinFunction(method); return (function != null ? getParameterNames(function.getParameters()) : null); } catch (UnsupportedOperationException ex) { return null; } }
/** * 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); }
List<KParameter> parameters = function.getParameters(); KParameter parameter = parameters .stream()
List<KParameter> parameters = function.getParameters(); KParameter parameter = parameters .stream()
/** * @param publicCopyMethod Compiler-generated public {@code copy} method accepting all properties. * @param syntheticCopyMethod Compiler-generated synthetic {@code copy$default} variant of the copy method accepting * the original instance and defaulting masks. */ private KotlinCopyMethod(Method publicCopyMethod, Method syntheticCopyMethod) { this.publicCopyMethod = publicCopyMethod; this.syntheticCopyMethod = syntheticCopyMethod; this.copyFunction = ReflectJvmMapping.getKotlinFunction(publicCopyMethod); this.parameterCount = copyFunction.getParameters().size(); }
static int findIndex(KFunction<?> function, String parameterName) { for (KParameter parameter : function.getParameters()) { if (parameterName.equals(parameter.getName())) { return parameter.getIndex(); } } return -1; } }
@Override @Nullable public String[] getParameterNames(Constructor<?> ctor) { if (ctor.getDeclaringClass().isEnum() || !KotlinDetector.isKotlinType(ctor.getDeclaringClass())) { return null; } try { KFunction<?> function = ReflectJvmMapping.getKotlinFunction(ctor); return (function != null ? getParameterNames(function.getParameters()) : null); } catch (UnsupportedOperationException ex) { return null; } }
@Override @Nullable public String[] getParameterNames(Constructor<?> ctor) { if (ctor.getDeclaringClass().isEnum() || !KotlinDetector.isKotlinType(ctor.getDeclaringClass())) { return null; } try { KFunction<?> function = ReflectJvmMapping.getKotlinFunction(ctor); return (function != null ? getParameterNames(function.getParameters()) : null); } catch (UnsupportedOperationException ex) { return null; } }
@Override @Nullable public String[] getParameterNames(Method method) { if (!KotlinDetector.isKotlinType(method.getDeclaringClass())) { return null; } try { KFunction<?> function = ReflectJvmMapping.getKotlinFunction(method); return (function != null ? getParameterNames(function.getParameters()) : null); } catch (UnsupportedOperationException ex) { return null; } }
@Override @Nullable public String[] getParameterNames(Method method) { if (!KotlinDetector.isKotlinType(method.getDeclaringClass())) { return null; } try { KFunction<?> function = ReflectJvmMapping.getKotlinFunction(method); return (function != null ? getParameterNames(function.getParameters()) : null); } catch (UnsupportedOperationException ex) { return null; } }
@SuppressWarnings("unchecked") private static Optional<Method> findPublicCopyMethod(Method defaultKotlinMethod) { Class<?> type = defaultKotlinMethod.getDeclaringClass(); KClass<?> kotlinClass = JvmClassMappingKt.getKotlinClass(type); KFunction<?> primaryConstructor = KClasses.getPrimaryConstructor(kotlinClass); if (primaryConstructor == null) { return Optional.empty(); } List<KParameter> constructorArguments = primaryConstructor.getParameters() // .stream() // .filter(it -> it.getKind() == Kind.VALUE) // .collect(Collectors.toList()); return Arrays.stream(type.getDeclaredMethods()).filter(it -> it.getName().equals("copy") // && !it.isSynthetic() // && !Modifier.isStatic(it.getModifiers()) // && it.getReturnType().equals(type) // && it.getParameterCount() == constructorArguments.size()) // .filter(it -> { KFunction<?> kotlinFunction = ReflectJvmMapping.getKotlinFunction(it); if (kotlinFunction == null) { return false; } return parameterMatches(constructorArguments, kotlinFunction); }).findFirst(); }
int mask = 0; List<KParameter> parameters = function.getParameters();
KotlinCopyByProperty(KFunction<?> copyFunction, PersistentProperty<?> property) { this.parameterPosition = findIndex(copyFunction, property.getName()); this.parameterCount = copyFunction.getParameters().size(); this.defaultMask = KotlinDefaultMask.from(copyFunction, it -> property.getName().equals(it.getName())); }
DefaultingKotlinClassInstantiatorAdapter(ObjectInstantiator instantiator, PreferredConstructor<?, ?> constructor) { KFunction<?> kotlinConstructor = ReflectJvmMapping.getKotlinFunction(constructor.getConstructor()); if (kotlinConstructor == null) { throw new IllegalArgumentException( "No corresponding Kotlin constructor found for " + constructor.getConstructor()); } this.instantiator = instantiator; this.constructor = kotlinConstructor; this.kParameters = kotlinConstructor.getParameters(); this.synthetic = constructor.getConstructor(); }
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(); }