Skip to content

Commit 172e11c

Browse files
committed
feat: support for locating missing class in Java.SymbolNotFoundError
1 parent 3665fc0 commit 172e11c

File tree

4 files changed

+63
-18
lines changed

4 files changed

+63
-18
lines changed

error_templates/java/symbol_not_found_error.go

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,12 @@ var SymbolNotFoundError = lib.ErrorTemplate{
2929
rootNode: m.Nearest,
3030
}
3131

32-
query := fmt.Sprintf("((identifier) @symbol (#eq? @symbol \"%s\"))", symbolName)
32+
nodeTypeToFind := "identifier"
33+
if errorCtx.symbolType == "class" {
34+
nodeTypeToFind = "type_identifier"
35+
}
36+
37+
query := fmt.Sprintf("((%s) @symbol (#eq? @symbol \"%s\"))", nodeTypeToFind, symbolName)
3338
lib.QueryNode(m.Nearest, strings.NewReader(query), func(ctx lib.QueryNodeCtx) bool {
3439
match := ctx.Cursor.FilterPredicates(ctx.Match, []byte(m.Nearest.Doc.Contents))
3540
for _, c := range match.Captures {
@@ -65,6 +70,8 @@ var SymbolNotFoundError = lib.ErrorTemplate{
6570
gen.Add(`The program cannot find variable "%s"`, ctx.symbolName)
6671
case "method":
6772
gen.Add("The error indicates that the compiler cannot find the method `%s` in the `%s` class.", ctx.symbolName, ctx.locationClass)
73+
case "class":
74+
gen.Add("The error indicates that the compiler cannot find the class `%s` when attempting to create an instance of it in the `%s` class.", ctx.symbolName, ctx.locationClass)
6875
}
6976
},
7077
OnGenBugFixFn: func(cd *lib.ContextData, gen *lib.BugFixGenerator) {
@@ -99,6 +106,21 @@ var SymbolNotFoundError = lib.ErrorTemplate{
99106
EndPosition: lastMethodNode.EndPosition().Add(lib.Position{Column: 1}), // same thing here
100107
})
101108
})
109+
case "class":
110+
gen.Add("Create the missing class", func(s *lib.BugFixSuggestion) {
111+
s.AddStep("Create a new class named `%s` to resolve the \"cannot find symbol\" error.", ctx.symbolName).
112+
AddFix(lib.FixSuggestion{
113+
NewText: fmt.Sprintf("class %s {\n\t// Add any necessary code for %s class\n}\n\n", ctx.symbolName, ctx.symbolName),
114+
StartPosition: lib.Position{
115+
Line: ctx.locationNode.StartPosition().Line,
116+
Column: 0,
117+
},
118+
EndPosition: lib.Position{
119+
Line: ctx.locationNode.StartPosition().Line,
120+
Column: 0,
121+
},
122+
})
123+
})
102124
}
103125
},
104126
}

error_templates/java/test_files/no_class_def_error/test.txt

Lines changed: 0 additions & 17 deletions
This file was deleted.
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
name: "MissingClass"
2+
template: "Java.SymbolNotFoundError"
3+
---
4+
MyClass.java:5: error: cannot find symbol
5+
NonExistingClass obj = new NonExistingClass();
6+
^
7+
symbol: class NonExistingClass
8+
location: class MyClass
9+
MyClass.java:5: error: cannot find symbol
10+
NonExistingClass obj = new NonExistingClass();
11+
^
12+
symbol: class NonExistingClass
13+
location: class MyClass
14+
2 errors
15+
===
16+
template: "Java.SymbolNotFoundError"
17+
---
18+
# SymbolNotFoundError
19+
The error indicates that the compiler cannot find the class `NonExistingClass` when attempting to create an instance of it in the `MyClass` class.
20+
```
21+
// Attempting to create an instance of a non-existing class
22+
NonExistingClass obj = new NonExistingClass();
23+
^^^^^^^^^^^^^^^^
24+
}
25+
}
26+
```
27+
## Steps to fix
28+
### Create the missing class
29+
Create a new class named `NonExistingClass` to resolve the "cannot find symbol" error.
30+
```diff
31+
// MyClass.java
32+
- public class MyClass {
33+
+ class NonExistingClass {
34+
+ // Add any necessary code for NonExistingClass class
35+
+ }
36+
+
37+
+ public class MyClass {
38+
public static void main(String[] args) {
39+
// Attempting to create an instance of a non-existing class
40+
```

0 commit comments

Comments
 (0)