1
1
package org.utbot.intellij.plugin.sarif
2
2
3
+ import com.intellij.psi.JavaPsiFacade
4
+ import com.intellij.psi.PsiClass
5
+ import org.jetbrains.kotlin.idea.search.allScope
3
6
import org.utbot.common.PathUtil.classFqnToPath
4
7
import org.utbot.common.PathUtil.safeRelativize
5
8
import org.utbot.common.PathUtil.toPath
6
9
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
10
10
import java.io.File
11
11
12
12
/* *
13
- * The search strategy based on the information available to the PsiClass
13
+ * The search strategy based on the information available to the PsiClass.
14
14
*/
15
15
class SourceFindingStrategyIdea (testClass : PsiClass ) : SourceFindingStrategy() {
16
16
17
17
/* *
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.
19
19
*/
20
20
override val testsRelativePath: String
21
21
get() = safeRelativize(project.basePath, testsFilePath)
22
22
? : testsFilePath.toPath().fileName.toString()
23
23
24
24
/* *
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].
26
26
*/
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
+ }
32
34
33
35
/* *
34
36
* Finds the source file containing the class [classFqn].
35
37
* Returns null if the file does not exist.
36
38
*/
37
39
override fun getSourceFile (classFqn : String , extension : String? ): File ? {
38
- val psiClass = JavaPsiFacade .getInstance(project).findClass( classFqn, project.allScope() )
40
+ val psiClass = findPsiClass( classFqn)
39
41
val sourceCodeFile = psiClass?.containingFile?.virtualFile?.path?.let (::File )
40
42
return if (sourceCodeFile?.exists() == true ) sourceCodeFile else null
41
43
}
@@ -48,7 +50,23 @@ class SourceFindingStrategyIdea(testClass: PsiClass) : SourceFindingStrategy() {
48
50
49
51
/* *
50
52
* 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.
52
54
*/
53
55
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
+ }
54
72
}
0 commit comments