Java to Kotlin converter: special conversion of some Collections methods

This commit is contained in:
Valentin Kipyatkov
2014-07-09 16:46:13 +04:00
parent 9bd742472d
commit 46d6ba3340
5 changed files with 99 additions and 0 deletions
@@ -240,6 +240,34 @@ open class ExpressionVisitor(private val converter: Converter) : JavaElementVisi
return
}
//TODO: type arguments maybe required if we are in initializer of variable with no explicit type
if (target is PsiMethod && isCollectionsEmptyList(target) && arguments.size == 0) {
result = MethodCallExpression.build(null, "listOf", listOf(), listOf(), false)
return
}
//TODO: type arguments maybe required if we are in initializer of variable with no explicit type
if (target is PsiMethod && isCollectionsEmptySet(target) && arguments.size == 0) {
result = MethodCallExpression.build(null, "setOf", listOf(), listOf(), false)
return
}
//TODO: type arguments maybe required if we are in initializer of variable with no explicit type
if (target is PsiMethod && isCollectionsEmptyMap(target) && arguments.size == 0) {
result = MethodCallExpression.build(null, "mapOf", listOf(), listOf(), false)
return
}
if (target is PsiMethod && isCollectionsSingletonList(target) && arguments.size == 1) {
result = MethodCallExpression.build(null, "listOf", listOf(converter.convertExpression(arguments.single())), listOf(), false)
return
}
if (target is PsiMethod && isCollectionsSingleton(target) && arguments.size == 1) {
result = MethodCallExpression.build(null, "setOf", listOf(converter.convertExpression(arguments.single())), listOf(), false)
return
}
result = MethodCallExpression(converter.convertExpression(methodExpr),
convertArguments(expression),
typeArguments,
@@ -258,6 +286,36 @@ open class ExpressionVisitor(private val converter: Converter) : JavaElementVisi
method.getContainingClass()?.getQualifiedName() == "java.util.Objects"
}
private fun isCollectionsEmptyList(method: PsiMethod): Boolean {
return method.getName() == "emptyList" &&
method.getParameterList().getParameters().size == 0 &&
method.getContainingClass()?.getQualifiedName() == "java.util.Collections"
}
private fun isCollectionsEmptySet(method: PsiMethod): Boolean {
return method.getName() == "emptySet" &&
method.getParameterList().getParameters().size == 0 &&
method.getContainingClass()?.getQualifiedName() == "java.util.Collections"
}
private fun isCollectionsEmptyMap(method: PsiMethod): Boolean {
return method.getName() == "emptyMap" &&
method.getParameterList().getParameters().size == 0 &&
method.getContainingClass()?.getQualifiedName() == "java.util.Collections"
}
private fun isCollectionsSingletonList(method: PsiMethod): Boolean {
return method.getName() == "singletonList" &&
method.getParameterList().getParameters().size == 1 &&
method.getContainingClass()?.getQualifiedName() == "java.util.Collections"
}
private fun isCollectionsSingleton(method: PsiMethod): Boolean {
return method.getName() == "singleton" &&
method.getParameterList().getParameters().size == 1 &&
method.getContainingClass()?.getQualifiedName() == "java.util.Collections"
}
override fun visitNewExpression(expression: PsiNewExpression) {
if (expression.getArrayInitializer() != null) {
result = converter.convertExpression(expression.getArrayInitializer())
@@ -2050,6 +2050,11 @@ public class JavaToKotlinConverterTestGenerated extends AbstractJavaToKotlinConv
doTest("j2k/tests/testData/ast/methodCallExpression/callWithKeywords.java");
}
@TestMetadata("collectionsMethods.java")
public void testCollectionsMethods() throws Exception {
doTest("j2k/tests/testData/ast/methodCallExpression/collectionsMethods.java");
}
@TestMetadata("emptyCall.java")
public void testEmptyCall() throws Exception {
doTest("j2k/tests/testData/ast/methodCallExpression/emptyCall.java");
@@ -0,0 +1,12 @@
//file
import java.util.*;
class A {
Map<String, String> foo() {
List<String> list1 = Collections.emptyList();
List<Integer> list2 = Collections.singletonList(1);
Set<String> set1 = Collections.emptySet();
Set<String> set2 = Collections.singleton("a");
return Collections.emptyMap();
}
}
@@ -0,0 +1,12 @@
import java.util.*
import kotlin.Map
class A {
fun foo(): Map<String, String> {
val list1 = listOf()
val list2 = listOf(1)
val set1 = setOf()
val set2 = setOf("a")
return mapOf()
}
}
@@ -0,0 +1,12 @@
import java.util.*
import kotlin.Map
class A {
fun foo(): Map<String, String> {
val list1 = listOf<String>()
val list2 = listOf(1)
val set1 = setOf<String>()
val set2 = setOf("a")
return mapOf()
}
}