Read annotations during construction of KotlinAnnotationsProvider

The rationale is to access kotlinClassesInternal and annotatedKotlinElementsInternal
in any order
This commit is contained in:
Alexey Tsvetkov
2016-04-15 01:48:37 +03:00
parent 69a8654e91
commit 3340731c71
@@ -36,19 +36,23 @@ class KotlinAnnotationProvider(private val annotationsReader: Reader) {
constructor(annotationsFile: File) : this(annotationsFile.reader().buffered()) constructor(annotationsFile: File) : this(annotationsFile.reader().buffered())
constructor() : this(StringReader("")) constructor() : this(StringReader(""))
public val annotatedKotlinElements: Map<String, Set<AnnotatedElementDescriptor>> by lazy { private val kotlinClassesInternal = hashSetOf<String>()
private val annotatedKotlinElementsInternal = hashMapOf<String, MutableSet<AnnotatedElementDescriptor>>()
init {
readAnnotations() readAnnotations()
} }
private val kotlinClassesInternal = hashSetOf<String>() val annotatedKotlinElements: Map<String, Set<AnnotatedElementDescriptor>>
get() = annotatedKotlinElementsInternal
public val kotlinClasses: Set<String> val kotlinClasses: Set<String>
get() = kotlinClassesInternal get() = kotlinClassesInternal
public val supportInheritedAnnotations: Boolean val supportInheritedAnnotations: Boolean
get() = kotlinClassesInternal.isNotEmpty() get() = kotlinClassesInternal.isNotEmpty()
private fun readAnnotations(): MutableMap<String, MutableSet<AnnotatedElementDescriptor>> { private fun readAnnotations() {
val shortenedAnnotationCache = hashMapOf<String, String>() val shortenedAnnotationCache = hashMapOf<String, String>()
val shortenedPackageNameCache = hashMapOf<String, String>() val shortenedPackageNameCache = hashMapOf<String, String>()
@@ -63,8 +67,6 @@ class KotlinAnnotationProvider(private val annotationsReader: Reader) {
return shortenedValue + '.' + s.substring(id.length + 1) return shortenedValue + '.' + s.substring(id.length + 1)
} }
val annotatedKotlinElements: MutableMap<String, MutableSet<AnnotatedElementDescriptor>> = hashMapOf()
annotationsReader.useLines { lines -> annotationsReader.useLines { lines ->
for (line in lines) { for (line in lines) {
if (line.isEmpty()) continue if (line.isEmpty()) continue
@@ -84,7 +86,7 @@ class KotlinAnnotationProvider(private val annotationsReader: Reader) {
val classFqName = expandClassName(lineParts[2]).replace('$', '.') val classFqName = expandClassName(lineParts[2]).replace('$', '.')
val elementName = if (lineParts.size == 4) lineParts[3] else null val elementName = if (lineParts.size == 4) lineParts[3] else null
val set = annotatedKotlinElements.getOrPut(annotationName) { hashSetOf() } val set = annotatedKotlinElementsInternal.getOrPut(annotationName) { hashSetOf() }
set.add(when (type) { set.add(when (type) {
ANNOTATED_CLASS -> AnnotatedClassDescriptor(classFqName) ANNOTATED_CLASS -> AnnotatedClassDescriptor(classFqName)
ANNOTATED_FIELD -> { ANNOTATED_FIELD -> {
@@ -107,8 +109,6 @@ class KotlinAnnotationProvider(private val annotationsReader: Reader) {
} }
} }
} }
return annotatedKotlinElements
} }
private fun handleShortenedName(cache: MutableMap<String, String>, lineParts: List<String>) { private fun handleShortenedName(cache: MutableMap<String, String>, lineParts: List<String>) {