Skip to content

Commit

Permalink
feat: support for locating missing class in Java.SymbolNotFoundError
Browse files Browse the repository at this point in the history
  • Loading branch information
nedpals committed Nov 27, 2023
1 parent 3665fc0 commit 172e11c
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 18 deletions.
24 changes: 23 additions & 1 deletion error_templates/java/symbol_not_found_error.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,12 @@ var SymbolNotFoundError = lib.ErrorTemplate{
rootNode: m.Nearest,
}

query := fmt.Sprintf("((identifier) @symbol (#eq? @symbol \"%s\"))", symbolName)
nodeTypeToFind := "identifier"
if errorCtx.symbolType == "class" {
nodeTypeToFind = "type_identifier"
}

query := fmt.Sprintf("((%s) @symbol (#eq? @symbol \"%s\"))", nodeTypeToFind, symbolName)
lib.QueryNode(m.Nearest, strings.NewReader(query), func(ctx lib.QueryNodeCtx) bool {
match := ctx.Cursor.FilterPredicates(ctx.Match, []byte(m.Nearest.Doc.Contents))
for _, c := range match.Captures {
Expand Down Expand Up @@ -65,6 +70,8 @@ var SymbolNotFoundError = lib.ErrorTemplate{
gen.Add(`The program cannot find variable "%s"`, ctx.symbolName)
case "method":
gen.Add("The error indicates that the compiler cannot find the method `%s` in the `%s` class.", ctx.symbolName, ctx.locationClass)
case "class":
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)
}
},
OnGenBugFixFn: func(cd *lib.ContextData, gen *lib.BugFixGenerator) {
Expand Down Expand Up @@ -99,6 +106,21 @@ var SymbolNotFoundError = lib.ErrorTemplate{
EndPosition: lastMethodNode.EndPosition().Add(lib.Position{Column: 1}), // same thing here
})
})
case "class":
gen.Add("Create the missing class", func(s *lib.BugFixSuggestion) {
s.AddStep("Create a new class named `%s` to resolve the \"cannot find symbol\" error.", ctx.symbolName).
AddFix(lib.FixSuggestion{
NewText: fmt.Sprintf("class %s {\n\t// Add any necessary code for %s class\n}\n\n", ctx.symbolName, ctx.symbolName),
StartPosition: lib.Position{
Line: ctx.locationNode.StartPosition().Line,
Column: 0,
},
EndPosition: lib.Position{
Line: ctx.locationNode.StartPosition().Line,
Column: 0,
},
})
})
}
},
}
Expand Down
17 changes: 0 additions & 17 deletions error_templates/java/test_files/no_class_def_error/test.txt

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
name: "MissingClass"
template: "Java.SymbolNotFoundError"
---
MyClass.java:5: error: cannot find symbol
NonExistingClass obj = new NonExistingClass();
^
symbol: class NonExistingClass
location: class MyClass
MyClass.java:5: error: cannot find symbol
NonExistingClass obj = new NonExistingClass();
^
symbol: class NonExistingClass
location: class MyClass
2 errors
===
template: "Java.SymbolNotFoundError"
---
# SymbolNotFoundError
The error indicates that the compiler cannot find the class `NonExistingClass` when attempting to create an instance of it in the `MyClass` class.
```
// Attempting to create an instance of a non-existing class
NonExistingClass obj = new NonExistingClass();
^^^^^^^^^^^^^^^^
}
}
```
## Steps to fix
### Create the missing class
Create a new class named `NonExistingClass` to resolve the "cannot find symbol" error.
```diff
// MyClass.java
- public class MyClass {
+ class NonExistingClass {
+ // Add any necessary code for NonExistingClass class
+ }
+
+ public class MyClass {
public static void main(String[] args) {
// Attempting to create an instance of a non-existing class
```

0 comments on commit 172e11c

Please sign in to comment.