-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support for Java.NumberFormatException
- Loading branch information
Showing
3 changed files
with
90 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}), | ||
}) | ||
} | ||
} | ||
}) | ||
}, | ||
} |
6 changes: 6 additions & 0 deletions
6
error_templates/java/test_files/number_format_exception/Main.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
30
error_templates/java/test_files/number_format_exception/test.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
``` |