J2K: Insert initializer type for properties with primitive type if necessary

This commit is contained in:
Natalia Ukhorskaya
2015-12-17 18:01:17 +03:00
parent 188119aa83
commit 1d787ed222
12 changed files with 50 additions and 9 deletions
@@ -117,6 +117,9 @@ class CodeConverter(
}
convertedExpression = LiteralExpression(text)
}
else if (expectedTypeStr == "char") {
convertedExpression = MethodCallExpression.build(convertedExpression, "toChar", emptyList(), emptyList(), false)
}
}
else if (expression is PsiPrefixExpression && expression.isLiteralWithSign()) {
val operandConverted = convertExpression(expression.operand, expectedType)
@@ -445,6 +445,11 @@ class Converter private constructor(
public fun shouldDeclareVariableType(variable: PsiVariable, type: Type, canChangeType: Boolean): Boolean {
val initializer = variable.initializer
if (initializer == null || initializer.isNullLiteral()) return true
if (initializer.type is PsiPrimitiveType && type is PrimitiveType) {
if (createDefaultCodeConverter().convertedExpressionType(initializer, variable.type) != type) {
return true
}
}
if (canChangeType) return false
+1 -2
View File
@@ -1,10 +1,9 @@
// ERROR: None of the following functions can be called with the arguments supplied: public open fun valueOf(p0: kotlin.Short): kotlin.Short! defined in java.lang.Short public open fun valueOf(p0: kotlin.String!): kotlin.Short! defined in java.lang.Short
package demo
internal class Test {
fun test() {
val i = Integer.valueOf(100)
val s = 3
val s: Short = 3
val ss = java.lang.Short.valueOf(s)
}
}
+1 -1
View File
@@ -5,7 +5,7 @@ internal class Test {
}
fun test() {
val b = 10
val b: Byte = 10
putInt(b.toInt())
val b2 = 10
+1 -1
View File
@@ -5,7 +5,7 @@ internal class Test {
}
fun test() {
val b = 10
val b: Byte = 10
putInt(b.toInt())
}
}
+1 -1
View File
@@ -3,7 +3,7 @@ package demo
internal class Test(i: Int) {
fun test() {
val b = 10
val b: Byte = 10
Test(b.toInt())
}
}
+1 -1
View File
@@ -1,7 +1,7 @@
internal class Test {
val int: Int
get() {
val b = 10
val b: Byte = 10
return b.toInt()
}
}
+11
View File
@@ -0,0 +1,11 @@
class A {
public void foo() {
int i = 1;
byte b = 1;
short s = 1;
long l = 1;
double d = 1.0;
float f = 1.0f;
char c = 1;
}
}
+11
View File
@@ -0,0 +1,11 @@
internal class A {
fun foo() {
val i = 1
val b: Byte = 1
val s: Short = 1
val l: Long = 1
val d = 1.0
val f = 1.0f
val c: Char = 1.toChar()
}
}
+3 -3
View File
@@ -3,9 +3,9 @@ internal class A {
private var l2: Long = 1
private val l3 = 1L
private var l4: Long = -1
private val l5 = 123456789101112
private val l6 = -123456789101112
private val l7 = +1
private val l5: Long = 123456789101112
private val l6: Long = -123456789101112
private val l7: Long = +1
private val l8 = +1L
fun foo1(l: Long) {
@@ -2785,6 +2785,12 @@ public class JavaToKotlinConverterForWebDemoTestGenerated extends AbstractJavaTo
doTest(fileName);
}
@TestMetadata("kt-9997.java")
public void testKt_9997() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("j2k/testData/fileOrElement/issues/kt-9997.java");
doTest(fileName);
}
@TestMetadata("qualifyStatic.java")
public void testQualifyStatic() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("j2k/testData/fileOrElement/issues/qualifyStatic.java");
@@ -2785,6 +2785,12 @@ public class JavaToKotlinConverterSingleFileTestGenerated extends AbstractJavaTo
doTest(fileName);
}
@TestMetadata("kt-9997.java")
public void testKt_9997() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("j2k/testData/fileOrElement/issues/kt-9997.java");
doTest(fileName);
}
@TestMetadata("qualifyStatic.java")
public void testQualifyStatic() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("j2k/testData/fileOrElement/issues/qualifyStatic.java");