@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; } }
if (method != null && index == -1) { KFunction<?> function = ReflectJvmMapping.getKotlinFunction(method); return (function != null && function.getReturnType().isMarkedNullable()); List<KParameter> parameters = function.getParameters(); KParameter parameter = parameters .stream()
/** * 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); }
/** * Checks rule event condition by evaluating the defined knowledge base rule method. * * @param rule rule. * @param event event. * @return {@code true} if this condition is met. */ @Override public boolean condition(Rule rule, Event event) { return function.call(rule, event); } }
public static String createEventConditionMethodName(KFunction<Boolean> kotlinObject) { return kotlinObject.getName(); }
@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; } }
if (method != null && index == -1) { KFunction<?> function = ReflectJvmMapping.getKotlinFunction(method); return (function != null && function.getReturnType().isMarkedNullable()); List<KParameter> parameters = function.getParameters(); KParameter parameter = parameters .stream()
/** * 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(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; } }
/** * 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(); }
@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; } }
/** * 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; }
/** * @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(); }
if (method != null && index == -1) { KFunction<?> function = ReflectJvmMapping.getKotlinFunction(method); return (function != null && function.getReturnType().isMarkedNullable()); List<KParameter> parameters = function.getParameters(); KParameter parameter = parameters .stream()
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; } }
@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(); }
@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; } }