kotlin.reflect.KFunction Java Examples
The following examples show how to use
kotlin.reflect.KFunction.
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: TypeParser.java From typescript-generator with MIT License | 6 votes |
@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 #2
Source File: ContextUpdater.java From zeppelin with Apache License 2.0 | 6 votes |
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 #3
Source File: BeanUtils.java From java-technology-stack with MIT License | 6 votes |
/** * 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 #4
Source File: BeanUtils.java From java-technology-stack with MIT License | 6 votes |
/** * 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 #5
Source File: BeanUtils.java From spring-analysis-note with MIT License | 6 votes |
/** * 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 #6
Source File: BeanUtils.java From spring-analysis-note with MIT License | 6 votes |
/** * 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 #7
Source File: TypeParser.java From typescript-generator with MIT License | 5 votes |
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); }
Example #8
Source File: AbstractEncoderMethodReturnValueHandler.java From spring-analysis-note with MIT License | 5 votes |
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 File: KotlinReflectionParameterNameDiscoverer.java From java-technology-stack with MIT License | 5 votes |
@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 |
@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 spring-analysis-note with MIT License | 5 votes |
@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: KotlinReflectionParameterNameDiscoverer.java From spring-analysis-note with MIT License | 5 votes |
@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 #13
Source File: MethodParameter.java From spring-analysis-note with MIT License | 5 votes |
/** * 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 #14
Source File: MethodParameter.java From spring-analysis-note with MIT License | 5 votes |
/** * 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 #15
Source File: KotlinReflectUtil.java From zeppelin with Apache License 2.0 | 4 votes |
public static String functionSignature(KFunction<?> function) { return function.toString().replaceAll("Line_\\d+\\.", ""); }
Example #16
Source File: KotlinFunctionInfo.java From zeppelin with Apache License 2.0 | 4 votes |
public KotlinFunctionInfo(KFunction<?> function) { this.function = function; }
Example #17
Source File: KotlinFunctionInfo.java From zeppelin with Apache License 2.0 | 4 votes |
public KFunction<?> getFunction() { return function; }
Example #18
Source File: TypeParser.java From typescript-generator with MIT License | 4 votes |
@Override public List<Type> getMethodParameterTypes(Method method) { final KFunction<?> kFunction = ReflectJvmMapping.getKotlinFunction(method); return getKFunctionParameterTypes(method, kFunction); }
Example #19
Source File: TypeParser.java From typescript-generator with MIT License | 4 votes |
@Override public List<Type> getConstructorParameterTypes(Constructor<?> constructor) { final KFunction<?> kFunction = ReflectJvmMapping.getKotlinFunction(constructor); return getKFunctionParameterTypes(constructor, kFunction); }
Example #20
Source File: AbstractMessageWriterResultHandler.java From spring-analysis-note with MIT License | 4 votes |
static private boolean isSuspend(Method method) { KFunction<?> function = ReflectJvmMapping.getKotlinFunction(method); return function != null && function.isSuspend(); }