From cb94936e519557b3821eb0661e6d98e8b2debb5b Mon Sep 17 00:00:00 2001 From: Alexey Tsvetkov Date: Wed, 27 Apr 2016 14:54:55 +0300 Subject: [PATCH] Refactoring: rename AnnotatedElementDescriptor->AnnotatedElement --- ...ementDescriptor.kt => AnnotatedElement.kt} | 22 +++++++++---------- .../annotation/KotlinAnnotationProvider.kt | 14 ++++++------ .../annotation/RoundEnvironmentWrapper.kt | 8 +++---- .../annotation/AnnotationListParseTest.kt | 11 ++++++---- 4 files changed, 29 insertions(+), 26 deletions(-) rename libraries/tools/kotlin-annotation-processing/src/main/kotlin/org/jetbrains/kotlin/annotation/{AnnotatedElementDescriptor.kt => AnnotatedElement.kt} (70%) 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/AnnotatedElement.kt similarity index 70% rename from libraries/tools/kotlin-annotation-processing/src/main/kotlin/org/jetbrains/kotlin/annotation/AnnotatedElementDescriptor.kt rename to libraries/tools/kotlin-annotation-processing/src/main/kotlin/org/jetbrains/kotlin/annotation/AnnotatedElement.kt index 94886125a16..9c2e0d11534 100644 --- 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/AnnotatedElement.kt @@ -1,19 +1,19 @@ package org.jetbrains.kotlin.annotation -sealed class AnnotatedElementDescriptor(val classFqName: String) { - class Class(classFqName: String) : AnnotatedElementDescriptor(classFqName) { +sealed class AnnotatedElement(val classFqName: String) { + class Class(classFqName: String) : AnnotatedElement(classFqName) { override fun equals(other: Any?) = other is Class && classFqName == other.classFqName override fun hashCode() = classFqName.hashCode() } - class Method(classFqName: String, val methodName: String) : AnnotatedElementDescriptor(classFqName) { + class Method(classFqName: String, val methodName: String) : AnnotatedElement(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) { + class Constructor(classFqName: String) : AnnotatedElement(classFqName) { companion object { const val METHOD_NAME = "" } @@ -23,25 +23,25 @@ sealed class AnnotatedElementDescriptor(val classFqName: String) { override fun hashCode() = 31 * classFqName.hashCode() + METHOD_NAME.hashCode() } - class Field(classFqName: String, val fieldName: String) : AnnotatedElementDescriptor(classFqName) { + class Field(classFqName: String, val fieldName: String) : AnnotatedElement(classFqName) { override fun equals(other: Any?) = other is Field && fieldName == other.fieldName && classFqName == other.classFqName override fun hashCode() = 31 * classFqName.hashCode() + fieldName.hashCode() } } -fun AnnotationWriter.writeAnnotatedElement(annotation: String, element: AnnotatedElementDescriptor) { +fun AnnotationWriter.writeAnnotatedElement(annotation: String, element: AnnotatedElement) { when (element) { - is AnnotatedElementDescriptor.Class -> { + is AnnotatedElement.Class -> { writeAnnotatedClass(annotation, element.classFqName) } - is AnnotatedElementDescriptor.Constructor -> { - writeAnnotatedMethod(annotation, element.classFqName, AnnotatedElementDescriptor.Constructor.METHOD_NAME) + is AnnotatedElement.Constructor -> { + writeAnnotatedMethod(annotation, element.classFqName, AnnotatedElement.Constructor.METHOD_NAME) } - is AnnotatedElementDescriptor.Method -> { + is AnnotatedElement.Method -> { writeAnnotatedMethod(annotation, element.classFqName, element.methodName) } - is AnnotatedElementDescriptor.Field -> { + is AnnotatedElement.Field -> { writeAnnotatedField(annotation, element.classFqName, element.fieldName) } } 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 5daae6ba86c..df4e466f2a8 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 @@ -27,13 +27,13 @@ open class KotlinAnnotationProvider(annotationsReader: Reader) { constructor() : this(StringReader("")) protected val kotlinClassesInternal = hashSetOf() - protected val annotatedKotlinElementsInternal = hashMapOf>() + protected val annotatedKotlinElementsInternal = hashMapOf>() init { readAnnotations(annotationsReader) } - val annotatedKotlinElements: Map> + val annotatedKotlinElements: Map> get() = annotatedKotlinElementsInternal val kotlinClasses: Set @@ -84,18 +84,18 @@ open class KotlinAnnotationProvider(annotationsReader: Reader) { val set = annotatedKotlinElementsInternal.getOrPut(annotationName) { HashSet() } set.add(when (type) { - Notation.ANNOTATED_CLASS -> AnnotatedElementDescriptor.Class(classFqName) + Notation.ANNOTATED_CLASS -> AnnotatedElement.Class(classFqName) Notation.ANNOTATED_FIELD -> { val name = elementName ?: throw AssertionError("Name for field must be provided") - AnnotatedElementDescriptor.Field(classFqName, name) + AnnotatedElement.Field(classFqName, name) } Notation.ANNOTATED_METHOD -> { val name = elementName ?: throw AssertionError("Name for method must be provided") - if (AnnotatedElementDescriptor.Constructor.METHOD_NAME == name) - AnnotatedElementDescriptor.Constructor(classFqName) + if (AnnotatedElement.Constructor.METHOD_NAME == name) + AnnotatedElement.Constructor(classFqName) else - AnnotatedElementDescriptor.Method(classFqName, name) + AnnotatedElement.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 e5c51bda789..66ed0b66e14 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 @@ -66,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 AnnotatedElementDescriptor.Class -> set.add(clazz) - is AnnotatedElementDescriptor.Constructor -> { + is AnnotatedElement.Class -> set.add(clazz) + is AnnotatedElement.Constructor -> { set.addAll(clazz.filterEnclosedElements(ElementKind.CONSTRUCTOR) .filter { it.hasAnnotation(annotationFqName) }) } - is AnnotatedElementDescriptor.Field -> { + is AnnotatedElement.Field -> { set.addAll(clazz.filterEnclosedElements(ElementKind.FIELD, descriptor.fieldName) .filter { it.hasAnnotation(annotationFqName) }) } - is AnnotatedElementDescriptor.Method -> { + is AnnotatedElement.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 62bba854fb0..800fb82c38b 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 @@ -67,10 +67,13 @@ open class AnnotationListParseTest { for (element in it.value) { actualAnnotations.append(it.key).append(' ').append(element.classFqName) when (element) { - is AnnotatedElementDescriptor.Method -> actualAnnotations.append(' ').append(element.methodName) - is AnnotatedElementDescriptor.Field -> actualAnnotations.append(' ').append(element.fieldName) - is AnnotatedElementDescriptor.Constructor -> actualAnnotations.append(" ${AnnotatedElementDescriptor.Constructor.METHOD_NAME}") - is AnnotatedElementDescriptor.Class -> {} + is AnnotatedElement.Method -> + actualAnnotations.append(' ').append(element.methodName) + is AnnotatedElement.Field -> + actualAnnotations.append(' ').append(element.fieldName) + is AnnotatedElement.Constructor -> + actualAnnotations.append(" ${AnnotatedElement.Constructor.METHOD_NAME}") + is AnnotatedElement.Class -> {} } actualAnnotations.append('\n') }