Do not track defined constants in source files

There is no need to track definitions of constants in sources for now.
References to constants are tracked in order to capture dependencies between types.
This ensures that any change to a type defining a constant (either from the
classpath or sources), will trigger reprocessing of the type that uses the
constant.
This commit is contained in:
Ivan Gavrilovic
2019-04-10 18:51:09 +01:00
committed by Alexey Tsvetkov
parent 929fca03fd
commit e171199bf5
5 changed files with 15 additions and 39 deletions
@@ -98,16 +98,6 @@ class JavaClassCache() : Serializable {
val allDirtyTypes = mutableSetOf<String>()
// check for constants first because they cause full rebuilt
for (sourceChange in currentDirtyFiles) {
val structure = sourceCache[sourceChange] ?: continue
if (structure.getDefinedConstants().isNotEmpty()) {
// TODO(gavra): compare constant values, and only full rebuild if the value changes
invalidateAll()
return SourcesToReprocess.FullRebuild
}
}
while (currentDirtyFiles.isNotEmpty()) {
val nextRound = mutableSetOf<URI>()
@@ -176,14 +166,12 @@ class SourceFileStructure(
private val mentionedTypes: MutableSet<String> = mutableSetOf()
private val privateTypes: MutableSet<String> = mutableSetOf()
private val definedConstants: MutableMap<String, Any> = mutableMapOf()
private val mentionedAnnotations: MutableSet<String> = mutableSetOf()
private val mentionedConstants: MutableMap<String, MutableSet<String>> = mutableMapOf()
fun getDeclaredTypes(): Set<String> = declaredTypes
fun getMentionedTypes(): Set<String> = mentionedTypes
fun getPrivateTypes(): Set<String> = privateTypes
fun getDefinedConstants(): Map<String, Any> = definedConstants
fun getMentionedAnnotations(): Set<String> = mentionedAnnotations
fun getMentionedConstants(): Map<String, Set<String>> = mentionedConstants
@@ -197,10 +185,6 @@ class SourceFileStructure(
}
}
fun addDefinedConstant(name: String, value: Any) {
definedConstants[name] = value
}
fun addMentionedAnnotations(name: String) {
mentionedAnnotations.add(name)
}
@@ -114,13 +114,6 @@ private class TypeTreeVisitor(val elementUtils: Elements, val trees: Trees, val
node.sym.constValue?.let { constValue ->
constantTreeVisitor.scan(trees.getPath(compilationUnit, node.init), null)
if (flags.contains(Modifier.FINAL)
&& flags.contains(Modifier.STATIC)
&& !flags.contains(Modifier.PRIVATE)
) {
sourceStructure.addDefinedConstant(node.sym.simpleName.toString(), constValue)
}
}
if (flags.contains(Modifier.PRIVATE)) Visibility.NON_ABI else Visibility.ABI
} else {
@@ -62,6 +62,5 @@ class TestInheritedAnnotation {
"test.BaseClass"
), shouldInheritAnnotation.getMentionedTypes()
)
assertEquals(emptyMap<String, String>(), shouldInheritAnnotation.getDefinedConstants())
}
}
@@ -136,24 +136,31 @@ class JavaClassCacheManagerTest {
}
@Test
fun testDefinesConstant() {
fun testReferencedConstant() {
SourceFileStructure(File("Constants.java").toURI()).also {
it.addDeclaredType("test.Constants")
it.addDefinedConstant("CONST", 123)
cache.javaCache.addSourceStructure(it)
}
SourceFileStructure(File("Unrelated1.java").toURI()).also {
it.addDeclaredType("test.Unrelated1")
SourceFileStructure(File("MentionsConst.java").toURI()).also {
it.addDeclaredType("test.MentionsConst")
it.addMentionedConstant("test.Constants", "CONST")
cache.javaCache.addSourceStructure(it)
}
SourceFileStructure(File("Unrelated2.java").toURI()).also {
it.addDeclaredType("test.Unrelated2")
SourceFileStructure(File("MentionsOtherConst.java").toURI()).also {
it.addDeclaredType("test.MentionsOtherConst")
it.addMentionedConstant("test.OtherConstants", "CONST")
cache.javaCache.addSourceStructure(it)
}
prepareForIncremental()
val dirtyFiles = cache.invalidateAndGetDirtyFiles(listOf(File("Constants.java")), emptyList())
assertEquals(SourcesToReprocess.FullRebuild, dirtyFiles)
val dirtyFiles =
cache.invalidateAndGetDirtyFiles(
listOf(File("Constants.java")), emptyList()
) as SourcesToReprocess.Incremental
assertEquals(
listOf(File("Constants.java").absoluteFile, File("MentionsConst.java").absoluteFile),
dirtyFiles.toReprocess
)
}
@Test
@@ -59,7 +59,6 @@ class TestComplexIncrementalAptCache {
assertEquals(emptySet<String>(), myEnum.getMentionedAnnotations())
assertEquals(emptySet<String>(), myEnum.getPrivateTypes())
assertEquals(setOf("test.MyEnum", "test.TypeGeneratedByApt"), myEnum.getMentionedTypes())
assertEquals(emptyMap<String, Any>(), myEnum.getDefinedConstants())
}
@Test
@@ -113,7 +112,6 @@ class TestComplexIncrementalAptCache {
"java.util.HashSet"
), myNumber.getMentionedTypes()
)
assertEquals(emptyMap<String, String>(), myNumber.getDefinedConstants())
}
@Test
@@ -132,7 +130,6 @@ class TestComplexIncrementalAptCache {
"test.NumberManager"
), numberAnnotation.getMentionedTypes()
)
assertEquals(emptyMap<String, String>(), numberAnnotation.getDefinedConstants())
}
@Test
@@ -143,7 +140,6 @@ class TestComplexIncrementalAptCache {
assertEquals(emptySet<String>(), numberException.getMentionedAnnotations())
assertEquals(emptySet<String>(), numberException.getPrivateTypes())
assertEquals(setOf("test.NumberException", "java.lang.RuntimeException"), numberException.getMentionedTypes())
assertEquals(emptyMap<String, String>(), numberException.getDefinedConstants())
}
@Test
@@ -166,7 +162,6 @@ class TestComplexIncrementalAptCache {
"test.NumberException"
), numberHolder.getMentionedTypes()
)
assertEquals(emptyMap<String, String>(), numberHolder.getDefinedConstants())
}
@Test
@@ -183,7 +178,6 @@ class TestComplexIncrementalAptCache {
"test.NumberHolder"
), numberManager.getMentionedTypes()
)
assertEquals(mapOf("CONST" to "STRING_CONST", "INT_CONST" to 246), numberManager.getDefinedConstants())
}
@Test
@@ -203,6 +197,5 @@ class TestComplexIncrementalAptCache {
"java.lang.Number"
), genericNumber.getMentionedTypes()
)
assertEquals(emptyMap<String, String>(), genericNumber.getDefinedConstants())
}
}