diff --git a/libraries/tools/kotlin-annotation-processing/src/main/kotlin/org/jetbrains/kotlin/annotation/AnnotatedElementDescriptor.kt b/libraries/tools/kotlin-annotation-processing/src/main/kotlin/org/jetbrains/kotlin/annotation/AnnotatedElementDescriptor.kt new file mode 100644 index 00000000000..ad301bdf31b --- /dev/null +++ b/libraries/tools/kotlin-annotation-processing/src/main/kotlin/org/jetbrains/kotlin/annotation/AnnotatedElementDescriptor.kt @@ -0,0 +1,23 @@ +package org.jetbrains.kotlin.annotation + +sealed class AnnotatedElementDescriptor(val classFqName: String) { + class Class(classFqName: String) : AnnotatedElementDescriptor(classFqName) { + // use referential equality + } + + class Method(classFqName: String, val methodName: String) : AnnotatedElementDescriptor(classFqName) { + override fun equals(other: Any?) = other is Method && methodName == other.methodName && classFqName == other.classFqName + + override fun hashCode() = 31 * classFqName.hashCode() + methodName.hashCode() + } + + class Constructor(classFqName: String) : AnnotatedElementDescriptor(classFqName) { + // use referential equality + } + + class Field(classFqName: String, val fieldName: String) : AnnotatedElementDescriptor(classFqName) { + override fun equals(other: Any?) = other is Field && fieldName == other.fieldName && classFqName == other.classFqName + + override fun hashCode() = 31 * classFqName.hashCode() + fieldName.hashCode() + } +} \ No newline at end of file diff --git a/libraries/tools/kotlin-annotation-processing/src/main/kotlin/org/jetbrains/kotlin/annotation/AnnotationProcessorWrapper.kt b/libraries/tools/kotlin-annotation-processing/src/main/kotlin/org/jetbrains/kotlin/annotation/AnnotationProcessorWrapper.kt index 9377eb67b75..1e8a85ecbaf 100644 --- a/libraries/tools/kotlin-annotation-processing/src/main/kotlin/org/jetbrains/kotlin/annotation/AnnotationProcessorWrapper.kt +++ b/libraries/tools/kotlin-annotation-processing/src/main/kotlin/org/jetbrains/kotlin/annotation/AnnotationProcessorWrapper.kt @@ -30,27 +30,6 @@ public class AnnotationProcessorStub : AbstractProcessor() { override fun process(annotations: Set?, roundEnv: RoundEnvironment?) = true } -abstract class AnnotatedElementDescriptor(public val classFqName: String) - -class AnnotatedClassDescriptor(classFqName: String) : AnnotatedElementDescriptor(classFqName) { - // use referential equality -} - -class AnnotatedMethodDescriptor(classFqName: String, public val methodName: String) : AnnotatedElementDescriptor(classFqName) { - override fun equals(other: Any?) = other is AnnotatedMethodDescriptor && methodName == other.methodName && classFqName == other.classFqName - - override fun hashCode() = 31 * classFqName.hashCode() + methodName.hashCode() -} -class AnnotatedConstructorDescriptor(classFqName: String) : AnnotatedElementDescriptor(classFqName) { - // use referential equality -} - -class AnnotatedFieldDescriptor(classFqName: String, public val fieldName: String) : AnnotatedElementDescriptor(classFqName) { - override fun equals(other: Any?) = other is AnnotatedFieldDescriptor && fieldName == other.fieldName && classFqName == other.classFqName - - override fun hashCode() = 31 * classFqName.hashCode() + fieldName.hashCode() -} - public abstract class AnnotationProcessorWrapper( private val processorFqName: String, private val taskQualifier: String diff --git a/libraries/tools/kotlin-annotation-processing/src/main/kotlin/org/jetbrains/kotlin/annotation/KotlinAnnotationProvider.kt b/libraries/tools/kotlin-annotation-processing/src/main/kotlin/org/jetbrains/kotlin/annotation/KotlinAnnotationProvider.kt index 7c1dc60568c..ca5074c9733 100644 --- a/libraries/tools/kotlin-annotation-processing/src/main/kotlin/org/jetbrains/kotlin/annotation/KotlinAnnotationProvider.kt +++ b/libraries/tools/kotlin-annotation-processing/src/main/kotlin/org/jetbrains/kotlin/annotation/KotlinAnnotationProvider.kt @@ -94,18 +94,18 @@ class KotlinAnnotationProvider(annotationsReader: Reader) { val set = annotatedKotlinElementsInternal.getOrPut(annotationName) { hashSetOf() } set.add(when (type) { - ANNOTATED_CLASS -> AnnotatedClassDescriptor(classFqName) + ANNOTATED_CLASS -> AnnotatedElementDescriptor.Class(classFqName) ANNOTATED_FIELD -> { val name = elementName ?: throw AssertionError("Name for field must be provided") - AnnotatedFieldDescriptor(classFqName, name) + AnnotatedElementDescriptor.Field(classFqName, name) } ANNOTATED_METHOD -> { val name = elementName ?: throw AssertionError("Name for method must be provided") if ("" == name) - AnnotatedConstructorDescriptor(classFqName) + AnnotatedElementDescriptor.Constructor(classFqName) else - AnnotatedMethodDescriptor(classFqName, name) + AnnotatedElementDescriptor.Method(classFqName, name) } else -> throw AssertionError("Unknown type: $type") }) diff --git a/libraries/tools/kotlin-annotation-processing/src/main/kotlin/org/jetbrains/kotlin/annotation/RoundEnvironmentWrapper.kt b/libraries/tools/kotlin-annotation-processing/src/main/kotlin/org/jetbrains/kotlin/annotation/RoundEnvironmentWrapper.kt index f0908fae6c6..e5c51bda789 100644 --- a/libraries/tools/kotlin-annotation-processing/src/main/kotlin/org/jetbrains/kotlin/annotation/RoundEnvironmentWrapper.kt +++ b/libraries/tools/kotlin-annotation-processing/src/main/kotlin/org/jetbrains/kotlin/annotation/RoundEnvironmentWrapper.kt @@ -7,7 +7,6 @@ import javax.lang.model.element.Element import javax.lang.model.element.ElementKind import javax.lang.model.element.TypeElement import javax.lang.model.type.NoType -import javax.lang.model.type.TypeVisitor internal class RoundEnvironmentWrapper( val processingEnv: ProcessingEnvironment, @@ -67,16 +66,16 @@ internal class RoundEnvironmentWrapper( val descriptorsWithKotlin = descriptors.fold(hashSetOf()) { set, descriptor -> val clazz = processingEnv.elementUtils.getTypeElement(descriptor.classFqName) ?: return@fold set when (descriptor) { - is AnnotatedClassDescriptor -> set.add(clazz) - is AnnotatedConstructorDescriptor -> { + is AnnotatedElementDescriptor.Class -> set.add(clazz) + is AnnotatedElementDescriptor.Constructor -> { set.addAll(clazz.filterEnclosedElements(ElementKind.CONSTRUCTOR) .filter { it.hasAnnotation(annotationFqName) }) } - is AnnotatedFieldDescriptor -> { + is AnnotatedElementDescriptor.Field -> { set.addAll(clazz.filterEnclosedElements(ElementKind.FIELD, descriptor.fieldName) .filter { it.hasAnnotation(annotationFqName) }) } - is AnnotatedMethodDescriptor -> { + is AnnotatedElementDescriptor.Method -> { set.addAll(clazz.filterEnclosedElements(ElementKind.METHOD, descriptor.methodName) .filter { it.hasAnnotation(annotationFqName) }) } diff --git a/libraries/tools/kotlin-annotation-processing/src/test/kotlin/org/jetbrains/kotlin/annotation/AnnotationListParseTest.kt b/libraries/tools/kotlin-annotation-processing/src/test/kotlin/org/jetbrains/kotlin/annotation/AnnotationListParseTest.kt index 1156940e6dc..a3ffb6086e8 100644 --- a/libraries/tools/kotlin-annotation-processing/src/test/kotlin/org/jetbrains/kotlin/annotation/AnnotationListParseTest.kt +++ b/libraries/tools/kotlin-annotation-processing/src/test/kotlin/org/jetbrains/kotlin/annotation/AnnotationListParseTest.kt @@ -61,11 +61,10 @@ public class AnnotationListParseTest { for (element in it.value) { actualAnnotations.append(it.key).append(' ').append(element.classFqName) when (element) { - is AnnotatedMethodDescriptor -> actualAnnotations.append(' ').append(element.methodName) - is AnnotatedFieldDescriptor -> actualAnnotations.append(' ').append(element.fieldName) - is AnnotatedConstructorDescriptor -> actualAnnotations.append(" ") - is AnnotatedClassDescriptor -> {} - else -> Assert.fail("Unknown element type: $element") + is AnnotatedElementDescriptor.Method -> actualAnnotations.append(' ').append(element.methodName) + is AnnotatedElementDescriptor.Field -> actualAnnotations.append(' ').append(element.fieldName) + is AnnotatedElementDescriptor.Constructor -> actualAnnotations.append(" ") + is AnnotatedElementDescriptor.Class -> {} } actualAnnotations.append('\n') }