Kapt: Represent a single element as an array if the annotation method type is array type
(cherry picked from commit 19ce4cb)
This commit is contained in:
committed by
Yan Zhulanow
parent
9127788f4a
commit
18068c699d
@@ -1,7 +1,19 @@
|
|||||||
// Simple
|
// Simple
|
||||||
|
|
||||||
|
@interface Anno {
|
||||||
|
String[] numbers();
|
||||||
|
}
|
||||||
|
|
||||||
|
@interface AnnoValue {
|
||||||
|
String[] value();
|
||||||
|
}
|
||||||
|
|
||||||
@KotlinAnnotation(a = "A", b = 5)
|
@KotlinAnnotation(a = "A", b = 5)
|
||||||
|
@Anno(numbers = { "five", "six" })
|
||||||
|
@AnnoValue({ "five", "six" })
|
||||||
public abstract class Simple {
|
public abstract class Simple {
|
||||||
|
@Anno(numbers = "seven")
|
||||||
|
@AnnoValue("seven")
|
||||||
final String field = "A";
|
final String field = "A";
|
||||||
|
|
||||||
abstract void voidMethod();
|
abstract void voidMethod();
|
||||||
|
|||||||
@@ -1,9 +1,13 @@
|
|||||||
@KotlinAnnotation(a = "A", b = 5)
|
@KotlinAnnotation(a = "A", b = 5)
|
||||||
|
@Anno(numbers = { "five", "six" })
|
||||||
|
@AnnoValue(value = { "five", "six" })
|
||||||
public abstract class Simple {
|
public abstract class Simple {
|
||||||
static {}
|
static {}
|
||||||
|
|
||||||
{}
|
{}
|
||||||
|
|
||||||
|
@Anno(numbers = { "seven" })
|
||||||
|
@AnnoValue(value = { "seven" })
|
||||||
final java.lang.String field
|
final java.lang.String field
|
||||||
|
|
||||||
abstract void voidMethod()
|
abstract void voidMethod()
|
||||||
|
|||||||
+9
-8
@@ -90,15 +90,16 @@ class DefaultJeElementRenderer : JeElementRenderer {
|
|||||||
.joinToString(prefix = "(", postfix = ")")
|
.joinToString(prefix = "(", postfix = ")")
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun renderAnnotationValue(value: AnnotationValue): String {
|
private fun renderAnnotationValue(av: AnnotationValue): String {
|
||||||
return when (value) {
|
return when (av) {
|
||||||
is JeAnnotationAnnotationValue -> renderAnnotation(value.value)
|
is JeAnnotationAnnotationValue -> renderAnnotation(av.value)
|
||||||
is JeArrayAnnotationValue -> value.value.joinToString(prefix = "{ ", postfix = " }") { renderAnnotationValue(it) }
|
is JeArrayAnnotationValue -> av.value.joinToString(prefix = "{ ", postfix = " }") { renderAnnotationValue(it) }
|
||||||
is JeEnumValueAnnotationValue -> value.value.simpleName.toString()
|
is JeSingletonArrayAnnotationValue -> av.value.joinToString(prefix = "{ ", postfix = " }") { renderAnnotationValue(it) }
|
||||||
|
is JeEnumValueAnnotationValue -> av.value.simpleName.toString()
|
||||||
is JeErrorAnnotationValue -> "<ERROR>"
|
is JeErrorAnnotationValue -> "<ERROR>"
|
||||||
is JeExpressionAnnotationValue, is JeLiteralAnnotationValue -> value.value?.let { renderConstantValue(it) } ?: "null"
|
is JeExpressionAnnotationValue, is JeLiteralAnnotationValue -> av.value?.let { renderConstantValue(it) } ?: "null"
|
||||||
is JeTypeAnnotationValue -> renderType(value.value) + ".class"
|
is JeTypeAnnotationValue -> renderType(av.value) + ".class"
|
||||||
else -> throw IllegalArgumentException("Unsupported annotation value: $value")
|
else -> throw IllegalArgumentException("Unsupported annotation value: $av")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+11
-4
@@ -16,9 +16,7 @@
|
|||||||
|
|
||||||
package org.jetbrains.kotlin.java.model.elements
|
package org.jetbrains.kotlin.java.model.elements
|
||||||
|
|
||||||
import com.intellij.psi.PsiAnnotation
|
import com.intellij.psi.*
|
||||||
import com.intellij.psi.PsiAnnotationMethod
|
|
||||||
import com.intellij.psi.PsiClass
|
|
||||||
import com.intellij.psi.util.PsiTypesUtil
|
import com.intellij.psi.util.PsiTypesUtil
|
||||||
import org.jetbrains.kotlin.java.model.types.JeDeclaredErrorType
|
import org.jetbrains.kotlin.java.model.types.JeDeclaredErrorType
|
||||||
import org.jetbrains.kotlin.java.model.types.JeDeclaredType
|
import org.jetbrains.kotlin.java.model.types.JeDeclaredType
|
||||||
@@ -43,10 +41,19 @@ class JeAnnotationMirror(val psi: PsiAnnotation) : AnnotationMirror {
|
|||||||
return mutableMapOf<ExecutableElement, AnnotationValue>().apply {
|
return mutableMapOf<ExecutableElement, AnnotationValue>().apply {
|
||||||
for (method in annotationClass.methods) {
|
for (method in annotationClass.methods) {
|
||||||
method as? PsiAnnotationMethod ?: continue
|
method as? PsiAnnotationMethod ?: continue
|
||||||
|
val returnType = method.returnType ?: continue
|
||||||
|
|
||||||
val attributeValue = psi.findDeclaredAttributeValue(method.name)
|
val attributeValue = psi.findDeclaredAttributeValue(method.name)
|
||||||
?: (if (withDefaults) method.defaultValue else null)
|
?: (if (withDefaults) method.defaultValue else null)
|
||||||
?: continue
|
?: continue
|
||||||
put(JeMethodExecutableElement(method), JeAnnotationValue(attributeValue))
|
|
||||||
|
val annotationValue = when {
|
||||||
|
returnType is PsiArrayType && attributeValue !is PsiArrayInitializerMemberValue ->
|
||||||
|
JeSingletonArrayAnnotationValue(attributeValue)
|
||||||
|
else -> JeAnnotationValue(attributeValue)
|
||||||
|
}
|
||||||
|
|
||||||
|
put(JeMethodExecutableElement(method), annotationValue)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+6
@@ -19,6 +19,7 @@ package org.jetbrains.kotlin.java.model.elements
|
|||||||
import com.intellij.psi.*
|
import com.intellij.psi.*
|
||||||
import org.jetbrains.kotlin.java.model.internal.calcConstantValue
|
import org.jetbrains.kotlin.java.model.internal.calcConstantValue
|
||||||
import org.jetbrains.kotlin.java.model.types.toJeType
|
import org.jetbrains.kotlin.java.model.types.toJeType
|
||||||
|
import java.util.*
|
||||||
import javax.lang.model.element.AnnotationValue
|
import javax.lang.model.element.AnnotationValue
|
||||||
import javax.lang.model.element.AnnotationValueVisitor
|
import javax.lang.model.element.AnnotationValueVisitor
|
||||||
|
|
||||||
@@ -93,6 +94,11 @@ class JeArrayAnnotationValue(val psi: PsiArrayInitializerMemberValue) : Annotati
|
|||||||
override fun <R : Any?, P : Any?> accept(v: AnnotationValueVisitor<R, P>, p: P) = v.visitArray(value, p)
|
override fun <R : Any?, P : Any?> accept(v: AnnotationValueVisitor<R, P>, p: P) = v.visitArray(value, p)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class JeSingletonArrayAnnotationValue(val psi: PsiAnnotationMemberValue) : AnnotationValue {
|
||||||
|
override fun getValue(): List<AnnotationValue> = Collections.singletonList(JeAnnotationValue(psi))
|
||||||
|
override fun <R : Any?, P : Any?> accept(v: AnnotationValueVisitor<R, P>, p: P) = v.visitArray(value, p)
|
||||||
|
}
|
||||||
|
|
||||||
class JeErrorAnnotationValue(val psi: PsiElement) : AnnotationValue {
|
class JeErrorAnnotationValue(val psi: PsiElement) : AnnotationValue {
|
||||||
override fun <R : Any?, P : Any?> accept(v: AnnotationValueVisitor<R, P>, p: P) = v.visitString(psi.text, p)
|
override fun <R : Any?, P : Any?> accept(v: AnnotationValueVisitor<R, P>, p: P) = v.visitString(psi.text, p)
|
||||||
override fun getValue() = null
|
override fun getValue() = null
|
||||||
|
|||||||
Reference in New Issue
Block a user