Autodetect Kotlin nullability for optional injection points (analogous to java.util.Optional)

Built-in support in MethodParameter and DependencyDescriptor supersedes our separate KotlinUtils helper.

Issue: SPR-14951
This commit is contained in:
Juergen Hoeller
2016-12-13 17:38:58 +01:00
parent 361ab6b621
commit 39d2769bd0
7 changed files with 257 additions and 150 deletions

View File

@@ -23,6 +23,11 @@ import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.Map;
import java.util.Optional;
import kotlin.Metadata;
import kotlin.reflect.KProperty;
import kotlin.reflect.jvm.ReflectJvmMapping;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
@@ -33,6 +38,7 @@ import org.springframework.core.GenericTypeResolver;
import org.springframework.core.MethodParameter;
import org.springframework.core.ParameterNameDiscoverer;
import org.springframework.core.ResolvableType;
import org.springframework.util.ClassUtils;
/**
* Descriptor for a specific dependency that is about to be injected.
@@ -45,6 +51,10 @@ import org.springframework.core.ResolvableType;
@SuppressWarnings("serial")
public class DependencyDescriptor extends InjectionPoint implements Serializable {
private static final boolean kotlinPresent =
ClassUtils.isPresent("kotlin.Unit", DependencyDescriptor.class.getClassLoader());
private final Class<?> declaringClass;
private String methodName;
@@ -83,6 +93,7 @@ public class DependencyDescriptor extends InjectionPoint implements Serializable
*/
public DependencyDescriptor(MethodParameter methodParameter, boolean required, boolean eager) {
super(methodParameter);
this.declaringClass = methodParameter.getDeclaringClass();
if (this.methodParameter.getMethod() != null) {
this.methodName = methodParameter.getMethod().getName();
@@ -116,6 +127,7 @@ public class DependencyDescriptor extends InjectionPoint implements Serializable
*/
public DependencyDescriptor(Field field, boolean required, boolean eager) {
super(field);
this.declaringClass = field.getDeclaringClass();
this.fieldName = field.getName();
this.required = required;
@@ -128,6 +140,7 @@ public class DependencyDescriptor extends InjectionPoint implements Serializable
*/
public DependencyDescriptor(DependencyDescriptor original) {
super(original);
this.declaringClass = original.declaringClass;
this.methodName = original.methodName;
this.parameterTypes = original.parameterTypes;
@@ -144,7 +157,17 @@ public class DependencyDescriptor extends InjectionPoint implements Serializable
* Return whether this dependency is required.
*/
public boolean isRequired() {
return this.required;
if (!this.required) {
return false;
}
if (this.field != null) {
return !(this.field.getType() == Optional.class ||
(kotlinPresent && KotlinDelegate.isNullable(this.field)));
}
else {
return !this.methodParameter.isOptional();
}
}
/**
@@ -398,4 +421,22 @@ public class DependencyDescriptor extends InjectionPoint implements Serializable
}
}
/**
* Inner class to avoid a hard dependency on Kotlin at runtime.
*/
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;
}
}
}

View File

@@ -0,0 +1,87 @@
/*
* Copyright 2002-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.beans.factory.annotation
import java.lang.reflect.Method
import org.junit.Before
import org.junit.Test
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor
import org.springframework.beans.factory.support.DefaultListableBeanFactory
import org.springframework.beans.factory.support.RootBeanDefinition
import org.springframework.tests.sample.beans.TestBean
import org.junit.Assert.*
/**
* Tests for Kotlin support with [@Autowired].
*
* @author Juergen Hoeller
*/
class KotlinAutowiredTests {
@Test
fun autowiringWithTarget() {
var bf = DefaultListableBeanFactory()
var bpp = AutowiredAnnotationBeanPostProcessor()
bpp.setBeanFactory(bf)
bf.addBeanPostProcessor(bpp)
var bd = RootBeanDefinition(KotlinBean::class.java)
bd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE)
bf.registerBeanDefinition("annotatedBean", bd)
var tb = TestBean()
bf.registerSingleton("testBean", tb)
var kb = bf.getBean("annotatedBean", KotlinBean::class.java)
assertSame(tb, kb.injectedFromConstructor)
assertSame(tb, kb.injectedFromMethod)
assertSame(tb, kb.injectedField)
}
@Test
fun autowiringWithoutTarget() {
var bf = DefaultListableBeanFactory()
var bpp = AutowiredAnnotationBeanPostProcessor()
bpp.setBeanFactory(bf)
bf.addBeanPostProcessor(bpp)
var bd = RootBeanDefinition(KotlinBean::class.java)
bd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE)
bf.registerBeanDefinition("annotatedBean", bd)
var kb = bf.getBean("annotatedBean", KotlinBean::class.java)
assertNull(kb.injectedFromConstructor)
assertNull(kb.injectedFromMethod)
assertNull(kb.injectedField)
}
class KotlinBean(val injectedFromConstructor: TestBean?) {
var injectedFromMethod: TestBean? = null
@Autowired
var injectedField: TestBean? = null
@Autowired
fun injectedMethod(p1: TestBean?) {
injectedFromMethod = p1
}
}
}