Refactor JavaEnumValueAnnotationArgument and implementations
Only require implementations to be able to compute the enum class name and the corresponding entry name. Only this should be necessary to correctly resolve the argument; the resolvers will find the corresponding class descriptor if necessary
This commit is contained in:
+10
-8
@@ -19,6 +19,7 @@ package org.jetbrains.kotlin.javac.wrappers.symbols
|
||||
import com.sun.tools.javac.code.Symbol
|
||||
import org.jetbrains.kotlin.javac.JavacWrapper
|
||||
import org.jetbrains.kotlin.load.java.structure.*
|
||||
import org.jetbrains.kotlin.name.ClassId
|
||||
import org.jetbrains.kotlin.name.Name
|
||||
import javax.lang.model.element.AnnotationMirror
|
||||
import javax.lang.model.element.AnnotationValue
|
||||
@@ -63,16 +64,17 @@ class SymbolBasedReferenceAnnotationArgument(
|
||||
name: Name,
|
||||
javac: JavacWrapper
|
||||
) : SymbolBasedAnnotationArgument(name, javac), JavaEnumValueAnnotationArgument {
|
||||
override val entryName: Name?
|
||||
get() = name
|
||||
|
||||
override fun resolve(): SymbolBasedField {
|
||||
val containingClass = (element.enclosingElement as Symbol.ClassSymbol).let {
|
||||
SymbolBasedClass(it, javac, null, it.classfile)
|
||||
}
|
||||
return SymbolBasedField(element, containingClass, javac)
|
||||
// TODO: do not create extra objects here
|
||||
private val javaField: JavaField? by lazy(LazyThreadSafetyMode.PUBLICATION) {
|
||||
val containingClass = element.enclosingElement as Symbol.ClassSymbol
|
||||
SymbolBasedField(element, SymbolBasedClass(containingClass, javac, null, containingClass.classfile), javac)
|
||||
}
|
||||
|
||||
override val enumClassId: ClassId?
|
||||
get() = javaField?.containingClass?.classId
|
||||
|
||||
override val entryName: Name?
|
||||
get() = javaField?.name
|
||||
}
|
||||
|
||||
class SymbolBasedClassObjectAnnotationArgument(
|
||||
|
||||
+17
-12
@@ -50,21 +50,26 @@ class TreeBasedLiteralAnnotationArgument(name: Name,
|
||||
override val value: Any?,
|
||||
javac: JavacWrapper) : TreeBasedAnnotationArgument(name, javac), JavaLiteralAnnotationArgument
|
||||
|
||||
class TreeBasedReferenceAnnotationArgument(name: Name,
|
||||
private val compilationUnit: CompilationUnitTree,
|
||||
private val field: JCTree.JCFieldAccess,
|
||||
javac: JavacWrapper,
|
||||
private val onElement: JavaElement) : TreeBasedAnnotationArgument(name, javac), JavaEnumValueAnnotationArgument {
|
||||
override val entryName: Name?
|
||||
get() = name
|
||||
class TreeBasedReferenceAnnotationArgument(
|
||||
name: Name,
|
||||
private val compilationUnit: CompilationUnitTree,
|
||||
private val field: JCTree.JCFieldAccess,
|
||||
javac: JavacWrapper,
|
||||
private val onElement: JavaElement
|
||||
) : TreeBasedAnnotationArgument(name, javac), JavaEnumValueAnnotationArgument {
|
||||
// TODO: do not run resolve here
|
||||
private val javaField: JavaField? by lazy(LazyThreadSafetyMode.PUBLICATION) {
|
||||
val javaClass = javac.resolve(field.selected, compilationUnit, onElement) as? JavaClass
|
||||
val fieldName = Name.identifier(field.name.toString())
|
||||
|
||||
override fun resolve(): JavaField? {
|
||||
val javaClass = javac.resolve(field.selected, compilationUnit, onElement) as? JavaClass ?: return null
|
||||
val fieldName = field.name.toString().let { Name.identifier(it) }
|
||||
|
||||
return javaClass.fields.find { it.name == fieldName }
|
||||
javaClass?.fields?.find { it.name == fieldName }
|
||||
}
|
||||
|
||||
override val enumClassId: ClassId?
|
||||
get() = javaField?.containingClass?.classId
|
||||
|
||||
override val entryName: Name?
|
||||
get() = javaField?.name
|
||||
}
|
||||
|
||||
class TreeBasedArrayAnnotationArgument(val args: List<JavaAnnotationArgument>,
|
||||
|
||||
@@ -20,7 +20,6 @@ import com.sun.tools.javac.tree.JCTree
|
||||
import org.jetbrains.kotlin.descriptors.Visibilities
|
||||
import org.jetbrains.kotlin.descriptors.Visibility
|
||||
import org.jetbrains.kotlin.javac.wrappers.symbols.SymbolBasedArrayAnnotationArgument
|
||||
import org.jetbrains.kotlin.javac.wrappers.symbols.SymbolBasedField
|
||||
import org.jetbrains.kotlin.javac.wrappers.symbols.SymbolBasedReferenceAnnotationArgument
|
||||
import org.jetbrains.kotlin.load.java.JavaVisibilities
|
||||
import org.jetbrains.kotlin.load.java.structure.JavaAnnotation
|
||||
@@ -81,18 +80,16 @@ fun Collection<JavaAnnotation>.filterTypeAnnotations(): Collection<JavaAnnotatio
|
||||
}
|
||||
is TreeBasedArrayAnnotationArgument -> {
|
||||
elementTypeArg.args.find {
|
||||
val field = (it as? TreeBasedReferenceAnnotationArgument)?.resolve() as? SymbolBasedField
|
||||
field?.element?.simpleName?.contentEquals("TYPE_USE") ?: false
|
||||
(it as? TreeBasedReferenceAnnotationArgument)?.entryName?.asString() == "TYPE_USE"
|
||||
}?.let { filteredAnnotations.add(annotation) }
|
||||
}
|
||||
is TreeBasedReferenceAnnotationArgument -> {
|
||||
(elementTypeArg.resolve() as? SymbolBasedField)?.let { field ->
|
||||
field.element.simpleName.takeIf { it.contentEquals("TYPE_USE") }
|
||||
?.let { filteredAnnotations.add(annotation) }
|
||||
if (elementTypeArg.entryName?.asString() == "TYPE_USE") {
|
||||
filteredAnnotations.add(annotation)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return filteredAnnotations
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user