Override/Implement: Prefer not-nullable return type when overriding Java method without nullability annotation

#KT-12381 Fixed
This commit is contained in:
Alexey Sedunov
2016-06-22 13:15:29 +03:00
parent 95239e59c0
commit 2b87f8ebe3
10 changed files with 35 additions and 7 deletions

View File

@@ -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

View File

@@ -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)

View File

@@ -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>
}
}

View File

@@ -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>
}
}

View File

@@ -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>
}
}

View File

@@ -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>
}
}

View File

@@ -0,0 +1,7 @@
package foo;
public class A {
public String foo(String s) {
return null;
}
}

View File

@@ -0,0 +1,5 @@
import foo.A
class B : A() {
<caret>
}

View File

@@ -0,0 +1,7 @@
import foo.A
class B : A() {
override fun foo(s: String?): String {
<selection><caret>return super.foo(s)</selection>
}
}

View File

@@ -243,4 +243,8 @@ class OverrideImplementTest : AbstractOverrideImplementTest() {
fun testConvertJavaDoc() {
doOverrideDirectoryTest("foo")
}
fun testPlatformTypes() {
doOverrideDirectoryTest("foo")
}
}