Skip to content

Commit

Permalink
Do not track defined constants in source files
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
gavra0 authored and ilmirus committed Apr 23, 2019
1 parent 01e8c7c commit b3be525
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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>()
Expand Down Expand Up @@ -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

Expand All @@ -197,10 +185,6 @@ class SourceFileStructure(
}
}

fun addDefinedConstant(name: String, value: Any) {
definedConstants[name] = value
}

fun addMentionedAnnotations(name: String) {
mentionedAnnotations.add(name)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,5 @@ class TestInheritedAnnotation {
"test.BaseClass"
), shouldInheritAnnotation.getMentionedTypes()
)
assertEquals(emptyMap<String, String>(), shouldInheritAnnotation.getDefinedConstants())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -113,7 +112,6 @@ class TestComplexIncrementalAptCache {
"java.util.HashSet"
), myNumber.getMentionedTypes()
)
assertEquals(emptyMap<String, String>(), myNumber.getDefinedConstants())
}

@Test
Expand All @@ -132,7 +130,6 @@ class TestComplexIncrementalAptCache {
"test.NumberManager"
), numberAnnotation.getMentionedTypes()
)
assertEquals(emptyMap<String, String>(), numberAnnotation.getDefinedConstants())
}

@Test
Expand All @@ -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
Expand All @@ -166,7 +162,6 @@ class TestComplexIncrementalAptCache {
"test.NumberException"
), numberHolder.getMentionedTypes()
)
assertEquals(emptyMap<String, String>(), numberHolder.getDefinedConstants())
}

@Test
Expand All @@ -183,7 +178,6 @@ class TestComplexIncrementalAptCache {
"test.NumberHolder"
), numberManager.getMentionedTypes()
)
assertEquals(mapOf("CONST" to "STRING_CONST", "INT_CONST" to 246), numberManager.getDefinedConstants())
}

@Test
Expand All @@ -203,6 +197,5 @@ class TestComplexIncrementalAptCache {
"java.lang.Number"
), genericNumber.getMentionedTypes()
)
assertEquals(emptyMap<String, String>(), genericNumber.getDefinedConstants())
}
}

0 comments on commit b3be525

Please sign in to comment.