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

#KT-12381 Fixed
(cherry picked from commit 2b87f8e)
This commit is contained in:
Alexey Sedunov
2016-06-22 13:15:29 +03:00
parent 542f8d9fd2
commit f9d28bf76d
10 changed files with 35 additions and 7 deletions
+1
View File
@@ -395,6 +395,7 @@
- [`KT-11328`](https://youtrack.jetbrains.com/issue/KT-11328) "New Kotlin class": generates packages when fully qualified name is specified - [`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-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-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 ### Reflection
@@ -35,6 +35,7 @@ import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.findDocComment.findDocComment import org.jetbrains.kotlin.psi.findDocComment.findDocComment
import org.jetbrains.kotlin.renderer.* import org.jetbrains.kotlin.renderer.*
import org.jetbrains.kotlin.resolve.descriptorUtil.setSingleOverridden import org.jetbrains.kotlin.resolve.descriptorUtil.setSingleOverridden
import org.jetbrains.kotlin.types.typeUtil.makeNotNullable
interface OverrideMemberChooserObject : ClassMember { interface OverrideMemberChooserObject : ClassMember {
enum class BodyType { 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 { private fun generateFunction(project: Project, descriptor: FunctionDescriptor, bodyType: OverrideMemberChooserObject.BodyType): KtNamedFunction {
val newDescriptor = descriptor.copy(descriptor.containingDeclaration, Modality.OPEN, descriptor.visibility, val newDescriptor = object : FunctionDescriptor by descriptor {
descriptor.kind, /* copyOverrides = */ true) override fun getModality() = Modality.OPEN
newDescriptor.setSingleOverridden(descriptor) 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 returnType = descriptor.returnType
val returnsNotUnit = returnType != null && !KotlinBuiltIns.isUnit(returnType) val returnsNotUnit = returnType != null && !KotlinBuiltIns.isUnit(returnType)
@@ -2,7 +2,7 @@ import foo.A
import foo.B import foo.B
class C : A() { 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> <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 import foo.Intf
class Impl(): 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> <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 import foo.Intf
class Impl(): Intf() { class Impl(): Intf() {
override fun getFooBar(): String? { override fun getFooBar(): String {
<selection><caret>return super.getFooBar()</selection> <selection><caret>return super.getFooBar()</selection>
} }
} }
@@ -3,7 +3,7 @@ package foo
import foo.Intf import foo.Intf
class Impl(): Intf() { class Impl(): Intf() {
override fun getFooBar(): String? { override fun getFooBar(): String {
<selection><caret>return super.getFooBar()</selection> <selection><caret>return super.getFooBar()</selection>
} }
} }
@@ -0,0 +1,7 @@
package foo;
public class A {
public String foo(String s) {
return null;
}
}
@@ -0,0 +1,5 @@
import foo.A
class B : A() {
<caret>
}
@@ -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() { fun testConvertJavaDoc() {
doOverrideDirectoryTest("foo") doOverrideDirectoryTest("foo")
} }
fun testPlatformTypes() {
doOverrideDirectoryTest("foo")
}
} }