diff --git a/error_templates/java/symbol_not_found_error.go b/error_templates/java/symbol_not_found_error.go index 40d08e0..b936276 100644 --- a/error_templates/java/symbol_not_found_error.go +++ b/error_templates/java/symbol_not_found_error.go @@ -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 { @@ -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) { @@ -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, + }, + }) + }) } }, } diff --git a/error_templates/java/test_files/no_class_def_error/test.txt b/error_templates/java/test_files/no_class_def_error/test.txt deleted file mode 100644 index 278d8b1..0000000 --- a/error_templates/java/test_files/no_class_def_error/test.txt +++ /dev/null @@ -1,17 +0,0 @@ -template: "Java.NoClassDefError" ---- -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.NoClassDefError" ---- -a diff --git a/error_templates/java/test_files/no_class_def_error/MyClass.java b/error_templates/java/test_files/symbol_not_found_error_missing_class/MyClass.java similarity index 100% rename from error_templates/java/test_files/no_class_def_error/MyClass.java rename to error_templates/java/test_files/symbol_not_found_error_missing_class/MyClass.java diff --git a/error_templates/java/test_files/symbol_not_found_error_missing_class/test.txt b/error_templates/java/test_files/symbol_not_found_error_missing_class/test.txt new file mode 100644 index 0000000..34cba39 --- /dev/null +++ b/error_templates/java/test_files/symbol_not_found_error_missing_class/test.txt @@ -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 +```