Java Code Examples for kotlin.reflect.full.KClasses

The following examples show how to use kotlin.reflect.full.KClasses. 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
Source Project: spring-analysis-note   Source File: BeanUtils.java    License: MIT License 6 votes vote down vote up
/**
 * Retrieve the Java constructor corresponding to the Kotlin primary constructor, if any.
 * @param clazz the {@link Class} of the Kotlin class
 * @see <a href="https://kotlinlang.org/docs/reference/classes.html#constructors">
 * https://kotlinlang.org/docs/reference/classes.html#constructors</a>
 */
@Nullable
public static <T> Constructor<T> findPrimaryConstructor(Class<T> clazz) {
	try {
		KFunction<T> primaryCtor = KClasses.getPrimaryConstructor(JvmClassMappingKt.getKotlinClass(clazz));
		if (primaryCtor == null) {
			return null;
		}
		Constructor<T> constructor = ReflectJvmMapping.getJavaConstructor(primaryCtor);
		if (constructor == null) {
			throw new IllegalStateException(
					"Failed to find Java constructor for Kotlin primary constructor: " + clazz.getName());
		}
		return constructor;
	}
	catch (UnsupportedOperationException ex) {
		return null;
	}
}
 
Example 2
Source Project: java-technology-stack   Source File: BeanUtils.java    License: MIT License 6 votes vote down vote up
/**
 * Retrieve the Java constructor corresponding to the Kotlin primary constructor, if any.
 * @param clazz the {@link Class} of the Kotlin class
 * @see <a href="http://kotlinlang.org/docs/reference/classes.html#constructors">
 * http://kotlinlang.org/docs/reference/classes.html#constructors</a>
 */
@Nullable
public static <T> Constructor<T> findPrimaryConstructor(Class<T> clazz) {
	try {
		KFunction<T> primaryCtor = KClasses.getPrimaryConstructor(JvmClassMappingKt.getKotlinClass(clazz));
		if (primaryCtor == null) {
			return null;
		}
		Constructor<T> constructor = ReflectJvmMapping.getJavaConstructor(primaryCtor);
		if (constructor == null) {
			throw new IllegalStateException(
					"Failed to find Java constructor for Kotlin primary constructor: " + clazz.getName());
		}
		return constructor;
	}
	catch (UnsupportedOperationException ex) {
		return null;
	}
}
 
Example 3
Source Project: typescript-generator   Source File: TypeParser.java    License: 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);
}