Skip to content

Commit

Permalink
Efficient Kotlin metadata detection
Browse files Browse the repository at this point in the history
Follow-up of 3991ab4.

Issue: SPR-15673
  • Loading branch information
sdeleuze committed Aug 22, 2017
1 parent cea9d1d commit ab64305
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 53 deletions.
Expand Up @@ -71,16 +71,18 @@ public abstract class BeanUtils {
Collections.newSetFromMap(new ConcurrentReferenceHashMap<>(64));

@Nullable
private static Class<?> kotlinMetadata;
private static final Class<?> kotlinMetadata;

static {
Class<?> metadata;
try {
kotlinMetadata = ClassUtils.forName("kotlin.Metadata", BeanUtils.class.getClassLoader());
metadata = ClassUtils.forName("kotlin.Metadata", BeanUtils.class.getClassLoader());
}
catch (ClassNotFoundException ex) {
// Kotlin API not available - no special support for Kotlin class instantiation
kotlinMetadata = null;
metadata = null;
}
kotlinMetadata = metadata;
}


Expand Down Expand Up @@ -125,7 +127,7 @@ public static <T> T instantiateClass(Class<T> clazz) throws BeanInstantiationExc
throw new BeanInstantiationException(clazz, "Specified class is an interface");
}
try {
Constructor<T> ctor = (isKotlinClass(clazz) ?
Constructor<T> ctor = (useKotlinSupport(clazz) ?
KotlinDelegate.findPrimaryConstructor(clazz) : clazz.getDeclaredConstructor());
if (ctor == null) {
throw new BeanInstantiationException(clazz, "No default constructor found");
Expand Down Expand Up @@ -172,7 +174,7 @@ public static <T> T instantiateClass(Constructor<T> ctor, Object... args) throws
Assert.notNull(ctor, "Constructor must not be null");
try {
ReflectionUtils.makeAccessible(ctor);
return (isKotlinClass(ctor.getDeclaringClass()) ?
return (useKotlinSupport(ctor.getDeclaringClass()) ?
KotlinDelegate.instantiateClass(ctor, args) : ctor.newInstance(args));
}
catch (InstantiationException ex) {
Expand Down Expand Up @@ -340,7 +342,7 @@ else if (!method.isBridge() && targetMethod.getParameterCount() == numParams) {
@Nullable
public static <T> Constructor<T> findPrimaryConstructor(Class<T> clazz) {
Assert.notNull(clazz, "Class must not be null");
if (isKotlinClass(clazz)) {
if (useKotlinSupport(clazz)) {
return KotlinDelegate.findPrimaryConstructor(clazz);
}
else {
Expand Down Expand Up @@ -707,10 +709,10 @@ private static void copyProperties(Object source, Object target, @Nullable Class
}

/**
* Return true if the specified class is a Kotlin one.
* Return true if Kotlin is present and if the specified class is a Kotlin one.
*/
@SuppressWarnings("unchecked")
private static boolean isKotlinClass(Class<?> clazz) {
private static boolean useKotlinSupport(Class<?> clazz) {
return (kotlinMetadata != null &&
clazz.getDeclaredAnnotation((Class<? extends Annotation>) kotlinMetadata) != null);
}
Expand Down
Expand Up @@ -26,7 +26,6 @@
import java.util.Map;
import java.util.Optional;

import kotlin.Metadata;
import kotlin.reflect.KProperty;
import kotlin.reflect.jvm.ReflectJvmMapping;

Expand All @@ -52,8 +51,20 @@
@SuppressWarnings("serial")
public class DependencyDescriptor extends InjectionPoint implements Serializable {

private static final boolean kotlinPresent =
ClassUtils.isPresent("kotlin.Metadata", DependencyDescriptor.class.getClassLoader());
@Nullable
private static final Class<?> kotlinMetadata;

static {
Class<?> metadata;
try {
metadata = ClassUtils.forName("kotlin.Metadata", DependencyDescriptor.class.getClassLoader());
}
catch (ClassNotFoundException ex) {
// Kotlin API not available - no Kotlin support
metadata = null;
}
kotlinMetadata = metadata;
}


private final Class<?> declaringClass;
Expand Down Expand Up @@ -172,13 +183,22 @@ public boolean isRequired() {

if (this.field != null) {
return !(this.field.getType() == Optional.class || hasNullableAnnotation() ||
(kotlinPresent && KotlinDelegate.isNullable(this.field)));
(useKotlinSupport(this.field.getDeclaringClass()) && KotlinDelegate.isNullable(this.field)));
}
else {
return !obtainMethodParameter().isOptional();
}
}

/**
* Return true if Kotlin is present and if the specified class is a Kotlin one.
*/
@SuppressWarnings("unchecked")
private static boolean useKotlinSupport(Class<?> clazz) {
return (kotlinMetadata != null &&
clazz.getDeclaredAnnotation((Class<? extends Annotation>) kotlinMetadata) != null);
}

/**
* Check whether the underlying field is annotated with any variant of a
* {@code Nullable} annotation, e.g. {@code javax.annotation.Nullable} or
Expand Down Expand Up @@ -435,11 +455,8 @@ private static class KotlinDelegate {
* Check whether the specified {@link Field} represents a nullable Kotlin type or not.
*/
public static boolean isNullable(Field field) {
if (field.getDeclaringClass().isAnnotationPresent(Metadata.class)) {
KProperty<?> property = ReflectJvmMapping.getKotlinProperty(field);
return (property != null && property.getReturnType().isMarkedNullable());
}
return false;
KProperty<?> property = ReflectJvmMapping.getKotlinProperty(field);
return (property != null && property.getReturnType().isMarkedNullable());
}
}

Expand Down
Expand Up @@ -58,8 +58,20 @@
*/
public class MethodParameter {

private static final boolean kotlinPresent =
ClassUtils.isPresent("kotlin.Metadata", MethodParameter.class.getClassLoader());
@Nullable
private static final Class<?> kotlinMetadata;

static {
Class<?> metadata;
try {
metadata = ClassUtils.forName("kotlin.Metadata", MethodParameter.class.getClassLoader());
}
catch (ClassNotFoundException ex) {
// Kotlin API not available - no Kotlin support
metadata = null;
}
kotlinMetadata = metadata;
}


private final Executable executable;
Expand Down Expand Up @@ -341,7 +353,16 @@ public MethodParameter nested() {
*/
public boolean isOptional() {
return (getParameterType() == Optional.class || hasNullableAnnotation() ||
(kotlinPresent && KotlinDelegate.isNullable(this)));
(useKotlinSupport(this.getContainingClass()) && KotlinDelegate.isNullable(this)));
}

/**
* Return true if Kotlin is present and if the specified class is a Kotlin one.
*/
@SuppressWarnings("unchecked")
private static boolean useKotlinSupport(Class<?> clazz) {
return (kotlinMetadata != null &&
clazz.getDeclaredAnnotation((Class<? extends Annotation>) kotlinMetadata) != null);
}

/**
Expand Down Expand Up @@ -736,41 +757,30 @@ private static class KotlinDelegate {
* Check whether the specified {@link MethodParameter} represents a nullable Kotlin type or not.
*/
public static boolean isNullable(MethodParameter param) {
if (isKotlinClass(param.getContainingClass())) {
Method method = param.getMethod();
Constructor<?> ctor = param.getConstructor();
int index = param.getParameterIndex();
if (method != null && index == -1) {
KFunction<?> function = ReflectJvmMapping.getKotlinFunction(method);
return (function != null && function.getReturnType().isMarkedNullable());
Method method = param.getMethod();
Constructor<?> ctor = param.getConstructor();
int index = param.getParameterIndex();
if (method != null && index == -1) {
KFunction<?> function = ReflectJvmMapping.getKotlinFunction(method);
return (function != null && function.getReturnType().isMarkedNullable());
}
else {
KFunction<?> function = null;
if (method != null) {
function = ReflectJvmMapping.getKotlinFunction(method);
}
else {
KFunction<?> function = null;
if (method != null) {
function = ReflectJvmMapping.getKotlinFunction(method);
}
else if (ctor != null) {
function = ReflectJvmMapping.getKotlinFunction(ctor);
}
if (function != null) {
List<KParameter> parameters = function.getParameters();
return parameters
.stream()
.filter(p -> KParameter.Kind.VALUE.equals(p.getKind()))
.collect(Collectors.toList())
.get(index)
.getType()
.isMarkedNullable();
}
else if (ctor != null) {
function = ReflectJvmMapping.getKotlinFunction(ctor);
}
}
return false;
}

private static boolean isKotlinClass(Class<?> clazz) {
for (Annotation annotation : clazz.getDeclaredAnnotations()) {
if (annotation.annotationType().getName().equals("kotlin.Metadata")) {
return true;
if (function != null) {
List<KParameter> parameters = function.getParameters();
return parameters
.stream()
.filter(p -> KParameter.Kind.VALUE.equals(p.getKind()))
.collect(Collectors.toList())
.get(index)
.getType()
.isMarkedNullable();
}
}
return false;
Expand Down

0 comments on commit ab64305

Please sign in to comment.