J2K: Insert initializer type for properties with primitive type if necessary
This commit is contained in:
@@ -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
@@ -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
@@ -5,7 +5,7 @@ internal class Test {
|
||||
}
|
||||
|
||||
fun test() {
|
||||
val b = 10
|
||||
val b: Byte = 10
|
||||
putInt(b.toInt())
|
||||
|
||||
val b2 = 10
|
||||
|
||||
+1
-1
@@ -5,7 +5,7 @@ internal class Test {
|
||||
}
|
||||
|
||||
fun test() {
|
||||
val b = 10
|
||||
val b: Byte = 10
|
||||
putInt(b.toInt())
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -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
@@ -1,7 +1,7 @@
|
||||
internal class Test {
|
||||
val int: Int
|
||||
get() {
|
||||
val b = 10
|
||||
val b: Byte = 10
|
||||
return b.toInt()
|
||||
}
|
||||
}
|
||||
+11
@@ -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
@@ -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
@@ -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");
|
||||
|
||||
Reference in New Issue
Block a user