KT-19943: Remove redundant special type unboxing calls in J2K

#KT-19943 Fixed
This commit is contained in:
a2kaido
2017-09-02 03:55:02 +09:00
committed by Simon Ogorodnik
parent 65cc5f1589
commit 083c3d8a5d
5 changed files with 78 additions and 0 deletions
@@ -449,6 +449,10 @@ class DefaultExpressionConverter : JavaElementVisitor(), ExpressionConverter {
val data = SpecialMethod.ConvertCallData(qualifier, arguments.asList(), typeArguments, dot, lPar, rPar, codeConverter)
val converted = specialMethod.convertCall(data)
if (converted != null) {
qualifier?.type?.let {
if (canRemoveSpecialMethod(specialMethod, it)) return
}
result = converted
return
}
@@ -468,6 +472,16 @@ class DefaultExpressionConverter : JavaElementVisitor(), ExpressionConverter {
methodExpression.assignNoPrototype()
}
private fun canRemoveSpecialMethod(specialMethod: SpecialMethod, psiType: PsiType): Boolean = when {
specialMethod == SpecialMethod.NUMBER_BYTE_VALUE && psiType.canonicalText == "java.lang.Byte" -> true
specialMethod == SpecialMethod.NUMBER_SHORT_VALUE && psiType.canonicalText == "java.lang.Short" -> true
specialMethod == SpecialMethod.NUMBER_INT_VALUE && psiType.canonicalText == "java.lang.Integer" -> true
specialMethod == SpecialMethod.NUMBER_LONG_VALUE && psiType.canonicalText == "java.lang.Long" -> true
specialMethod == SpecialMethod.NUMBER_FLOAT_VALUE && psiType.canonicalText == "java.lang.Float" -> true
specialMethod == SpecialMethod.NUMBER_DOUBLE_VALUE && psiType.canonicalText == "java.lang.Double" -> true
else -> false
}
private fun KtLightMethod.isKotlinExtensionFunction(): Boolean {
val origin = this.kotlinOrigin
if (origin != null) return origin.isExtensionDeclaration()
+27
View File
@@ -0,0 +1,27 @@
import java.util.List;
public class TestSpecialMethodForTypeValue {
public byte testByte(List<Byte> xs) {
return xs.get(0).byteValue();
}
public short testShort(List<Short> xs) {
return xs.get(0).shortValue();
}
public int testInt(List<Integer> xs) {
return xs.get(0).intValue();
}
public long testLong(List<Long> xs) {
return xs.get(0).longValue();
}
public float testFloat(List<Float> xs) {
return xs.get(0).floatValue();
}
public double testDouble(List<Double> xs) {
return xs.get(0).doubleValue();
}
}
+25
View File
@@ -0,0 +1,25 @@
class TestSpecialMethodForTypeValue {
fun testByte(xs: List<Byte>): Byte {
return xs[0]
}
fun testShort(xs: List<Short>): Short {
return xs[0]
}
fun testInt(xs: List<Int>): Int {
return xs[0]
}
fun testLong(xs: List<Long>): Long {
return xs[0]
}
fun testFloat(xs: List<Float>): Float {
return xs[0]
}
fun testDouble(xs: List<Double>): Double {
return xs[0]
}
}
@@ -2969,6 +2969,12 @@ public class JavaToKotlinConverterForWebDemoTestGenerated extends AbstractJavaTo
doTest(fileName);
}
@TestMetadata("kt-19943.java")
public void testKt_19943() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("j2k/testData/fileOrElement/issues/kt-19943.java");
doTest(fileName);
}
@TestMetadata("kt-5294.java")
public void testKt_5294() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("j2k/testData/fileOrElement/issues/kt-5294.java");
@@ -2969,6 +2969,12 @@ public class JavaToKotlinConverterSingleFileTestGenerated extends AbstractJavaTo
doTest(fileName);
}
@TestMetadata("kt-19943.java")
public void testKt_19943() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("j2k/testData/fileOrElement/issues/kt-19943.java");
doTest(fileName);
}
@TestMetadata("kt-5294.java")
public void testKt_5294() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("j2k/testData/fileOrElement/issues/kt-5294.java");