Java Code Examples for kotlin.reflect.jvm.ReflectJvmMapping#getKotlinFunction()

The following examples show how to use kotlin.reflect.jvm.ReflectJvmMapping#getKotlinFunction() . 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: 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 2
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 3
Source File: ContextUpdater.java    From zeppelin with Apache License 2.0 6 votes vote down vote up
private void refreshMethods(List<Object> lines) {
  functions.clear();
  for (Object line : lines) {
    Method[] methods = line.getClass().getMethods();
    for (Method method : methods) {
      if (objectMethods.contains(method) || method.getName().equals("main")) {
        continue;
      }
      KFunction<?> function = ReflectJvmMapping.getKotlinFunction(method);
      if (function == null) {
        continue;
      }
      functions.add(new KotlinFunctionInfo(function));
    }
  }
}
 
Example 4
Source File: TypeParser.java    From typescript-generator with MIT License 6 votes vote down vote up
@Override
public Type getMethodReturnType(Method method) {
    final KFunction<?> kFunction = ReflectJvmMapping.getKotlinFunction(method);
    if (kFunction != null) {
        return getType(kFunction.getReturnType(), new LinkedHashMap<>());
    } else {
        // `method` might be a getter so try to find a corresponding field and pass it to Kotlin reflection
        final KClass<?> kClass = JvmClassMappingKt.getKotlinClass(method.getDeclaringClass());
        final Optional<Field> field = KClasses.getMemberProperties(kClass).stream()
                .filter(kProperty -> Objects.equals(ReflectJvmMapping.getJavaGetter(kProperty), method))
                .map(kProperty -> ReflectJvmMapping.getJavaField(kProperty))
                .filter(Objects::nonNull)
                .findFirst();
        if (field.isPresent()) {
            return getFieldType(field.get());
        }
    }
    return javaTypeParser.getMethodReturnType(method);
}
 
Example 5
Source File: AbstractEncoderMethodReturnValueHandler.java    From spring-analysis-note with MIT License 5 votes vote down vote up
static private boolean isSuspend(@Nullable Method method) {
	if (method == null) {
		return false;
	}
	KFunction<?> function = ReflectJvmMapping.getKotlinFunction(method);
	return (function != null && function.isSuspend());
}
 
Example 6
Source File: MethodParameter.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Return the generic return type of the method, with support of suspending
 * functions via Kotlin reflection.
 */
static private Type getGenericReturnType(Method method) {
	KFunction<?> function = ReflectJvmMapping.getKotlinFunction(method);
	if (function != null && function.isSuspend()) {
		return ReflectJvmMapping.getJavaType(function.getReturnType());
	}
	return method.getGenericReturnType();
}
 
Example 7
Source File: MethodParameter.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Return the return type of the method, with support of suspending
 * functions via Kotlin reflection.
 */
static private Class<?> getReturnType(Method method) {
	KFunction<?> function = ReflectJvmMapping.getKotlinFunction(method);
	if (function != null && function.isSuspend()) {
		Type paramType = ReflectJvmMapping.getJavaType(function.getReturnType());
		Class<?> paramClass = ResolvableType.forType(paramType).resolve();
		Assert.notNull(paramClass, "Type " + paramType + "can't be resolved to a class");
		return paramClass;
	}
	return method.getReturnType();
}
 
Example 8
Source File: KotlinReflectionParameterNameDiscoverer.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@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;
	}
}
 
Example 9
Source File: KotlinReflectionParameterNameDiscoverer.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@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;
	}
}
 
Example 10
Source File: KotlinReflectionParameterNameDiscoverer.java    From java-technology-stack with MIT License 5 votes vote down vote up
@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;
	}
}
 
Example 11
Source File: KotlinReflectionParameterNameDiscoverer.java    From java-technology-stack with MIT License 5 votes vote down vote up
@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;
	}
}
 
Example 12
Source File: AbstractMessageWriterResultHandler.java    From spring-analysis-note with MIT License 4 votes vote down vote up
static private boolean isSuspend(Method method) {
	KFunction<?> function = ReflectJvmMapping.getKotlinFunction(method);
	return function != null && function.isSuspend();
}
 
Example 13
Source File: TypeParser.java    From typescript-generator with MIT License 4 votes vote down vote up
@Override
public List<Type> getMethodParameterTypes(Method method) {
    final KFunction<?> kFunction = ReflectJvmMapping.getKotlinFunction(method);
    return getKFunctionParameterTypes(method, kFunction);
}
 
Example 14
Source File: TypeParser.java    From typescript-generator with MIT License 4 votes vote down vote up
@Override
public List<Type> getConstructorParameterTypes(Constructor<?> constructor) {
    final KFunction<?> kFunction = ReflectJvmMapping.getKotlinFunction(constructor);
    return getKFunctionParameterTypes(constructor, kFunction);
}