J2K: not initialized field of non-primitive type should have null initializer

#KT-11544 Fixed
This commit is contained in:
Natalia Ukhorskaya
2016-03-23 13:54:13 +03:00
parent 8616c9588d
commit 4bc4f0bce7
16 changed files with 73 additions and 24 deletions
@@ -255,6 +255,9 @@ class TypeConverter(val converter: Converter) {
}
}
}
else if (variable is PsiField && !variable.hasWriteAccesses(converter.referenceSearcher, variable.containingClass)) {
return Nullability.Nullable
}
if (variable.isMainMethodParameter() ) {
return Nullability.NotNull
@@ -1,10 +1,9 @@
// ERROR: Property must be initialized or be abstract
internal object Library {
val ourOut: java.io.PrintStream
val ourOut: java.io.PrintStream? = null
}
internal class User {
fun main() {
Library.ourOut.print(1)
Library.ourOut!!.print(1)
}
}
@@ -1,10 +1,9 @@
// ERROR: Property must be initialized or be abstract
internal class Library {
val myString: String
val myString: String? = null
}
internal class User {
fun main() {
Library().myString.isEmpty()
Library().myString!!.isEmpty()
}
}
@@ -1,5 +1,5 @@
internal class T {
var a: String
var b: String
var a: String? = null
var b: String? = null
var c = "abc"
}
@@ -1,4 +1,3 @@
// ERROR: Property must be initialized or be abstract
import A.Nested
internal class A @JvmOverloads constructor(nested: Nested = Nested(Nested.FIELD)) {
@@ -12,5 +11,5 @@ internal class A @JvmOverloads constructor(nested: Nested = Nested(Nested.FIELD)
}
internal class B {
var nested: Nested
var nested: Nested? = null
}
@@ -1,4 +1,3 @@
// ERROR: Property must be initialized or be abstract
package pack
import pack.A.Nested
@@ -14,5 +13,5 @@ internal class A @JvmOverloads constructor(nested: Nested = Nested(Nested.FIELD)
}
internal class B {
var nested: Nested
var nested: Nested? = null
}
@@ -1,4 +1,3 @@
// ERROR: Property must be initialized or be abstract
package pack
import pack.A.*
@@ -14,5 +13,5 @@ internal class A @JvmOverloads constructor(nested: Nested = Nested(Nested.FIELD)
}
internal class B {
var nested: Nested
var nested: Nested? = null
}
+1 -2
View File
@@ -1,5 +1,4 @@
// ERROR: Unresolved reference: Foo
// ERROR: Property must be initialized or be abstract
internal class C {
var f: Foo
var f: Foo? = null
}
+22
View File
@@ -0,0 +1,22 @@
public class Init {
public String field1;
public String field2;
public int field3;
public int field4;
public Init() {
field1 = "str";
field3 = 1;
String prop1;
prop1 = "aaa";
String prop2;
int prop3;
prop3 = 1;
int prop4;
}
}
+22
View File
@@ -0,0 +1,22 @@
class Init {
var field1: String
var field2: String? = null
var field3: Int = 0
var field4: Int = 0
init {
field1 = "str"
field3 = 1
val prop1: String
prop1 = "aaa"
val prop2: String
val prop3: Int
prop3 = 1
val prop4: Int
}
}
+1 -2
View File
@@ -1,5 +1,4 @@
// ERROR: Unresolved reference: Foo
// ERROR: Property must be initialized or be abstract
internal class C {
protected var f: Foo
protected var f: Foo? = null
}
+1 -2
View File
@@ -1,5 +1,4 @@
// ERROR: Unresolved reference: Foo
// ERROR: Property must be initialized or be abstract
internal class C {
var f: Foo
var f: Foo? = null
}
+1 -2
View File
@@ -1,5 +1,4 @@
// ERROR: Unresolved reference: Foo
// ERROR: Property must be initialized or be abstract
internal class C {
var f: Foo
var f: Foo? = null
}
+1 -2
View File
@@ -1,8 +1,7 @@
// ERROR: Property must be initialized or be abstract
internal open class Base {
internal inner class Nested
}
internal class Derived : Base() {
var field: Base.Nested
var field: Base.Nested? = null
}
@@ -1825,6 +1825,12 @@ public class JavaToKotlinConverterForWebDemoTestGenerated extends AbstractJavaTo
doTest(fileName);
}
@TestMetadata("needInitializer.java")
public void testNeedInitializer() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("j2k/testData/fileOrElement/field/needInitializer.java");
doTest(fileName);
}
@TestMetadata("privateField.java")
public void testPrivateField() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("j2k/testData/fileOrElement/field/privateField.java");
@@ -1825,6 +1825,12 @@ public class JavaToKotlinConverterSingleFileTestGenerated extends AbstractJavaTo
doTest(fileName);
}
@TestMetadata("needInitializer.java")
public void testNeedInitializer() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("j2k/testData/fileOrElement/field/needInitializer.java");
doTest(fileName);
}
@TestMetadata("privateField.java")
public void testPrivateField() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("j2k/testData/fileOrElement/field/privateField.java");