Skip to content

Commit ad604b8

Browse files
committed
fix possible NPE in JavaCodeVisionProvider
1 parent c8b4827 commit ad604b8

File tree

2 files changed

+32
-12
lines changed

2 files changed

+32
-12
lines changed

ide-common/src/main/java/org/digma/intellij/plugin/psi/PsiUtils.java

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,30 +8,32 @@
88
import com.intellij.psi.PsiManager;
99
import org.jetbrains.annotations.NotNull;
1010

11-
/**
12-
* Query and navigate PSI elements.
13-
* This is the only class dealing with language specific services.
14-
*/
11+
1512
public class PsiUtils {
1613

1714
private PsiUtils() {
1815
}
1916

20-
// @Nullable
21-
// public static MethodUnderCaret detectMethodUnderCaret(Project project, LanguageService languageService, int caretOffset, VirtualFile file) {
22-
// PsiFile psiFile = PsiManager.getInstance(project).findFile(file);
23-
// return languageService.detectMethodUnderCaret(project, psiFile, caretOffset);
24-
// }
2517

2618

2719
/*
28-
Note: our DocumentInfo object has a fileUri which is a uri including the 'file:' schema and is used to convert PsiFile
29-
to and from string. it is mainly used to store the file uri that the document belongs to and when necessary file the
30-
PsiFile.
20+
Note: our DocumentInfo object has a fileUri which is an uri including the 'file:' schema and is used to convert PsiFile
21+
to and from string. it is mainly used to store the file uri that the document belongs to and to find the file when
22+
necessary.
3123
*/
3224

3325
@NotNull
3426
public static String psiFileToUri(@NotNull PsiFile psiFile) {
27+
28+
//usually this method should only be called for source files in the project and then
29+
//psiFile.getVirtualFile() will never return null.
30+
//this is a fallback to protect against NPE in case we have some bug somewhere, psiFile.getName
31+
//will never help us, but we will probably discover the bug in some other way because things will not work
32+
//correctly with psiFile.getName.
33+
if (psiFile.getVirtualFile() == null){
34+
return psiFile.getName();
35+
}
36+
3537
return psiFile.getVirtualFile().getUrl();
3638
}
3739

java/src/main/kotlin/org/digma/intellij/plugin/idea/psi/java/JavaCodeVisionProvider.kt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ import com.intellij.psi.*
99
abstract class JavaCodeVisionProvider: JavaCodeVisionProviderBase() {
1010

1111

12+
internal val empty: List<Pair<TextRange, CodeVisionEntry>> = listOf()
13+
14+
1215
override val defaultAnchor: CodeVisionAnchorKind
1316
get() = CodeVisionAnchorKind.Top
1417

@@ -33,6 +36,11 @@ abstract class JavaCodeVisionProvider: JavaCodeVisionProviderBase() {
3336
get() = listOf(CodeVisionRelativeOrdering.CodeVisionRelativeOrderingFirst)
3437

3538
override fun computeLenses(editor: Editor, psiFile: PsiFile): List<Pair<TextRange, CodeVisionEntry>> {
39+
40+
if (psiFile.virtualFile == null){
41+
return empty
42+
}
43+
3644
editor.project?.let {
3745
val javaCodeLensService = editor.project!!.getService(JavaCodeLensService::class.java)
3846
return javaCodeLensService.getErrorHotspotCodeLens(psiFile)
@@ -63,6 +71,11 @@ abstract class JavaCodeVisionProvider: JavaCodeVisionProviderBase() {
6371
get() = listOf(CodeVisionRelativeOrdering.CodeVisionRelativeOrderingAfter(ErrorHotspot.ID))
6472

6573
override fun computeLenses(editor: Editor, psiFile: PsiFile): List<Pair<TextRange, CodeVisionEntry>> {
74+
75+
if (psiFile.virtualFile == null){
76+
return empty
77+
}
78+
6679
editor.project?.let {
6780
val javaCodeLensService = editor.project!!.getService(JavaCodeLensService::class.java)
6881
return javaCodeLensService.getHighUsageCodeLens(psiFile)
@@ -92,6 +105,11 @@ abstract class JavaCodeVisionProvider: JavaCodeVisionProviderBase() {
92105
get() = listOf(CodeVisionRelativeOrdering.CodeVisionRelativeOrderingAfter(HighUsage.ID))
93106

94107
override fun computeLenses(editor: Editor, psiFile: PsiFile): List<Pair<TextRange, CodeVisionEntry>> {
108+
109+
if (psiFile.virtualFile == null){
110+
return empty
111+
}
112+
95113
editor.project?.let {
96114
val javaCodeLensService = editor.project!!.getService(JavaCodeLensService::class.java)
97115
return javaCodeLensService.getLowUsageCodeLens(psiFile)

0 commit comments

Comments
 (0)