mirror of
https://github.com/jlengrand/kotlin.git
synced 2026-04-07 00:21:37 +00:00
Override/Implement: Prefer not-nullable return type when overriding Java method without nullability annotation
#KT-12381 Fixed
This commit is contained in:
@@ -392,6 +392,7 @@
|
||||
- [`KT-11328`](https://youtrack.jetbrains.com/issue/KT-11328) "New Kotlin class": generates packages when fully qualified name is specified
|
||||
- [`KT-11778`](https://youtrack.jetbrains.com/issue/KT-11778) Exception in Lombok plugin: Rewrite at slice FUNCTION
|
||||
- [`KT-11708`](https://youtrack.jetbrains.com/issue/KT-11708) "Go to declaration" doesn't work on a call to function with SAM conversion on a derived type
|
||||
- [`KT-12381`](https://youtrack.jetbrains.com/issue/KT-12381) Prefer not-nullable return type when overriding Java method without nullability annotation
|
||||
|
||||
### Reflection
|
||||
|
||||
|
||||
@@ -35,6 +35,7 @@ import org.jetbrains.kotlin.psi.*
|
||||
import org.jetbrains.kotlin.psi.findDocComment.findDocComment
|
||||
import org.jetbrains.kotlin.renderer.*
|
||||
import org.jetbrains.kotlin.resolve.descriptorUtil.setSingleOverridden
|
||||
import org.jetbrains.kotlin.types.typeUtil.makeNotNullable
|
||||
|
||||
interface OverrideMemberChooserObject : ClassMember {
|
||||
enum class BodyType {
|
||||
@@ -155,9 +156,12 @@ private fun generateConstructorParameter(project: Project, descriptor: PropertyD
|
||||
}
|
||||
|
||||
private fun generateFunction(project: Project, descriptor: FunctionDescriptor, bodyType: OverrideMemberChooserObject.BodyType): KtNamedFunction {
|
||||
val newDescriptor = descriptor.copy(descriptor.containingDeclaration, Modality.OPEN, descriptor.visibility,
|
||||
descriptor.kind, /* copyOverrides = */ true)
|
||||
newDescriptor.setSingleOverridden(descriptor)
|
||||
val newDescriptor = object : FunctionDescriptor by descriptor {
|
||||
override fun getModality() = Modality.OPEN
|
||||
override fun getReturnType() = descriptor.returnType?.makeNotNullable()
|
||||
override fun getOverriddenDescriptors() = listOf(descriptor)
|
||||
override fun <R : Any?, D : Any?> accept(visitor: DeclarationDescriptorVisitor<R, D>, data: D) = visitor.visitFunctionDescriptor(this, data)
|
||||
}
|
||||
|
||||
val returnType = descriptor.returnType
|
||||
val returnsNotUnit = returnType != null && !KotlinBuiltIns.isUnit(returnType)
|
||||
|
||||
@@ -2,7 +2,7 @@ import foo.A
|
||||
import foo.B
|
||||
|
||||
class C : A() {
|
||||
override fun foo(x: MutableList<Any?>?, y: String?): B<*>? {
|
||||
override fun foo(x: MutableList<Any?>?, y: String?): B<*> {
|
||||
<selection><caret>throw UnsupportedOperationException("not implemented") //To change body of created functions use File | Settings | File Templates.</selection>
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import foo.Intf
|
||||
|
||||
class Impl(): Intf {
|
||||
override fun getFooBar(): String? {
|
||||
override fun getFooBar(): String {
|
||||
<selection><caret>throw UnsupportedOperationException("not implemented") //To change body of created functions use File | Settings | File Templates.</selection>
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@ package foo
|
||||
import foo.Intf
|
||||
|
||||
class Impl(): Intf() {
|
||||
override fun getFooBar(): String? {
|
||||
override fun getFooBar(): String {
|
||||
<selection><caret>return super.getFooBar()</selection>
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@ package foo
|
||||
import foo.Intf
|
||||
|
||||
class Impl(): Intf() {
|
||||
override fun getFooBar(): String? {
|
||||
override fun getFooBar(): String {
|
||||
<selection><caret>return super.getFooBar()</selection>
|
||||
}
|
||||
}
|
||||
|
||||
7
idea/testData/codeInsight/overrideImplement/platformTypes/foo/A.java
vendored
Normal file
7
idea/testData/codeInsight/overrideImplement/platformTypes/foo/A.java
vendored
Normal file
@@ -0,0 +1,7 @@
|
||||
package foo;
|
||||
|
||||
public class A {
|
||||
public String foo(String s) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
5
idea/testData/codeInsight/overrideImplement/platformTypes/foo/Impl.kt
vendored
Normal file
5
idea/testData/codeInsight/overrideImplement/platformTypes/foo/Impl.kt
vendored
Normal file
@@ -0,0 +1,5 @@
|
||||
import foo.A
|
||||
|
||||
class B : A() {
|
||||
<caret>
|
||||
}
|
||||
7
idea/testData/codeInsight/overrideImplement/platformTypes/foo/Impl.kt.after
vendored
Normal file
7
idea/testData/codeInsight/overrideImplement/platformTypes/foo/Impl.kt.after
vendored
Normal file
@@ -0,0 +1,7 @@
|
||||
import foo.A
|
||||
|
||||
class B : A() {
|
||||
override fun foo(s: String?): String {
|
||||
<selection><caret>return super.foo(s)</selection>
|
||||
}
|
||||
}
|
||||
@@ -243,4 +243,8 @@ class OverrideImplementTest : AbstractOverrideImplementTest() {
|
||||
fun testConvertJavaDoc() {
|
||||
doOverrideDirectoryTest("foo")
|
||||
}
|
||||
|
||||
fun testPlatformTypes() {
|
||||
doOverrideDirectoryTest("foo")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user