Refactoring: rename AnnotatedElementDescriptor->AnnotatedElement

This commit is contained in:
Alexey Tsvetkov
2016-04-27 14:54:55 +03:00
parent 6dd388d0f0
commit cb94936e51
4 changed files with 29 additions and 26 deletions
@@ -1,19 +1,19 @@
package org.jetbrains.kotlin.annotation package org.jetbrains.kotlin.annotation
sealed class AnnotatedElementDescriptor(val classFqName: String) { sealed class AnnotatedElement(val classFqName: String) {
class Class(classFqName: String) : AnnotatedElementDescriptor(classFqName) { class Class(classFqName: String) : AnnotatedElement(classFqName) {
override fun equals(other: Any?) = other is Class && classFqName == other.classFqName override fun equals(other: Any?) = other is Class && classFqName == other.classFqName
override fun hashCode() = classFqName.hashCode() 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 equals(other: Any?) = other is Method && methodName == other.methodName && classFqName == other.classFqName
override fun hashCode() = 31 * classFqName.hashCode() + methodName.hashCode() override fun hashCode() = 31 * classFqName.hashCode() + methodName.hashCode()
} }
class Constructor(classFqName: String) : AnnotatedElementDescriptor(classFqName) { class Constructor(classFqName: String) : AnnotatedElement(classFqName) {
companion object { companion object {
const val METHOD_NAME = "<init>" const val METHOD_NAME = "<init>"
} }
@@ -23,25 +23,25 @@ sealed class AnnotatedElementDescriptor(val classFqName: String) {
override fun hashCode() = 31 * classFqName.hashCode() + METHOD_NAME.hashCode() 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 equals(other: Any?) = other is Field && fieldName == other.fieldName && classFqName == other.classFqName
override fun hashCode() = 31 * classFqName.hashCode() + fieldName.hashCode() override fun hashCode() = 31 * classFqName.hashCode() + fieldName.hashCode()
} }
} }
fun AnnotationWriter.writeAnnotatedElement(annotation: String, element: AnnotatedElementDescriptor) { fun AnnotationWriter.writeAnnotatedElement(annotation: String, element: AnnotatedElement) {
when (element) { when (element) {
is AnnotatedElementDescriptor.Class -> { is AnnotatedElement.Class -> {
writeAnnotatedClass(annotation, element.classFqName) writeAnnotatedClass(annotation, element.classFqName)
} }
is AnnotatedElementDescriptor.Constructor -> { is AnnotatedElement.Constructor -> {
writeAnnotatedMethod(annotation, element.classFqName, AnnotatedElementDescriptor.Constructor.METHOD_NAME) writeAnnotatedMethod(annotation, element.classFqName, AnnotatedElement.Constructor.METHOD_NAME)
} }
is AnnotatedElementDescriptor.Method -> { is AnnotatedElement.Method -> {
writeAnnotatedMethod(annotation, element.classFqName, element.methodName) writeAnnotatedMethod(annotation, element.classFqName, element.methodName)
} }
is AnnotatedElementDescriptor.Field -> { is AnnotatedElement.Field -> {
writeAnnotatedField(annotation, element.classFqName, element.fieldName) writeAnnotatedField(annotation, element.classFqName, element.fieldName)
} }
} }
@@ -27,13 +27,13 @@ open class KotlinAnnotationProvider(annotationsReader: Reader) {
constructor() : this(StringReader("")) constructor() : this(StringReader(""))
protected val kotlinClassesInternal = hashSetOf<String>() protected val kotlinClassesInternal = hashSetOf<String>()
protected val annotatedKotlinElementsInternal = hashMapOf<String, MutableSet<AnnotatedElementDescriptor>>() protected val annotatedKotlinElementsInternal = hashMapOf<String, MutableSet<AnnotatedElement>>()
init { init {
readAnnotations(annotationsReader) readAnnotations(annotationsReader)
} }
val annotatedKotlinElements: Map<String, Set<AnnotatedElementDescriptor>> val annotatedKotlinElements: Map<String, Set<AnnotatedElement>>
get() = annotatedKotlinElementsInternal get() = annotatedKotlinElementsInternal
val kotlinClasses: Set<String> val kotlinClasses: Set<String>
@@ -84,18 +84,18 @@ open class KotlinAnnotationProvider(annotationsReader: Reader) {
val set = annotatedKotlinElementsInternal.getOrPut(annotationName) { HashSet() } val set = annotatedKotlinElementsInternal.getOrPut(annotationName) { HashSet() }
set.add(when (type) { set.add(when (type) {
Notation.ANNOTATED_CLASS -> AnnotatedElementDescriptor.Class(classFqName) Notation.ANNOTATED_CLASS -> AnnotatedElement.Class(classFqName)
Notation.ANNOTATED_FIELD -> { Notation.ANNOTATED_FIELD -> {
val name = elementName ?: throw AssertionError("Name for field must be provided") val name = elementName ?: throw AssertionError("Name for field must be provided")
AnnotatedElementDescriptor.Field(classFqName, name) AnnotatedElement.Field(classFqName, name)
} }
Notation.ANNOTATED_METHOD -> { Notation.ANNOTATED_METHOD -> {
val name = elementName ?: throw AssertionError("Name for method must be provided") val name = elementName ?: throw AssertionError("Name for method must be provided")
if (AnnotatedElementDescriptor.Constructor.METHOD_NAME == name) if (AnnotatedElement.Constructor.METHOD_NAME == name)
AnnotatedElementDescriptor.Constructor(classFqName) AnnotatedElement.Constructor(classFqName)
else else
AnnotatedElementDescriptor.Method(classFqName, name) AnnotatedElement.Method(classFqName, name)
} }
else -> throw AssertionError("Unknown type: $type") else -> throw AssertionError("Unknown type: $type")
}) })
@@ -66,16 +66,16 @@ internal class RoundEnvironmentWrapper(
val descriptorsWithKotlin = descriptors.fold(hashSetOf<Element>()) { set, descriptor -> val descriptorsWithKotlin = descriptors.fold(hashSetOf<Element>()) { set, descriptor ->
val clazz = processingEnv.elementUtils.getTypeElement(descriptor.classFqName) ?: return@fold set val clazz = processingEnv.elementUtils.getTypeElement(descriptor.classFqName) ?: return@fold set
when (descriptor) { when (descriptor) {
is AnnotatedElementDescriptor.Class -> set.add(clazz) is AnnotatedElement.Class -> set.add(clazz)
is AnnotatedElementDescriptor.Constructor -> { is AnnotatedElement.Constructor -> {
set.addAll(clazz.filterEnclosedElements(ElementKind.CONSTRUCTOR) set.addAll(clazz.filterEnclosedElements(ElementKind.CONSTRUCTOR)
.filter { it.hasAnnotation(annotationFqName) }) .filter { it.hasAnnotation(annotationFqName) })
} }
is AnnotatedElementDescriptor.Field -> { is AnnotatedElement.Field -> {
set.addAll(clazz.filterEnclosedElements(ElementKind.FIELD, descriptor.fieldName) set.addAll(clazz.filterEnclosedElements(ElementKind.FIELD, descriptor.fieldName)
.filter { it.hasAnnotation(annotationFqName) }) .filter { it.hasAnnotation(annotationFqName) })
} }
is AnnotatedElementDescriptor.Method -> { is AnnotatedElement.Method -> {
set.addAll(clazz.filterEnclosedElements(ElementKind.METHOD, descriptor.methodName) set.addAll(clazz.filterEnclosedElements(ElementKind.METHOD, descriptor.methodName)
.filter { it.hasAnnotation(annotationFqName) }) .filter { it.hasAnnotation(annotationFqName) })
} }
@@ -67,10 +67,13 @@ open class AnnotationListParseTest {
for (element in it.value) { for (element in it.value) {
actualAnnotations.append(it.key).append(' ').append(element.classFqName) actualAnnotations.append(it.key).append(' ').append(element.classFqName)
when (element) { when (element) {
is AnnotatedElementDescriptor.Method -> actualAnnotations.append(' ').append(element.methodName) is AnnotatedElement.Method ->
is AnnotatedElementDescriptor.Field -> actualAnnotations.append(' ').append(element.fieldName) actualAnnotations.append(' ').append(element.methodName)
is AnnotatedElementDescriptor.Constructor -> actualAnnotations.append(" ${AnnotatedElementDescriptor.Constructor.METHOD_NAME}") is AnnotatedElement.Field ->
is AnnotatedElementDescriptor.Class -> {} actualAnnotations.append(' ').append(element.fieldName)
is AnnotatedElement.Constructor ->
actualAnnotations.append(" ${AnnotatedElement.Constructor.METHOD_NAME}")
is AnnotatedElement.Class -> {}
} }
actualAnnotations.append('\n') actualAnnotations.append('\n')
} }