From 2d1eb7546549ac9a64e09087a1d1108b9542fbdb Mon Sep 17 00:00:00 2001 From: Daniel Alome Date: Tue, 20 Oct 2026 23:22:40 +0100 Subject: [PATCH 1/2] style: spotless reformat of SourceClassTrie.kt, no functional change Editing the file pulls it under the Spotless ratchet. Kept separate from the ADFA-6468 fix. --- .../androidide/utils/SourceClassTrie.kt | 307 +++++++++--------- 1 file changed, 158 insertions(+), 149 deletions(-) diff --git a/shared/src/main/java/com/itsaky/androidide/utils/SourceClassTrie.kt b/shared/src/main/java/com/itsaky/androidide/utils/SourceClassTrie.kt index e7569b197e..682c64c9d4 100644 --- a/shared/src/main/java/com/itsaky/androidide/utils/SourceClassTrie.kt +++ b/shared/src/main/java/com/itsaky/androidide/utils/SourceClassTrie.kt @@ -28,153 +28,162 @@ import kotlin.io.path.nameWithoutExtension * * @author Akash Yadav */ -open class SourceClassTrie(root: SourcePackageNode = SourcePackageNode()) : ClassTrie(root) { - - companion object { - @JvmStatic - private val pkgNameMethod by lazy { - Class.forName("com.itsaky.androidide.projects.util.StringSearch") - .getDeclaredMethod("packageName", Path::class.java) - } - - @JvmStatic - private fun packageName(file: Path): String { - return try { - pkgNameMethod.invoke(null, file) as String - } catch (error: Throwable) { - throw RuntimeException(error) - } - } - } - - override fun append(name: String): Node { - throw UnsupportedOperationException("Method not supported!") - } - - open fun append(path: Path, sourceDir: Path): SourceNode { - val modified = Files.getLastModifiedTime(path).toInstant() - val packageName = packageName(path) - val segments = segments(packageName) - var node: SourcePackageNode = root as SourcePackageNode - var currentPath = sourceDir - for ((index, segment) in segments.withIndex()) { - if (segment.isEmpty() || !segment[0].isJavaIdentifierStart()) { - continue - } - - currentPath = currentPath.resolve(segment) - node = createNode(currentPath, node, segment, segments, index) - } - node.isClass = false - - val name = path.nameWithoutExtension - val klass = - SourceNode( - file = path, - packageName = packageName, - modified = modified, - parent = node, - name = name - ) - klass.isClass = true - node.children[name] = klass - return klass - } - - open fun createNode( - dir: Path, - node: SourcePackageNode, - segment: String, - segments: List, - index: Int - ) = node.createChild(dir, segment, segments.subList(0, index + 1).joinToString(separator = ".")) - - override fun createNode(node: Node, segment: String, segments: List, index: Int): Node { - throw UnsupportedOperationException("Method not supported") - } - - open fun allSources(): List { - return allClassNodes().filterIsInstance(SourceNode::class.java) - } - - fun findSource(file: Path): SourceNode? { - return findSourceInNode(file, root) - } - - fun findDirNode(dir: Path, node: Node): SourcePackageNode? { - var dirNode: SourcePackageNode? = null - for (child in node.children.values) { - if ( - child !is SourceNode && - child is SourcePackageNode && - DocumentUtils.isSameFile(child.dir, dir) - ) { - dirNode = child - } - - dirNode = dirNode ?: findDirNode(dir, child) - - if (dirNode != null) { - break - } - } - return dirNode - } - - fun getSourceFilesInDir(dir: Path): List { - val dirNode = findDirNode(dir, root) ?: return emptyList() - return dirNode.children.values.filterIsInstance(SourceNode::class.java) - } - - private fun findSourceInNode(file: Path, node: Node): SourceNode? { - - if (node is SourceNode && DocumentUtils.isSameFile(file, node.file)) { - return node - } - - for (child in node.children.values) { - if (child is SourceNode && DocumentUtils.isSameFile(file, child.file)) { - return child - } - - val childResult = findSourceInNode(file, child) - if (childResult != null) { - return childResult - } - } - return null - } - - open class SourceNode( - val file: Path, - val packageName: String, - val modified: Instant, - parent: SourcePackageNode? = null, - name: String - ) : Node(name = name, qualifiedName = "$packageName.$name", parent = parent) { - - override fun createChild(name: String, qualifiedName: String): Node { - throw UnsupportedOperationException("Method not supported!") - } - } - - open class SourcePackageNode( - val dir: Path, - parent: SourcePackageNode? = null, - name: String, - qualifiedName: String - ) : Node(name, qualifiedName, parent) { - - internal constructor() : this(Paths.get(""), null, "", "") - - override fun createChild(name: String, qualifiedName: String): Node { - throw UnsupportedOperationException("Method not suported!") - } - - open fun createChild(dir: Path, name: String, qualifiedName: String): SourcePackageNode { - return this.children.computeIfAbsent(name) { - SourcePackageNode(dir, this, name, qualifiedName) - } as SourcePackageNode - } - } +open class SourceClassTrie( + root: SourcePackageNode = SourcePackageNode(), +) : ClassTrie(root) { + companion object { + @JvmStatic + private val pkgNameMethod by lazy { + Class + .forName("com.itsaky.androidide.projects.util.StringSearch") + .getDeclaredMethod("packageName", Path::class.java) + } + + @JvmStatic + private fun packageName(file: Path): String = + try { + pkgNameMethod.invoke(null, file) as String + } catch (error: Throwable) { + throw RuntimeException(error) + } + } + + override fun append(name: String): Node = throw UnsupportedOperationException("Method not supported!") + + open fun append( + path: Path, + sourceDir: Path, + ): SourceNode { + val modified = Files.getLastModifiedTime(path).toInstant() + val packageName = packageName(path) + val segments = segments(packageName) + var node: SourcePackageNode = root as SourcePackageNode + var currentPath = sourceDir + for ((index, segment) in segments.withIndex()) { + if (segment.isEmpty() || !segment[0].isJavaIdentifierStart()) { + continue + } + + currentPath = currentPath.resolve(segment) + node = createNode(currentPath, node, segment, segments, index) + } + node.isClass = false + + val name = path.nameWithoutExtension + val klass = + SourceNode( + file = path, + packageName = packageName, + modified = modified, + parent = node, + name = name, + ) + klass.isClass = true + node.children[name] = klass + return klass + } + + open fun createNode( + dir: Path, + node: SourcePackageNode, + segment: String, + segments: List, + index: Int, + ) = node.createChild(dir, segment, segments.subList(0, index + 1).joinToString(separator = ".")) + + override fun createNode( + node: Node, + segment: String, + segments: List, + index: Int, + ): Node = throw UnsupportedOperationException("Method not supported") + + open fun allSources(): List = allClassNodes().filterIsInstance(SourceNode::class.java) + + fun findSource(file: Path): SourceNode? = findSourceInNode(file, root) + + fun findDirNode( + dir: Path, + node: Node, + ): SourcePackageNode? { + var dirNode: SourcePackageNode? = null + for (child in node.children.values) { + if ( + child !is SourceNode && + child is SourcePackageNode && + DocumentUtils.isSameFile(child.dir, dir) + ) { + dirNode = child + } + + dirNode = dirNode ?: findDirNode(dir, child) + + if (dirNode != null) { + break + } + } + return dirNode + } + + fun getSourceFilesInDir(dir: Path): List { + val dirNode = findDirNode(dir, root) ?: return emptyList() + return dirNode.children.values.filterIsInstance(SourceNode::class.java) + } + + private fun findSourceInNode( + file: Path, + node: Node, + ): SourceNode? { + if (node is SourceNode && DocumentUtils.isSameFile(file, node.file)) { + return node + } + + for (child in node.children.values) { + if (child is SourceNode && DocumentUtils.isSameFile(file, child.file)) { + return child + } + + val childResult = findSourceInNode(file, child) + if (childResult != null) { + return childResult + } + } + return null + } + + open class SourceNode( + val file: Path, + val packageName: String, + val modified: Instant, + parent: SourcePackageNode? = null, + name: String, + ) : Node(name = name, qualifiedName = "$packageName.$name", parent = parent) { + override fun createChild( + name: String, + qualifiedName: String, + ): Node = throw UnsupportedOperationException("Method not supported!") + } + + open class SourcePackageNode( + val dir: Path, + parent: SourcePackageNode? = null, + name: String, + qualifiedName: String, + ) : Node(name, qualifiedName, parent) { + internal constructor() : this(Paths.get(""), null, "", "") + + override fun createChild( + name: String, + qualifiedName: String, + ): Node = throw UnsupportedOperationException("Method not suported!") + + open fun createChild( + dir: Path, + name: String, + qualifiedName: String, + ): SourcePackageNode = + this.children.computeIfAbsent(name) { + SourcePackageNode(dir, this, name, qualifiedName) + } as SourcePackageNode + } } From fb9ae94495e12ab40ce3e1b40e04189f3c60556d Mon Sep 17 00:00:00 2001 From: Daniel Alome Date: Tue, 20 Oct 2026 23:22:40 +0100 Subject: [PATCH 2/2] ADFA-6468: Let a source file and a package share a name in SourceClassTrie A package node keeps sub-packages and source files in one map keyed by simple name. A Java file named like a sibling package (Calculator.java next to package com.example.Calculator) collided with it: indexed first, the file made createChild's cast throw ClassCastException during project indexing; indexed second, it overwrote the package and dropped every source in it from the index. Packages now keep the simple-name key and a clashing file is stored under its full file name, which cannot collide since package names have no dot. Package and class lookups by name keep working. removeChild removes by identity so deleting the clashing file leaves the package, and all three operations stay atomic under the node's lock. --- .../androidide/utils/SourceClassTrie.kt | 38 +++++++- .../utils/SourceClassTrieNameClashTest.kt | 93 +++++++++++++++++++ 2 files changed, 127 insertions(+), 4 deletions(-) create mode 100644 subprojects/projects/src/test/java/com/itsaky/androidide/utils/SourceClassTrieNameClashTest.kt diff --git a/shared/src/main/java/com/itsaky/androidide/utils/SourceClassTrie.kt b/shared/src/main/java/com/itsaky/androidide/utils/SourceClassTrie.kt index 682c64c9d4..e4760e7af5 100644 --- a/shared/src/main/java/com/itsaky/androidide/utils/SourceClassTrie.kt +++ b/shared/src/main/java/com/itsaky/androidide/utils/SourceClassTrie.kt @@ -79,7 +79,7 @@ open class SourceClassTrie( name = name, ) klass.isClass = true - node.children[name] = klass + node.addSource(klass) return klass } @@ -182,8 +182,38 @@ open class SourceClassTrie( name: String, qualifiedName: String, ): SourcePackageNode = - this.children.computeIfAbsent(name) { - SourcePackageNode(dir, this, name, qualifiedName) - } as SourcePackageNode + synchronized(this) { + when (val existing = children[name]) { + is SourcePackageNode -> { + existing + } + + is SourceNode -> { + children[existing.file.fileName.toString()] = existing + SourcePackageNode(dir, this, name, qualifiedName).also { children[name] = it } + } + + null -> { + SourcePackageNode(dir, this, name, qualifiedName).also { children[name] = it } + } + + else -> { + error("Unexpected ${existing.javaClass.name} at '$qualifiedName'") + } + } + } + + fun addSource(source: SourceNode) { + synchronized(this) { + val key = if (children[source.name] is SourcePackageNode) source.file.fileName.toString() else source.name + children[key] = source + } + } + + override fun removeChild(child: Node) { + synchronized(this) { + children.values.remove(child) + } + } } } diff --git a/subprojects/projects/src/test/java/com/itsaky/androidide/utils/SourceClassTrieNameClashTest.kt b/subprojects/projects/src/test/java/com/itsaky/androidide/utils/SourceClassTrieNameClashTest.kt new file mode 100644 index 0000000000..809d47ee80 --- /dev/null +++ b/subprojects/projects/src/test/java/com/itsaky/androidide/utils/SourceClassTrieNameClashTest.kt @@ -0,0 +1,93 @@ +package com.itsaky.androidide.utils + +import com.google.common.truth.Truth.assertThat +import org.junit.Before +import org.junit.Rule +import org.junit.Test +import org.junit.rules.TemporaryFolder +import java.nio.file.Path + +class SourceClassTrieNameClashTest { + @get:Rule + val temp = TemporaryFolder() + + private lateinit var sourceDir: Path + private lateinit var calculator: Path + private lateinit var helper: Path + + @Before + fun setUp() { + sourceDir = temp.newFolder("java").toPath() + calculator = source("com/example/Calculator.java", "package com.example;\n\npublic class Calculator {}\n") + helper = source("com/example/Calculator/Helper.java", "package com.example.Calculator;\n\npublic class Helper {}\n") + } + + @Test + fun fileIndexedBeforeASameNamedPackageDoesNotCrash() { + val trie = SourceClassTrie() + + trie.append(calculator, sourceDir) + trie.append(helper, sourceDir) + + assertThat(trie.allSources().map { it.file }).containsExactly(calculator, helper) + } + + @Test + fun fileIndexedAfterASameNamedPackageKeepsThePackagesSources() { + val trie = SourceClassTrie() + + trie.append(helper, sourceDir) + trie.append(calculator, sourceDir) + + assertThat(trie.allSources().map { it.file }).containsExactly(calculator, helper) + } + + @Test + fun thePackageNameStillResolvesToThePackage() { + val trie = indexedTrie() + + val node = trie.findNode("com.example.Calculator") + + assertThat(node).isInstanceOf(SourceClassTrie.SourcePackageNode::class.java) + assertThat( + node!! + .children.values + .filterIsInstance() + .map { it.file }, + ).containsExactly(helper) + } + + @Test + fun theClashingFileIsStillListedInItsDirectory() { + val trie = indexedTrie() + + assertThat(trie.getSourceFilesInDir(calculator.parent).map { it.file }).containsExactly(calculator) + } + + @Test + fun removingTheClashingFileKeepsThePackage() { + val trie = indexedTrie() + + val source = trie.findSource(calculator)!! + source.parent!!.removeChild(source) + + assertThat(trie.findSource(calculator)).isNull() + assertThat(trie.findSource(helper)).isNotNull() + } + + private fun indexedTrie() = + SourceClassTrie().apply { + append(calculator, sourceDir) + append(helper, sourceDir) + } + + private fun source( + relativePath: String, + content: String, + ): Path { + val file = sourceDir.resolve(relativePath) + file.parent.toFile().mkdirs() + file.toFile().writeText(content) + return file + } +}