Skip to content

Commit 37e4b89

Browse files
committed
refactor(java-auto-test): use read action to ensure thread safety
The commit refactors the `JavaAutoTestService` to use `runReadAction` for accessing `PsiFile` and `ProjectFileIndex`, ensuring that these operations are performed in a read-only context and are thread-safe. This change addresses an issue where concurrent modifications to the file system or project structure could lead to inconsistent results when reading `PsiFile` and `ProjectFileIndex` data.
1 parent b2ee9ff commit 37e4b89

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

java/src/main/kotlin/cc/unitmesh/idea/service/JavaAutoTestService.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,12 +162,12 @@ class JavaAutoTestService : AutoTestService() {
162162
val name = virtualFile.name
163163

164164
val psiFile: PsiJavaFile = PsiManager.getInstance(project).findFile(virtualFile) as? PsiJavaFile ?: return null
165-
val canonicalName = psiFile.packageName + "." + virtualFile.nameWithoutExtension
165+
val canonicalName = runReadAction { psiFile.packageName + "." + virtualFile.nameWithoutExtension }
166166

167167
val runManager = RunManager.getInstance(project)
168168

169169
var moduleName = ""
170-
val moduleForFile = ProjectFileIndex.getInstance(project).getModuleForFile(virtualFile)
170+
val moduleForFile = runReadAction { ProjectFileIndex.getInstance(project).getModuleForFile(virtualFile) }
171171
// a moduleForFile.name will be like <project>.<module>.<testModule>, so we need to remove the last part and first part
172172
if (moduleForFile != null) {
173173
val moduleNameSplit = moduleForFile.name.split(".").drop(1).dropLast(1).joinToString(":")

0 commit comments

Comments
 (0)