Java Code Examples for kotlin.reflect.jvm.ReflectJvmMapping

The following examples show how to use kotlin.reflect.jvm.ReflectJvmMapping. 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: 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
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 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: zeppelin   Source File: ContextUpdater.java    License: 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 6
Source Project: zeppelin   Source File: ContextUpdater.java    License: Apache License 2.0 6 votes vote down vote up
private void findVariables(Field[] fields, Object o) throws IllegalAccessException {
  for (Field field : fields) {
    String fieldName = field.getName();
    if (fieldName.contains("$$implicitReceiver")) {
      continue;
    }

    field.setAccessible(true);
    Object value = field.get(o);
    if (!fieldName.contains("script$")) {
      KProperty<?> descriptor = ReflectJvmMapping.getKotlinProperty(field);
      if (descriptor != null) {
        vars.putIfAbsent(fieldName, new KotlinVariableInfo(value, descriptor));
      }
    }
  }
}
 
Example 7
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);
}
 
Example 8
static private boolean isSuspend(@Nullable Method method) {
	if (method == null) {
		return false;
	}
	KFunction<?> function = ReflectJvmMapping.getKotlinFunction(method);
	return (function != null && function.isSuspend());
}
 
Example 9
Source Project: spring-analysis-note   Source File: MethodParameter.java    License: 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 10
Source Project: spring-analysis-note   Source File: MethodParameter.java    License: 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 11
@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 12
@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 13
@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 14
@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 15
Source Project: typescript-generator   Source File: TypeParser.java    License: MIT License 5 votes vote down vote up
@Override
public Type getFieldType(Field field) {
    final KProperty<?> kProperty = ReflectJvmMapping.getKotlinProperty(field);
    if (kProperty != null) {
        return getType(kProperty.getReturnType(), new LinkedHashMap<>());
    }
    return javaTypeParser.getFieldType(field);
}
 
Example 16
Source Project: typescript-generator   Source File: TypeParser.java    License: MIT License 5 votes vote down vote up
private TypeVariable<?> getJavaTypeVariable(KType kType) {
    try {
        final Type javaType = ReflectJvmMapping.getJavaType(kType);
        if (javaType instanceof TypeVariable) {
            return (TypeVariable<?>) javaType;
        } else {
            return null;
        }
    } catch (Throwable e) {
        return null;
    }
}
 
Example 17
static private boolean isSuspend(Method method) {
	KFunction<?> function = ReflectJvmMapping.getKotlinFunction(method);
	return function != null && function.isSuspend();
}
 
Example 18
Source Project: spring-analysis-note   Source File: DependencyDescriptor.java    License: MIT License 4 votes vote down vote up
/**
 * Check whether the specified {@link Field} represents a nullable Kotlin type or not.
 */
public static boolean isNullable(Field field) {
	KProperty<?> property = ReflectJvmMapping.getKotlinProperty(field);
	return (property != null && property.getReturnType().isMarkedNullable());
}
 
Example 19
Source Project: java-technology-stack   Source File: DependencyDescriptor.java    License: MIT License 4 votes vote down vote up
/**
 * Check whether the specified {@link Field} represents a nullable Kotlin type or not.
 */
public static boolean isNullable(Field field) {
	KProperty<?> property = ReflectJvmMapping.getKotlinProperty(field);
	return (property != null && property.getReturnType().isMarkedNullable());
}
 
Example 20
Source Project: typescript-generator   Source File: TypeParser.java    License: 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 21
Source Project: typescript-generator   Source File: TypeParser.java    License: 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);
}