Java to Kotlin: correct conversion of array components in annotation attributes

This commit is contained in:
Valentin Kipyatkov
2014-06-16 19:10:58 +04:00
parent 65b3ec6dfd
commit bd30ae572e
4 changed files with 41 additions and 23 deletions
+29 -23
View File
@@ -629,7 +629,7 @@ public class Converter private(val project: Project, val settings: ConverterSett
}
}
val list = annotations.map { convertAnnotation(it, owner is PsiLocalVariable) }.filterNotNull()
val list = annotations.map { convertAnnotation(it, owner is PsiLocalVariable) }.filterNotNull() //TODO: brackets are also needed for local classes
return Annotations(list, newLines)
}
@@ -649,34 +649,40 @@ public class Converter private(val project: Project, val settings: ConverterSett
val attrName = it.getName()?.let { Identifier(it) }
val value = it.getValue()
val attrValues = when(value) {
is PsiExpression -> listOf(convertExpression(it.getValue() as? PsiExpression, expectedType))
is PsiArrayInitializerMemberValue -> {
val isVarArg = method == lastMethod /* converted to vararg in Kotlin */
if (isVarArg && it.getName() == null) {
value.getInitializers().map { DummyStringExpression(it.getText()!!)/*TODO*/ }
}
else {
val expectedTypeConverted = typeConverter.convertType(expectedType)
if (expectedTypeConverted is ArrayType) {
val array = createArrayInitializerExpression(expectedTypeConverted, value.getInitializers().map { DummyStringExpression(it.getText()!!)/*TODO*/ })
listOf(if (isVarArg) StarExpression(array) else array)
}
else {
listOf(DummyStringExpression(value.getText()!!))
}
}
}
else -> listOf(DummyStringExpression(value?.getText() ?: ""))
}
val isVarArg = method == lastMethod /* converted to vararg in Kotlin */
val attrValues = convertAttributeValue(value, expectedType, isVarArg, it.getName() == null)
attrValues.map { attrName to it }
}
return Annotation(name, arguments, brackets)
}
private fun convertAttributeValue(value: PsiAnnotationMemberValue?, expectedType: PsiType?, isVararg: Boolean, isUnnamed: Boolean): List<Expression> {
return when(value) {
is PsiExpression -> listOf(convertExpression(value as? PsiExpression, expectedType))
is PsiArrayInitializerMemberValue -> {
val componentType = (expectedType as? PsiArrayType)?.getComponentType()
val componentsConverted = value.getInitializers().map { convertAttributeValue(it, componentType, false, true).single() }
if (isVararg && isUnnamed) {
componentsConverted
}
else {
val expectedTypeConverted = typeConverter.convertType(expectedType)
if (expectedTypeConverted is ArrayType) {
val array = createArrayInitializerExpression(expectedTypeConverted, componentsConverted)
listOf(if (isVararg) StarExpression(array) else array)
}
else {
listOf(DummyStringExpression(value.getText()!!))
}
}
}
else -> listOf(DummyStringExpression(value?.getText() ?: ""))
}
}
private val TYPE_MAP: Map<String, String> = mapOf(
JAVA_LANG_BYTE to "byte",
JAVA_LANG_SHORT to "short",
+8
View File
@@ -32,6 +32,14 @@ public @interface Anon6 {
int intValue() default 10;
}
public @interface Anon7 {
Class[] value();
}
public @interface Anon8 {
Class[] classes();
}
public enum E {
A, B, C
}
@@ -7,6 +7,8 @@ import javaApi.*;
@Anon4({"x", "y"})
@Anon5(1)
@Anon6({"x", "y"})
@Anon7({ String.class, StringBuilder.class })
@Anon8(classes = { String.class, StringBuilder.class })
class C {
@Anon5(1) @Deprecated private int field1 = 0;
@@ -6,6 +6,8 @@ Anon3(e = E.A, stringArray = array<String>(), value = *array<String>("a", "b"))
Anon4("x", "y")
Anon5(1)
Anon6(array<String>("x", "y"))
Anon7(javaClass<String>(), javaClass<StringBuilder>())
Anon8(classes = *array<Class<*>>(javaClass<String>(), javaClass<StringBuilder>()))
class C() {
Anon5(1) deprecated("") private var field1: Int = 0