kotlin.reflect.KParameter Java Examples

The following examples show how to use kotlin.reflect.KParameter. 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
Source File: KotlinReflectionParameterNameDiscoverer.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@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 File: BeanUtils.java    From spring-analysis-note with 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
Source File: KotlinReflectionParameterNameDiscoverer.java    From java-technology-stack with MIT License 6 votes vote down vote up
@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 File: BeanUtils.java    From java-technology-stack with 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 File: TypeParser.java    From typescript-generator with 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);
}