Skip to content

Commit d495c92

Browse files
committed
Add another way to find the psi class (#470)
1 parent 82a4b26 commit d495c92

File tree

1 file changed

+31
-13
lines changed

1 file changed

+31
-13
lines changed
Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,43 @@
11
package org.utbot.intellij.plugin.sarif
22

3+
import com.intellij.psi.JavaPsiFacade
4+
import com.intellij.psi.PsiClass
5+
import org.jetbrains.kotlin.idea.search.allScope
36
import org.utbot.common.PathUtil.classFqnToPath
47
import org.utbot.common.PathUtil.safeRelativize
58
import org.utbot.common.PathUtil.toPath
69
import org.utbot.sarif.SourceFindingStrategy
7-
import com.intellij.psi.JavaPsiFacade
8-
import com.intellij.psi.PsiClass
9-
import org.jetbrains.kotlin.idea.search.allScope
1010
import java.io.File
1111

1212
/**
13-
* The search strategy based on the information available to the PsiClass
13+
* The search strategy based on the information available to the PsiClass.
1414
*/
1515
class SourceFindingStrategyIdea(testClass: PsiClass) : SourceFindingStrategy() {
1616

1717
/**
18-
* Returns the relative path (against `project.basePath`) to the file with generated tests
18+
* Returns the relative path (against `project.basePath`) to the file with generated tests.
1919
*/
2020
override val testsRelativePath: String
2121
get() = safeRelativize(project.basePath, testsFilePath)
2222
?: testsFilePath.toPath().fileName.toString()
2323

2424
/**
25-
* Returns the relative path (against `project.basePath`) to the source file containing the class [classFqn]
25+
* Returns the relative path (against `project.basePath`) to the source file containing the class [classFqn].
2626
*/
27-
override fun getSourceRelativePath(classFqn: String, extension: String?): String =
28-
JavaPsiFacade.getInstance(project)
29-
.findClass(classFqn, project.allScope())?.let { psiClass ->
30-
safeRelativize(project.basePath, psiClass.containingFile.virtualFile.path)
31-
} ?: (classFqnToPath(classFqn) + (extension ?: defaultExtension))
27+
override fun getSourceRelativePath(classFqn: String, extension: String?): String {
28+
val psiClass = findPsiClass(classFqn)
29+
val absolutePath = psiClass?.containingFile?.virtualFile?.path
30+
val relativePath = safeRelativize(project.basePath, absolutePath)
31+
val defaultRelativePath = classFqnToPath(classFqn) + (extension ?: defaultExtension)
32+
return relativePath ?: defaultRelativePath
33+
}
3234

3335
/**
3436
* Finds the source file containing the class [classFqn].
3537
* Returns null if the file does not exist.
3638
*/
3739
override fun getSourceFile(classFqn: String, extension: String?): File? {
38-
val psiClass = JavaPsiFacade.getInstance(project).findClass(classFqn, project.allScope())
40+
val psiClass = findPsiClass(classFqn)
3941
val sourceCodeFile = psiClass?.containingFile?.virtualFile?.path?.let(::File)
4042
return if (sourceCodeFile?.exists() == true) sourceCodeFile else null
4143
}
@@ -48,7 +50,23 @@ class SourceFindingStrategyIdea(testClass: PsiClass) : SourceFindingStrategy() {
4850

4951
/**
5052
* The file extension to be used in [getSourceRelativePath] if the source file
51-
* was not found by the class qualified name and the `extension` parameter is null
53+
* was not found by the class qualified name and the `extension` parameter is null.
5254
*/
5355
private val defaultExtension = "." + (testClass.containingFile.virtualFile.extension ?: "java")
56+
57+
/**
58+
* Returns PsiClass by given [classFqn].
59+
*/
60+
private fun findPsiClass(classFqn: String): PsiClass? {
61+
val psiFacade = JavaPsiFacade.getInstance(project)
62+
val psiClass = psiFacade.findClass(classFqn, project.allScope())
63+
if (psiClass != null)
64+
return psiClass
65+
66+
// If for some reason `psiClass` was not found by the `findClass` method
67+
val packageName = classFqn.substringBeforeLast('.')
68+
val shortClassName = classFqn.substringAfterLast('.')
69+
val neededPackage = psiFacade.findPackage(packageName)
70+
return neededPackage?.classes?.firstOrNull { it.name == shortClassName }
71+
}
5472
}

0 commit comments

Comments
 (0)