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

@@ -0,0 +1,67 @@
/*
* 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.core
import java.lang.reflect.Method
import org.junit.Before
import org.junit.Test
import org.junit.Assert.*
/**
* Tests for Kotlin support in [MethodParameter].
*
* @author Raman Gupta
* @author Sebastien Deleuze
* @author Juergen Hoeller
*/
class KotlinMethodParameterTests {
lateinit var nullableMethod: Method
lateinit var nonNullableMethod: Method
@Before
@Throws(NoSuchMethodException::class)
fun setup() {
nullableMethod = javaClass.getMethod("nullable", String::class.java)
nonNullableMethod = javaClass.getMethod("nonNullable", String::class.java)
}
@Test
fun `Method parameter nullability`() {
assertTrue(MethodParameter(nullableMethod, 0).isOptional())
assertFalse(MethodParameter(nonNullableMethod, 0).isOptional())
}
@Test
fun `Method return type nullability`() {
assertTrue(MethodParameter(nullableMethod, -1).isOptional())
assertFalse(MethodParameter(nonNullableMethod, -1).isOptional())
}
@Suppress("unused", "unused_parameter")
fun nullable(p1: String?): Int? = 42
@Suppress("unused", "unused_parameter")
fun nonNullable(p1: String): Int = 42
}

View File

@@ -1,62 +0,0 @@
package org.springframework.util
import org.junit.Before
import org.junit.Test
import java.lang.reflect.Method
import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
import org.springframework.core.MethodParameter
import org.springframework.util.KotlinUtils.*
/**
* Test fixture for [KotlinUtils].
*
* @author Raman Gupta
* @author Sebastien Deleuze
*/
class KotlinUtilsTests {
lateinit var nullableMethod: Method
lateinit var nonNullableMethod: Method
@Before
@Throws(NoSuchMethodException::class)
fun setup() {
nullableMethod = javaClass.getMethod("nullable", String::class.java)
nonNullableMethod = javaClass.getMethod("nonNullable", String::class.java)
}
@Test
fun `Is kotlin present`() {
// we'd have to change the build to test the opposite
assertTrue(isKotlinPresent())
}
@Test
fun `Are kotlin classes detected`() {
assertFalse(isKotlinClass(MethodParameter::class.java))
assertTrue(isKotlinClass(javaClass))
}
@Test
fun `Obtains method return type nullability`() {
assertTrue(isNullable(MethodParameter(nullableMethod, -1)))
assertFalse(isNullable(MethodParameter(nonNullableMethod, -1)))
}
@Test
fun `Obtains method parameter nullability`() {
assertTrue(isNullable(MethodParameter(nullableMethod, 0)))
assertFalse(isNullable(MethodParameter(nonNullableMethod, 0)))
}
@Suppress("unused", "unused_parameter")
fun nullable(p1: String?): Int? = 42
@Suppress("unused", "unused_parameter")
fun nonNullable(p1: String): Int = 42
}