Polishing.

Guard usage of KotlinReflectUtils with type presence check.
Extend tests to cover primitive arrays.
Move methods from KotlinValueUtils to KotlinReflectUtils.
Move copy value cache to KotlinCopyMethod.

Original Pull Request: #2866
This commit is contained in:
Christoph Strobl
2023-06-29 14:14:57 +02:00
parent 80ca2b47ca
commit 09281c7182
10 changed files with 149 additions and 53 deletions

View File

@@ -37,7 +37,7 @@ import org.springframework.lang.Nullable;
/**
* Reflection utility methods specific to Kotlin reflection. Requires Kotlin classes to be present to avoid linkage
* errors.
* errors - ensure to guard usage with {@link KotlinDetector#isKotlinPresent()}.
*
* @author Mark Paluch
* @author Christoph Strobl
@@ -132,6 +132,42 @@ public final class KotlinReflectionUtils {
return JvmClassMappingKt.getJavaClass(KTypesJvm.getJvmErasure(kotlinFunction.getReturnType()));
}
/**
* Returns whether the given {@link KType} is a {@link KClass#isValue() value} class.
*
* @param type the kotlin type to inspect.
* @return {@code true} the type is a value class.
* @since 3.2
*/
public static boolean isValueClass(KType type) {
return type.getClassifier() instanceof KClass<?> kc && kc.isValue();
}
/**
* Returns whether the given class makes uses Kotlin {@link KClass#isValue() value} classes.
*
* @param type the kotlin type to inspect.
* @return {@code true} when at least one property uses Kotlin value classes.
* @since 3.2
*/
public static boolean hasValueClassProperty(Class<?> type) {
if (!KotlinDetector.isKotlinType(type)) {
return false;
}
KClass<?> kotlinClass = JvmClassMappingKt.getKotlinClass(type);
for (KCallable<?> member : kotlinClass.getMembers()) {
if (member instanceof KProperty<?> kp && isValueClass(kp.getReturnType())) {
return true;
}
}
return false;
}
/**
* Returns {@literal} whether the given {@link MethodParameter} is nullable. Its declaring method can reference a
* Kotlin function, property or interface property.

View File

@@ -21,6 +21,7 @@ import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.function.Predicate;
import org.springframework.core.KotlinDetector;
import org.springframework.util.Assert;
/**
@@ -45,7 +46,7 @@ public interface Predicates {
Predicate<Member> IS_PUBLIC = member -> Modifier.isPublic(member.getModifiers());
Predicate<Member> IS_SYNTHETIC = Member::isSynthetic;
Predicate<Class<?>> IS_KOTLIN = KotlinReflectionUtils::isSupportedKotlinClass;
Predicate<Class<?>> IS_KOTLIN = KotlinDetector.isKotlinPresent() ? KotlinReflectionUtils::isSupportedKotlinClass : type -> false;
Predicate<Member> IS_STATIC = member -> Modifier.isStatic(member.getModifiers());
Predicate<Method> IS_BRIDGE_METHOD = Method::isBridge;