Update kapt annotations file incrementally

This commit is contained in:
Alexey Tsvetkov
2016-04-25 17:35:06 +03:00
parent c21693b0d0
commit fe6e69ff89
17 changed files with 276 additions and 30 deletions
@@ -2,7 +2,9 @@ package org.jetbrains.kotlin.annotation
sealed class AnnotatedElementDescriptor(val classFqName: String) {
class Class(classFqName: String) : AnnotatedElementDescriptor(classFqName) {
// use referential equality
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) {
@@ -15,7 +17,10 @@ sealed class AnnotatedElementDescriptor(val classFqName: String) {
companion object {
const val METHOD_NAME = "<init>"
}
// use referential equality
override fun equals(other: Any?) = other is Constructor && classFqName == other.classFqName
override fun hashCode() = 31 * classFqName.hashCode() + METHOD_NAME.hashCode()
}
class Field(classFqName: String, val fieldName: String) : AnnotatedElementDescriptor(classFqName) {
@@ -22,12 +22,12 @@ import java.io.StringReader
import java.util.*
import org.jetbrains.kotlin.annotation.CompactNotationType as Notation
class KotlinAnnotationProvider(annotationsReader: Reader) {
open class KotlinAnnotationProvider(annotationsReader: Reader) {
constructor(annotationsFile: File) : this(annotationsFile.reader().buffered())
constructor() : this(StringReader(""))
private val kotlinClassesInternal = hashSetOf<String>()
private val annotatedKotlinElementsInternal = hashMapOf<String, MutableSet<AnnotatedElementDescriptor>>()
protected val kotlinClassesInternal = hashSetOf<String>()
protected val annotatedKotlinElementsInternal = hashMapOf<String, MutableSet<AnnotatedElementDescriptor>>()
init {
readAnnotations(annotationsReader)
@@ -42,7 +42,7 @@ class KotlinAnnotationProvider(annotationsReader: Reader) {
val supportInheritedAnnotations: Boolean
get() = kotlinClassesInternal.isNotEmpty()
private fun readAnnotations(annotationsReader: Reader) {
protected fun readAnnotations(annotationsReader: Reader) {
fun handleShortenedName(cache: MutableMap<String, String>, lineParts: List<String>) {
val name = lineParts[1]
val id = lineParts[2]
@@ -0,0 +1,35 @@
/*
* Copyright 2010-2015 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.kotlin.annotation
import java.io.File
import java.io.StringReader
import org.jetbrains.kotlin.annotation.CompactNotationType as Notation
class MutableKotlinAnnotationProvider : KotlinAnnotationProvider(StringReader("")) {
fun addAnnotationsFrom(file: File) {
file.bufferedReader().use { readAnnotations(it) }
}
fun removeClasses(classesFqNames: Set<String>) {
kotlinClassesInternal.removeAll(classesFqNames)
for ((annotation, elements) in annotatedKotlinElementsInternal) {
elements.removeAll { it.classFqName in classesFqNames }
}
}
}
@@ -1,7 +1,5 @@
package org.jetbrains.kotlin.annotation
import org.junit.Assert
import org.junit.Assert.assertEquals
import org.junit.Assert.assertTrue
import org.junit.Test
import java.io.File
@@ -84,20 +82,4 @@ open class AnnotationListParseTest {
val fileContents = (actualAnnotationsSorted + classDeclarationsSorted).joinToString("\n")
assertEqualsToFile(expectedFile, fileContents)
}
// KotlinTestUtils.assertEqualsToFile() is not reachable from here
public fun assertEqualsToFile(expectedFile: File, actual: String) {
val lineSeparator = System.getProperty("line.separator")
val actualText = actual.replace(lineSeparator, "\n").trim('\n', ' ', '\t')
if (!expectedFile.exists()) {
expectedFile.writeText(actualText.replace("\n", lineSeparator))
Assert.fail("Expected data file did not exist. Generating: " + expectedFile)
}
val expectedText = expectedFile.readText().replace(lineSeparator, "\n")
assertEquals(expectedText, actualText)
}
}
@@ -29,14 +29,10 @@ class AnnotationSerializationTest : AnnotationListParseTest() {
val annotationProvider = KotlinAnnotationProvider(annotationsFile)
annotationsFile.delete()
val writer = annotationsFile.bufferedWriter()
try {
annotationsFile.bufferedWriter().use { writer ->
val annotationWriter = CompactAnnotationWriter(writer)
annotationProvider.writeAnnotations(annotationWriter)
}
finally {
writer.close()
}
super.doTest(workingDir)
}
@@ -0,0 +1,63 @@
/*
* Copyright 2010-2016 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.kotlin.annotation
import org.junit.Test
import java.io.File
import java.io.StringWriter
class MutableAnnotationProviderTest {
private val resourcesRootFile = File("src/test/resources/mutate")
@Test
fun testRemoveClass() {
val testDir = File(resourcesRootFile, "removeClass")
val annotationsFile = File(testDir, "annotations.txt")
val annotationsFileUpdated = File(testDir, "annotations-updated.txt")
val content = mutateAnnotationsFiles {
addAnnotationsFrom(annotationsFile)
removeClasses(setOf("foo.A"))
}
assertEqualsToFile(annotationsFileUpdated, content)
}
@Test
fun testMergeFiles() {
val testDir = File(resourcesRootFile, "mergeFiles")
val annotationsAFile = File(testDir, "annotationsA.txt")
val annotationsBFile = File(testDir, "annotationsB.txt")
val annotationsMergedFile = File(testDir, "annotations-merged.txt")
val content = mutateAnnotationsFiles {
addAnnotationsFrom(annotationsAFile)
addAnnotationsFrom(annotationsBFile)
}
assertEqualsToFile(annotationsMergedFile, content)
}
private fun mutateAnnotationsFiles(fn: MutableKotlinAnnotationProvider.() -> Unit): String {
val annotationProvider = MutableKotlinAnnotationProvider()
annotationProvider.fn()
val writer = StringWriter()
annotationProvider.writeAnnotations(CompactAnnotationWriter(writer))
return writer.toString()
}
}
@@ -0,0 +1,18 @@
package org.jetbrains.kotlin.annotation
import org.junit.Assert
import java.io.File
fun assertEqualsToFile(expectedFile: File, actual: String) {
val lineSeparator = System.getProperty("line.separator")
val actualText = actual.replace(lineSeparator, "\n").trim('\n', ' ', '\t')
if (!expectedFile.exists()) {
expectedFile.writeText(actualText.replace("\n", lineSeparator))
Assert.fail("Expected data file did not exist. Generating: " + expectedFile)
}
val expectedText = expectedFile.readText().replace(lineSeparator, "\n")
Assert.assertEquals(expectedText, actualText)
}
@@ -0,0 +1,15 @@
a annotations.Ann 0
p foo 0
m 0 0/A funA
p bar 1
c 0 1/B
c 0 0/A
m 0 1/B funB
m 0 0/A valA$annotations
m 0 1/B valB$annotations
a java.lang.annotation.Retention 1
p annotations 2
c 1 2/Ann
d 0/A
d 1/B
d 2/Ann
@@ -0,0 +1,10 @@
p foo 0
d 0/A
a annotations.Ann 0
c 0 0/A
m 0 0/A valA$annotations
m 0 0/A funA
p annotations 1
d 1/Ann
a java.lang.annotation.Retention 1
c 1 1/Ann
@@ -0,0 +1,10 @@
p bar 0
d 0/B
a annotations.Ann 0
c 0 0/B
m 0 0/B valB$annotations
m 0 0/B funB
p annotations 1
d 1/Ann
a java.lang.annotation.Retention 1
c 1 1/Ann
@@ -0,0 +1,10 @@
a annotations.Ann 0
p bar 0
c 0 0/B
m 0 0/B funB
m 0 0/B valB$annotations
a java.lang.annotation.Retention 1
p annotations 1
c 1 1/Ann
d 0/B
d 1/Ann
@@ -0,0 +1,15 @@
p bar 0
d 0/B
a annotations.Ann 0
c 0 0/B
m 0 0/B valB$annotations
m 0 0/B funB
p foo 1
d 1/A
c 0 1/A
m 0 1/A valA$annotations
m 0 1/A funA
p annotations 2
d 2/Ann
a java.lang.annotation.Retention 1
c 1 2/Ann
@@ -1,2 +1 @@
java.lang.Deprecated org.test.SomeClass <init>
java.lang.Deprecated org.test.SomeClass <init>