j2k: Adjust annotation converter to last changes
Only parameter named `value` can be marked vararg
This commit is contained in:
@@ -98,15 +98,15 @@ class AnnotationConverter(private val converter: Converter) {
|
|||||||
val nameRef = annotation.getNameReferenceElement()
|
val nameRef = annotation.getNameReferenceElement()
|
||||||
val name = Identifier((nameRef ?: return null).getText()!!).assignPrototype(nameRef)
|
val name = Identifier((nameRef ?: return null).getText()!!).assignPrototype(nameRef)
|
||||||
val annotationClass = nameRef.resolve() as? PsiClass
|
val annotationClass = nameRef.resolve() as? PsiClass
|
||||||
val lastMethod = annotationClass?.getMethods()?.lastOrNull()
|
|
||||||
val arguments = annotation.getParameterList().getAttributes().flatMap {
|
val arguments = annotation.getParameterList().getAttributes().flatMap {
|
||||||
val method = annotationClass?.findMethodsByName(it.getName() ?: "value", false)?.firstOrNull()
|
val parameterName = it.getName() ?: "value"
|
||||||
|
val method = annotationClass?.findMethodsByName(parameterName, false)?.firstOrNull()
|
||||||
val expectedType = method?.getReturnType()
|
val expectedType = method?.getReturnType()
|
||||||
|
|
||||||
val attrName = it.getName()?.let { Identifier(it).assignNoPrototype() }
|
val attrName = it.getName()?.let { Identifier(it).assignNoPrototype() }
|
||||||
val value = it.getValue()
|
val value = it.getValue()
|
||||||
|
|
||||||
val isVarArg = method == lastMethod /* converted to vararg in Kotlin */
|
val isVarArg = parameterName == "value" /* converted to vararg in Kotlin */
|
||||||
val attrValues = convertAttributeValue(value, expectedType, isVarArg, it.getName() == null)
|
val attrValues = convertAttributeValue(value, expectedType, isVarArg, it.getName() == null)
|
||||||
|
|
||||||
attrValues.map { attrName to converter.deferredElement(it) }
|
attrValues.map { attrName to converter.deferredElement(it) }
|
||||||
|
|||||||
@@ -234,21 +234,36 @@ class Converter private(
|
|||||||
val paramModifiers = Modifiers(listOf(Modifier.PUBLIC)).assignNoPrototype()
|
val paramModifiers = Modifiers(listOf(Modifier.PUBLIC)).assignNoPrototype()
|
||||||
val noBlankLinesInheritance = CommentsAndSpacesInheritance(blankLinesBefore = false)
|
val noBlankLinesInheritance = CommentsAndSpacesInheritance(blankLinesBefore = false)
|
||||||
val annotationMethods = psiClass.getMethods().filterIsInstance<PsiAnnotationMethod>()
|
val annotationMethods = psiClass.getMethods().filterIsInstance<PsiAnnotationMethod>()
|
||||||
val parameters = annotationMethods
|
val (methodsNamedValue, otherMethods) = annotationMethods.partition { it.getName() == "value" }
|
||||||
.map { method ->
|
|
||||||
|
fun createParameter(type: Type, method: PsiAnnotationMethod): Parameter {
|
||||||
|
type.assignPrototype(method.getReturnTypeElement(), noBlankLinesInheritance)
|
||||||
|
|
||||||
|
return Parameter(method.declarationIdentifier(),
|
||||||
|
type,
|
||||||
|
Parameter.VarValModifier.Val,
|
||||||
|
convertAnnotations(method),
|
||||||
|
paramModifiers,
|
||||||
|
annotationConverter.convertAnnotationMethodDefault(method)).assignPrototype(method, noBlankLinesInheritance)
|
||||||
|
}
|
||||||
|
|
||||||
|
val parameters =
|
||||||
|
// Argument named `value` comes first if it exists
|
||||||
|
// Convert it as vararg if it's array
|
||||||
|
methodsNamedValue.
|
||||||
|
map { method ->
|
||||||
val returnType = method.getReturnType()
|
val returnType = method.getReturnType()
|
||||||
val typeConverted = if (method == annotationMethods.last() && returnType is PsiArrayType)
|
val typeConverted = if (returnType is PsiArrayType)
|
||||||
VarArgType(typeConverter.convertType(returnType.getComponentType(), Nullability.NotNull))
|
VarArgType(typeConverter.convertType(returnType.getComponentType(), Nullability.NotNull))
|
||||||
else
|
else
|
||||||
typeConverter.convertType(returnType, Nullability.NotNull)
|
typeConverter.convertType(returnType, Nullability.NotNull)
|
||||||
typeConverted.assignPrototype(method.getReturnTypeElement(), noBlankLinesInheritance)
|
|
||||||
|
|
||||||
Parameter(method.declarationIdentifier(),
|
createParameter(typeConverted, method)
|
||||||
typeConverted,
|
} +
|
||||||
Parameter.VarValModifier.Val,
|
otherMethods
|
||||||
convertAnnotations(method),
|
.map { method ->
|
||||||
paramModifiers,
|
val typeConverted = typeConverter.convertType(method.getReturnType(), Nullability.NotNull)
|
||||||
annotationConverter.convertAnnotationMethodDefault(method)).assignPrototype(method, noBlankLinesInheritance)
|
createParameter(typeConverted, method)
|
||||||
}
|
}
|
||||||
val parameterList = ParameterList(parameters).assignNoPrototype()
|
val parameterList = ParameterList(parameters).assignNoPrototype()
|
||||||
val constructorSignature = if (parameterList.parameters.isNotEmpty())
|
val constructorSignature = if (parameterList.parameters.isNotEmpty())
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
annotation class Anon(public val s: String = "a", public val stringArray: Array<String> = array("a", "b"), public vararg val intArray: Int)
|
annotation class Anon(public val s: String = "a", public val stringArray: Array<String> = array("a", "b"), public val intArray: IntArray)
|
||||||
|
|
||||||
Anon(intArray = *intArray(1, 2))
|
Anon(intArray = intArray(1, 2))
|
||||||
class A
|
class A
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
@interface Anon {
|
||||||
|
String[] value();
|
||||||
|
int x() default 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Anon("a", "b")
|
||||||
|
interface I1 {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Anon("c", "d", x = 1)
|
||||||
|
interface I2 {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Anon({"c", "d"}, x = 1)
|
||||||
|
interface I3 {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Anon(value = {"c", "d"})
|
||||||
|
interface I4 {
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
annotation class Anon(public vararg val value: String, public val x: Int = 1)
|
||||||
|
|
||||||
|
Anon("a", "b")
|
||||||
|
trait I1
|
||||||
|
|
||||||
|
Anon("c", "d", x = 1)
|
||||||
|
trait I2
|
||||||
|
|
||||||
|
Anon("c", "d", x = 1)
|
||||||
|
trait I3
|
||||||
|
|
||||||
|
Anon(value = *array("c", "d"))
|
||||||
|
trait I4
|
||||||
@@ -1,13 +1,13 @@
|
|||||||
import javaApi.*
|
import javaApi.*
|
||||||
|
|
||||||
Anon1(value = array("a"), stringArray = array("b"), intArray = intArray(1, 2), string = "x")
|
Anon1(value = *array("a"), stringArray = array("b"), intArray = intArray(1, 2), string = "x")
|
||||||
Anon2(value = "a", intValue = 1, charValue = 'a')
|
Anon2(value = "a", intValue = 1, charValue = 'a')
|
||||||
Anon3(e = E.A, stringArray = array(), value = *array("a", "b"))
|
Anon3(e = E.A, stringArray = array(), value = *array("a", "b"))
|
||||||
Anon4("x", "y")
|
Anon4("x", "y")
|
||||||
Anon5(1)
|
Anon5(1)
|
||||||
Anon6(array("x", "y"))
|
Anon6("x", "y")
|
||||||
Anon7(javaClass<String>(), javaClass<StringBuilder>())
|
Anon7(javaClass<String>(), javaClass<StringBuilder>())
|
||||||
Anon8(classes = *array(javaClass<String>(), javaClass<StringBuilder>()))
|
Anon8(classes = array(javaClass<String>(), javaClass<StringBuilder>()))
|
||||||
class C {
|
class C {
|
||||||
Anon5(1) deprecated("") private val field1 = 0
|
Anon5(1) deprecated("") private val field1 = 0
|
||||||
|
|
||||||
@@ -19,7 +19,7 @@ class C {
|
|||||||
Anon5(1)
|
Anon5(1)
|
||||||
var field4 = 0
|
var field4 = 0
|
||||||
|
|
||||||
Anon6(array())
|
Anon6
|
||||||
fun foo(deprecated("") p1: Int, deprecated("") Anon5(2) p2: Char) {
|
fun foo(deprecated("") p1: Int, deprecated("") Anon5(2) p2: Char) {
|
||||||
[deprecated("")] [Anon5(3)] val c = 'a'
|
[deprecated("")] [Anon5(3)] val c = 'a'
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -134,6 +134,12 @@ public class JavaToKotlinConverterForWebDemoTestGenerated extends AbstractJavaTo
|
|||||||
doTest(fileName);
|
doTest(fileName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("annotationInterface4.java")
|
||||||
|
public void testAnnotationInterface4() throws Exception {
|
||||||
|
String fileName = JetTestUtils.navigationMetadata("j2k/testData/fileOrElement/annotations/annotationInterface4.java");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
@TestMetadata("annotationUsages.java")
|
@TestMetadata("annotationUsages.java")
|
||||||
public void testAnnotationUsages() throws Exception {
|
public void testAnnotationUsages() throws Exception {
|
||||||
String fileName = JetTestUtils.navigationMetadata("j2k/testData/fileOrElement/annotations/annotationUsages.java");
|
String fileName = JetTestUtils.navigationMetadata("j2k/testData/fileOrElement/annotations/annotationUsages.java");
|
||||||
|
|||||||
@@ -134,6 +134,12 @@ public class JavaToKotlinConverterSingleFileTestGenerated extends AbstractJavaTo
|
|||||||
doTest(fileName);
|
doTest(fileName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("annotationInterface4.java")
|
||||||
|
public void testAnnotationInterface4() throws Exception {
|
||||||
|
String fileName = JetTestUtils.navigationMetadata("j2k/testData/fileOrElement/annotations/annotationInterface4.java");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
@TestMetadata("annotationUsages.java")
|
@TestMetadata("annotationUsages.java")
|
||||||
public void testAnnotationUsages() throws Exception {
|
public void testAnnotationUsages() throws Exception {
|
||||||
String fileName = JetTestUtils.navigationMetadata("j2k/testData/fileOrElement/annotations/annotationUsages.java");
|
String fileName = JetTestUtils.navigationMetadata("j2k/testData/fileOrElement/annotations/annotationUsages.java");
|
||||||
|
|||||||
Reference in New Issue
Block a user