Java to Kotlin converter: declare private field with no writes as val
This commit is contained in:
@@ -285,7 +285,7 @@ public class Converter private(val project: Project, val settings: ConverterSett
|
|||||||
else {
|
else {
|
||||||
val initializer = field.getInitializer()
|
val initializer = field.getInitializer()
|
||||||
val convertedType = typeConverter.convertVariableType(field)
|
val convertedType = typeConverter.convertVariableType(field)
|
||||||
val isVal = field.hasModifierProperty(PsiModifier.FINAL)
|
val isVal = field.isEffectivelyFinal()
|
||||||
val omitType = !settings.specifyFieldTypeByDefault &&
|
val omitType = !settings.specifyFieldTypeByDefault &&
|
||||||
initializer != null &&
|
initializer != null &&
|
||||||
(modifiers.isPrivate && (isVal || convertedType == typeConverter.convertExpressionType(initializer)) ||
|
(modifiers.isPrivate && (isVal || convertedType == typeConverter.convertExpressionType(initializer)) ||
|
||||||
|
|||||||
@@ -263,13 +263,4 @@ class TypeConverter(val settings: ConverterSettings, val conversionScope: Conver
|
|||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun PsiVariable.isEffectivelyFinal(): Boolean {
|
|
||||||
if (hasModifierProperty(PsiModifier.FINAL)) return true
|
|
||||||
return when(this) {
|
|
||||||
is PsiLocalVariable -> !hasWriteAccesses(getContainingMethod())
|
|
||||||
is PsiField -> if (hasModifierProperty(PsiModifier.PRIVATE)) !hasWriteAccesses(getContainingClass()) else false
|
|
||||||
else -> false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -93,3 +93,12 @@ fun PsiElement.isInSingleLine(): Boolean {
|
|||||||
}
|
}
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun PsiVariable.isEffectivelyFinal(): Boolean {
|
||||||
|
if (hasModifierProperty(PsiModifier.FINAL)) return true
|
||||||
|
return when(this) {
|
||||||
|
is PsiLocalVariable -> !hasWriteAccesses(getContainingMethod())
|
||||||
|
is PsiField -> if (hasModifierProperty(PsiModifier.PRIVATE)) !hasWriteAccesses(getContainingClass()) else false
|
||||||
|
else -> false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -1099,6 +1099,11 @@ public class JavaToKotlinConverterTestGenerated extends AbstractJavaToKotlinConv
|
|||||||
doTest("j2k/tests/testData/ast/field/specifyType.java");
|
doTest("j2k/tests/testData/ast/field/specifyType.java");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("valOrVar.java")
|
||||||
|
public void testValOrVar() throws Exception {
|
||||||
|
doTest("j2k/tests/testData/ast/field/valOrVar.java");
|
||||||
|
}
|
||||||
|
|
||||||
@TestMetadata("valWithInit.java")
|
@TestMetadata("valWithInit.java")
|
||||||
public void testValWithInit() throws Exception {
|
public void testValWithInit() throws Exception {
|
||||||
doTest("j2k/tests/testData/ast/field/valWithInit.java");
|
doTest("j2k/tests/testData/ast/field/valWithInit.java");
|
||||||
|
|||||||
@@ -9,10 +9,10 @@ Anon6(array("x", "y"))
|
|||||||
Anon7(javaClass<String>(), javaClass<StringBuilder>())
|
Anon7(javaClass<String>(), javaClass<StringBuilder>())
|
||||||
Anon8(classes = *array(javaClass<String>(), javaClass<StringBuilder>()))
|
Anon8(classes = *array(javaClass<String>(), javaClass<StringBuilder>()))
|
||||||
class C() {
|
class C() {
|
||||||
Anon5(1) deprecated("") private var field1 = 0
|
Anon5(1) deprecated("") private val field1 = 0
|
||||||
|
|
||||||
Anon5(1)
|
Anon5(1)
|
||||||
private var field2 = 0
|
private val field2 = 0
|
||||||
|
|
||||||
Anon5(1) var field3 = 0
|
Anon5(1) var field3 = 0
|
||||||
|
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ class A() {
|
|||||||
/* body is empty */
|
/* body is empty */
|
||||||
}
|
}
|
||||||
|
|
||||||
private /*it's private*/ var field = 0
|
private /*it's private*/ val field = 0
|
||||||
|
|
||||||
public /*it's public*/ fun foo(): Char {
|
public /*it's public*/ fun foo(): Char {
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,3 +1,3 @@
|
|||||||
class A() {
|
class A() {
|
||||||
private var isOpen = true // ideally should be atomic boolean
|
private val isOpen = true // ideally should be atomic boolean
|
||||||
}
|
}
|
||||||
@@ -1,13 +1,13 @@
|
|||||||
//file
|
//file
|
||||||
public class Test {
|
public class Test {
|
||||||
private final String myName;
|
private final String myName;
|
||||||
private boolean a;
|
boolean a;
|
||||||
private double b;
|
double b;
|
||||||
private float c;
|
float c;
|
||||||
private long d;
|
long d;
|
||||||
private int e;
|
int e;
|
||||||
private short f;
|
protected short f;
|
||||||
private char g;
|
protected char g;
|
||||||
|
|
||||||
public Test() {}
|
public Test() {}
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
public class Test private(private val myName: String, private var a: Boolean, private var b: Double, private var c: Float, private var d: Long, private var e: Int, private var f: Short, private var g: Char) {
|
public class Test private(private val myName: String, var a: Boolean, var b: Double, var c: Float, var d: Long, var e: Int, protected var f: Short, protected var g: Char) {
|
||||||
class object {
|
class object {
|
||||||
|
|
||||||
public fun create(): Test {
|
public fun create(): Test {
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
enum class E {
|
enum class E {
|
||||||
I
|
I
|
||||||
private var name: String = 0
|
private val name: String = 0
|
||||||
}
|
}
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
class Base() {
|
class Base() {
|
||||||
private var myFirst: String = 0
|
private val myFirst: String = 0
|
||||||
}
|
}
|
||||||
|
|
||||||
class Child() : Base() {
|
class Child() : Base() {
|
||||||
private var mySecond: String = 0
|
private val mySecond: String = 0
|
||||||
}
|
}
|
||||||
@@ -1,3 +1,3 @@
|
|||||||
class C() {
|
class C() {
|
||||||
private var f: Foo = 0
|
private val f: Foo = 0
|
||||||
}
|
}
|
||||||
@@ -20,6 +20,7 @@ class A {
|
|||||||
String foo() { return "x"; }
|
String foo() { return "x"; }
|
||||||
|
|
||||||
void bar() {
|
void bar() {
|
||||||
|
field5 = new ArrayList<String>();
|
||||||
field7++;
|
field7++;
|
||||||
field8++;
|
field8++;
|
||||||
field9 = null;
|
field9 = null;
|
||||||
|
|||||||
@@ -21,6 +21,7 @@ class A() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun bar() {
|
fun bar() {
|
||||||
|
field5 = ArrayList<String>()
|
||||||
field7++
|
field7++
|
||||||
field8++
|
field8++
|
||||||
field9 = null
|
field9 = null
|
||||||
|
|||||||
@@ -0,0 +1,12 @@
|
|||||||
|
//file
|
||||||
|
class A {
|
||||||
|
private final int field1 = 0;
|
||||||
|
private int field2 = 0;
|
||||||
|
private int field3 = 0;
|
||||||
|
final int field4 = 0;
|
||||||
|
int field5 = 0;
|
||||||
|
|
||||||
|
void foo() {
|
||||||
|
field3 = field2;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
class A() {
|
||||||
|
private val field1 = 0
|
||||||
|
private val field2 = 0
|
||||||
|
private var field3 = 0
|
||||||
|
val field4 = 0
|
||||||
|
var field5 = 0
|
||||||
|
|
||||||
|
fun foo() {
|
||||||
|
field3 = field2
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -15,7 +15,7 @@ class C() {
|
|||||||
/**
|
/**
|
||||||
* This is a field doc comment.
|
* This is a field doc comment.
|
||||||
*/
|
*/
|
||||||
private var i: Int = 0
|
private val i: Int = 0
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This is a function doc comment.
|
* This is a function doc comment.
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
class C() {
|
class C() {
|
||||||
private var s: String? = x()
|
private val s = x()
|
||||||
|
|
||||||
fun foo() {
|
fun foo() {
|
||||||
if (s == null) {
|
if (s == null) {
|
||||||
|
|||||||
@@ -8,10 +8,4 @@ class A {
|
|||||||
final List<String> field2 = new ArrayList<String>();
|
final List<String> field2 = new ArrayList<String>();
|
||||||
public final int field3 = 0;
|
public final int field3 = 0;
|
||||||
protected final int field4 = 0;
|
protected final int field4 = 0;
|
||||||
|
|
||||||
private List<String> field5 = new ArrayList<String>();
|
|
||||||
List<String> field6 = new ArrayList<String>();
|
|
||||||
|
|
||||||
private int field7 = 0;
|
|
||||||
int field8 = 0;
|
|
||||||
}
|
}
|
||||||
@@ -7,10 +7,4 @@ class A() {
|
|||||||
val field2: List<String> = ArrayList<String>()
|
val field2: List<String> = ArrayList<String>()
|
||||||
public val field3: Int = 0
|
public val field3: Int = 0
|
||||||
protected val field4: Int = 0
|
protected val field4: Int = 0
|
||||||
|
|
||||||
private var field5: List<String> = ArrayList<String>()
|
|
||||||
var field6: List<String> = ArrayList<String>()
|
|
||||||
|
|
||||||
private var field7: Int = 0
|
|
||||||
var field8: Int = 0
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user