diff --git a/error_templates/java/number_format_exception.go b/error_templates/java/number_format_exception.go new file mode 100644 index 0000000..71154b6 --- /dev/null +++ b/error_templates/java/number_format_exception.go @@ -0,0 +1,54 @@ +package java + +import ( + "context" + + lib "github.com/nedpals/errgoengine" +) + +var NumberFormatException = lib.ErrorTemplate{ + Name: "NumberFormatException", + Pattern: runtimeErrorPattern("java.lang.NumberFormatException", "For input string: \"(?P.+)\""), + OnAnalyzeErrorFn: func(cd *lib.ContextData, m *lib.MainError) { + for q := m.Document.RootNode().Query(`(method_invocation name: (identifier) @name arguments: (argument_list (_) @arg) (#eq? @name "parseInt") (#any-eq? @arg "%s"))`, cd.Variables["string"]); q.Next(); { + if q.CurrentTagName() != "arg" { + continue + } + node := q.CurrentNode() + m.Nearest = node + break + } + }, + OnGenExplainFn: func(cd *lib.ContextData, gen *lib.ExplainGenerator) { + gen.Add("This error occurs when there is an attempt to convert a string to a numeric type, but the string does not represent a valid number.") + }, + OnGenBugFixFn: func(cd *lib.ContextData, gen *lib.BugFixGenerator) { + gen.Add("Ensure valid input for parsing", func(s *lib.BugFixSuggestion) { + if cd.MainError.Nearest.Type() == "identifier" { + variableSym := cd.Analyzer.AnalyzeNode(context.TODO(), cd.MainError.Nearest) + + if variableSym != nil { + varNode := cd.MainError.Document.RootNode().NamedDescendantForPointRange( + variableSym.Location(), + ) + + if varNode.Type() != "variable_declarator" { + return + } + + valueNode := varNode.ChildByFieldName("value") + if valueNode.Type() != "string_literal" { + return + } + + s.AddStep("Make sure the string contains a valid numeric representation before attempting to parse it."). + AddFix(lib.FixSuggestion{ + NewText: "123", + StartPosition: varNode.ChildByFieldName("value").StartPosition().Add(lib.Position{Column: 1}), + EndPosition: varNode.ChildByFieldName("value").EndPosition().Add(lib.Position{Column: -1}), + }) + } + } + }) + }, +} diff --git a/error_templates/java/test_files/number_format_exception/Main.java b/error_templates/java/test_files/number_format_exception/Main.java new file mode 100644 index 0000000..4d9e547 --- /dev/null +++ b/error_templates/java/test_files/number_format_exception/Main.java @@ -0,0 +1,6 @@ +public class Main { + public static void main(String[] args) { + String invalidNumber = "abc"; + int result = Integer.parseInt(invalidNumber); + } +} diff --git a/error_templates/java/test_files/number_format_exception/test.txt b/error_templates/java/test_files/number_format_exception/test.txt new file mode 100644 index 0000000..9ab858e --- /dev/null +++ b/error_templates/java/test_files/number_format_exception/test.txt @@ -0,0 +1,30 @@ +template: "Java.NumberFormatException" +--- +Exception in thread "main" java.lang.NumberFormatException: For input string: "abc" + at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:67) + at java.base/java.lang.Integer.parseInt(Integer.java:661) + at java.base/java.lang.Integer.parseInt(Integer.java:777) + at Main.main(Main.java:5) +=== +template: "Java.NumberFormatException" +--- +# NumberFormatException +This error occurs when there is an attempt to convert a string to a numeric type, but the string does not represent a valid number. +``` + String invalidNumber = "abc"; + int result = Integer.parseInt(invalidNumber); + ^^^^^^^^^^^^^ + } +} +``` +## Steps to fix +### Ensure valid input for parsing +Make sure the string contains a valid numeric representation before attempting to parse it. +```diff +public class Main { + public static void main(String[] args) { +- String invalidNumber = "abc"; ++ String invalidNumber = "123"; + int result = Integer.parseInt(invalidNumber); + } +```