Skip to content

Commit

Permalink
feat: support for Java.NumberFormatException
Browse files Browse the repository at this point in the history
  • Loading branch information
nedpals committed Jan 29, 2024
1 parent f10cf61 commit 494c34f
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 0 deletions.
54 changes: 54 additions & 0 deletions error_templates/java/number_format_exception.go
Original file line number Diff line number Diff line change
@@ -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<string>.+)\""),
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}),
})
}
}
})
},
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
public class Main {
public static void main(String[] args) {
String invalidNumber = "abc";
int result = Integer.parseInt(invalidNumber);
}
}
30 changes: 30 additions & 0 deletions error_templates/java/test_files/number_format_exception/test.txt
Original file line number Diff line number Diff line change
@@ -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);
}
```

0 comments on commit 494c34f

Please sign in to comment.