Java Code Examples for kotlin.reflect.KParameter

The following examples show how to use kotlin.reflect.KParameter. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
@Nullable
private String[] getParameterNames(List<KParameter> parameters) {
	List<KParameter> filteredParameters = parameters
			.stream()
			// Extension receivers of extension methods must be included as they appear as normal method parameters in Java
			.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;
}
 
Example 2
Source Project: spring-analysis-note   Source File: BeanUtils.java    License: MIT License 6 votes vote down vote up
/**
 * 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);
}
 
Example 3
@Nullable
private String[] getParameterNames(List<KParameter> parameters) {
	List<KParameter> filteredParameters = parameters
			.stream()
			// Extension receivers of extension methods must be included as they appear as normal method parameters in Java
			.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;
}
 
Example 4
Source Project: java-technology-stack   Source File: BeanUtils.java    License: MIT License 6 votes vote down vote up
/**
 * 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);
}
 
Example 5
Source Project: typescript-generator   Source File: TypeParser.java    License: MIT License 5 votes vote down vote up
private List<Type> getKFunctionParameterTypes(Executable executable, KFunction<?> kFunction) {
    if (kFunction != null) {
        final List<KParameter> kParameters = kFunction.getParameters().stream()
                .filter(kParameter -> kParameter.getKind() == KParameter.Kind.VALUE)
                .collect(Collectors.toList());
        return getTypes(
                kParameters.stream()
                        .map(parameter -> parameter.getType())
                        .collect(Collectors.toList()),
                new LinkedHashMap<>()
        );
    }
    return javaTypeParser.getExecutableParameterTypes(executable);
}