Skip to content

Commit d1e1667

Browse files
committed
todo templates
1 parent abb119e commit d1e1667

File tree

7 files changed

+147
-72
lines changed

7 files changed

+147
-72
lines changed

error_templates/java/arithmetic_exception.go

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,43 @@ package java
22

33
import lib "github.com/nedpals/errgoengine"
44

5+
type arithExceptionKind int
6+
7+
const (
8+
unknown arithExceptionKind = 0
9+
dividedByZero arithExceptionKind = iota
10+
nonTerminatingDecimal arithExceptionKind = iota
11+
)
12+
13+
type arithExceptionCtx struct {
14+
kind arithExceptionKind
15+
}
16+
517
var ArithmeticException = lib.ErrorTemplate{
618
Name: "ArithmeticException",
719
Pattern: runtimeErrorPattern("java.lang.ArithmeticException", "(?P<reason>.+)"),
8-
OnGenExplainFn: func(cd *lib.ContextData, gen *lib.ExplainGenerator) {
20+
OnAnalyzeErrorFn: func(cd *lib.ContextData, err *lib.MainError) {
21+
ctx := arithExceptionCtx{}
922
reason := cd.Variables["reason"]
1023
switch reason {
1124
case "/ by zero":
12-
gen.Add("One of your variables initialized a double value by dividing a number to zero")
25+
ctx.kind = dividedByZero
1326
case "Non-terminating decimal expansion; no exact representable decimal result.":
14-
gen.Add("TODO")
27+
ctx.kind = nonTerminatingDecimal
1528
default:
29+
ctx.kind = unknown
30+
}
31+
err.Context = ctx
32+
},
33+
OnGenExplainFn: func(cd *lib.ContextData, gen *lib.ExplainGenerator) {
34+
ctx := cd.MainError.Context.(arithExceptionCtx)
35+
switch ctx.kind {
36+
case dividedByZero:
37+
ctx.kind = dividedByZero
38+
gen.Add("One of your variables initialized a double value by dividing a number to zero")
39+
case nonTerminatingDecimal:
40+
gen.Add("TODO")
41+
case unknown:
1642
gen.Add("Unknown ArithmeticException")
1743
}
1844
},

error_templates/java/null_pointer_exception.go

Lines changed: 50 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -5,59 +5,30 @@ import (
55
"github.com/nedpals/errgoengine/languages/java"
66
)
77

8-
// const ()
8+
type exceptionLocationKind int
9+
10+
const (
11+
fromUnknown exceptionLocationKind = 0
12+
fromFunctionArgument exceptionLocationKind = iota
13+
fromSystemOut exceptionLocationKind = iota
14+
fromArrayAccess exceptionLocationKind = iota
15+
fromExpression exceptionLocationKind = iota
16+
fromMethodInvocation exceptionLocationKind = iota
17+
)
918

10-
// type nullPointerExceptionCtx struct {
11-
// isSystemOut bool
12-
// }
19+
type nullPointerExceptionCtx struct {
20+
kind exceptionLocationKind
21+
// symbolInvolved lib.SyntaxNode
22+
methodName string
23+
origin string
24+
}
1325

1426
// TODO: unit testing
1527
var NullPointerException = lib.ErrorTemplate{
1628
Name: "NullPointerException",
1729
Pattern: runtimeErrorPattern("java.lang.NullPointerException", ""),
18-
// OnAnalyzeErrorFn: func(cd *lib.ContextData, m *lib.MainError) {
19-
// ctx := nullPointerExceptionCtx{}
20-
21-
// // if the offending line is an offending method call, get the argument that triggered the null error
22-
// if cd.MainError.Nearest.Type() == "expression_statement" {
23-
// exprNode := cd.MainError.Nearest.NamedChild(0)
24-
25-
// // NOTE: i hope we will be able to parse the entire
26-
// // java system library without hardcoding the definitions lol
27-
// if exprNode.Type() == "method_invocation" {
28-
// objNode := exprNode.ChildByFieldName("object")
29-
// // check if this is just simple printing
30-
// if objNode.Text() == "System.out" {
31-
// isSystemOut = true
32-
// } else if retType := cd.Analyzer.AnalyzeNode(exprNode); retType == java.BuiltinTypes.NullSymbol {
33-
// cd.MainError.Nearest = exprNode
34-
// }
35-
36-
// if objNode.Type() == "array_access" {
37-
// // inArray = true
38-
// cd.MainError.Nearest = exprNode
39-
// } else {
40-
// arguments := exprNode.ChildByFieldName("arguments")
41-
// for i := 0; i < int(arguments.NamedChildCount()); i++ {
42-
// argNode := arguments.NamedChild(i)
43-
// retType := cd.Analyzer.AnalyzeNode(argNode)
44-
45-
// if retType == java.BuiltinTypes.NullSymbol || argNode.Type() == "array_access" {
46-
// cd.MainError.Nearest = argNode
47-
// break
48-
// }
49-
// }
50-
// }
51-
// }
52-
// }
53-
// },
54-
OnGenExplainFn: func(cd *lib.ContextData, gen *lib.ExplainGenerator) {
55-
// TODO: create a function that will find the node with a null return type
56-
// sb := &strings.Builder{}
57-
isSystemOut := false
58-
// inArray := false
59-
methodName := ""
60-
origin := ""
30+
OnAnalyzeErrorFn: func(cd *lib.ContextData, err *lib.MainError) {
31+
ctx := nullPointerExceptionCtx{}
6132

6233
// if the offending line is an offending method call, get the argument that triggered the null error
6334
if cd.MainError.Nearest.Type() == "expression_statement" {
@@ -69,14 +40,16 @@ var NullPointerException = lib.ErrorTemplate{
6940
objNode := exprNode.ChildByFieldName("object")
7041
// check if this is just simple printing
7142
if objNode.Text() == "System.out" {
72-
isSystemOut = true
43+
ctx.kind = fromSystemOut
7344
} else if retType := cd.Analyzer.AnalyzeNode(exprNode); retType == java.BuiltinTypes.NullSymbol {
7445
cd.MainError.Nearest = exprNode
46+
ctx.kind = fromMethodInvocation
7547
}
7648

7749
if objNode.Type() == "array_access" {
7850
// inArray = true
7951
cd.MainError.Nearest = exprNode
52+
ctx.kind = fromArrayAccess
8053
} else {
8154
arguments := exprNode.ChildByFieldName("arguments")
8255
for i := 0; i < int(arguments.NamedChildCount()); i++ {
@@ -85,42 +58,53 @@ var NullPointerException = lib.ErrorTemplate{
8558

8659
if retType == java.BuiltinTypes.NullSymbol || argNode.Type() == "array_access" {
8760
cd.MainError.Nearest = argNode
61+
ctx.kind = fromFunctionArgument
8862
break
8963
}
9064
}
9165
}
66+
} else if exprNode.Type() == "assignment_expression" {
67+
// right := exprNode.ChildByFieldName("right")
68+
//
69+
}
70+
71+
// identify the *MAIN* culprit
72+
mainNode := cd.MainError.Nearest
73+
switch mainNode.Type() {
74+
case "method_invocation":
75+
nameNode := mainNode.ChildByFieldName("name")
76+
ctx.methodName = nameNode.Text()
77+
ctx.origin = mainNode.ChildByFieldName("object").Text()
78+
default:
79+
ctx.origin = mainNode.Text()
9280
}
93-
}
9481

95-
// identify the *MAIN* culprit
96-
mainNode := cd.MainError.Nearest
97-
switch mainNode.Type() {
98-
case "method_invocation":
99-
nameNode := mainNode.ChildByFieldName("name")
100-
methodName = nameNode.Text()
101-
origin = mainNode.ChildByFieldName("object").Text()
102-
default:
103-
origin = mainNode.Text()
82+
err.Context = ctx
10483
}
84+
},
85+
OnGenExplainFn: func(cd *lib.ContextData, gen *lib.ExplainGenerator) {
86+
// TODO: create a function that will find the node with a null return type
87+
ctx := cd.MainError.Context.(nullPointerExceptionCtx)
10588

106-
if isSystemOut {
107-
if len(methodName) != 0 {
108-
gen.Add("Your program tried to print the value of \"%s\" method from \"%s\" which is a null.", methodName, origin)
109-
} else {
110-
gen.Add("Your program tried to print the value of \"%s\" which is a null", origin)
89+
if ctx.kind == fromSystemOut {
90+
gen.Add("Your program tried to print the value of ")
91+
if len(ctx.methodName) != 0 {
92+
gen.Add("\"%s\" method from ", ctx.methodName)
11193
}
112-
} else if len(methodName) != 0 {
94+
gen.Add("\"%s\" which is a null.", ctx.origin)
95+
return
96+
} else if len(ctx.methodName) != 0 {
11397
// if inArray {
11498
// gen.Add("Your program tried to execute the \"%s\" method from \"%s\" which is a null.", )
11599
// } else {
116-
gen.Add("Your program tried to execute the \"%s\" method from \"%s\" which is a null.", methodName, origin)
100+
gen.Add("Your program tried to execute the \"%s\" method from \"%s\" which is a null.", ctx.methodName, ctx.origin)
117101
// }
102+
return
118103
}
119104

120105
gen.Add("Your program try to access or manipulate an object reference that is currently pointing to `null`, meaning it doesn't refer to any actual object in memory. This typically happens when you forget to initialize an object before using it, or when you try to access an object that hasn't been properly assigned a value. ")
121106
},
122107
OnGenBugFixFn: func(cd *lib.ContextData, gen *lib.BugFixGenerator) {
123-
// TODO:
124108

125109
},
126110
}

error_templates/java/test_files/non_static_method_access_error/test.txt

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,16 @@ Main.java:9: error: non-static method printMessage() cannot be referenced from a
77
===
88
template: "Java.NonStaticMethodAccessError"
99
---
10-
aa
10+
# NonStaticMethodAccessError
11+
In the `Main` class, you have invoked the `printMessage` method inside the `main` static method.
12+
13+
```
14+
// Attempt to call the non-static method without creating an object
15+
printMessage(); // This will result in an error
16+
^^^^^^^^^^^^
17+
}
18+
}
19+
20+
```
21+
## Steps to fix
22+
1. Fix 1

error_templates/java/test_files/null_pointer_exception/test.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,7 @@ Exception in thread "main" java.lang.NullPointerException
55
===
66
template: "Java.NullPointerException"
77
---
8+
# NullPointerException
89
Your program tried to print the value of "toUpperCase" method from "test" which is a null.
10+
## Steps to fix
11+
1. Go to blabla

error_templates/java/test_files/unknown_variable_error/test.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,15 @@ Program.java:3: error: cannot find symbol
99
===
1010
template: "Java.UnknownVariableError"
1111
---
12+
# UnknownVariableError
1213
The program cannot find variable "a"
14+
```
15+
public static void main(String[] args) {
16+
System.out.println(a);
17+
^
18+
}
19+
}
20+
21+
```
22+
## Steps to fix
23+
Nothing to fix

error_templates/java/test_files/unreachable_statement_error/test.txt

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,16 @@ Unreachable.java:5: error: unreachable statement
77
===
88
template: "Java.UnreachableStatementError"
99
---
10-
You have code below after you returned a value
10+
# UnreachableStatementError
11+
Portions of the code weren't accessible because you returned a value on top of it.
12+
13+
```
14+
return;
15+
System.out.println("c");
16+
^^^^^^^^^^^^^^^^^^^^^^^
17+
}
18+
}
19+
20+
```
21+
## Steps to fix
22+
Nothing to fix
Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,44 @@
11
package java
22

33
import (
4+
"fmt"
5+
"strings"
6+
47
lib "github.com/nedpals/errgoengine"
58
)
69

10+
type unknownVarErrorCtx struct {
11+
rootNode lib.SyntaxNode
12+
parentNode lib.SyntaxNode
13+
}
14+
715
var UnknownVariableError = lib.ErrorTemplate{
816
Name: "UnknownVariableError",
917
Pattern: comptimeErrorPattern("cannot find symbol", `symbol:\s+variable (?P<variable>\S+)`),
1018
StackTracePattern: comptimeStackTracePattern,
19+
OnAnalyzeErrorFn: func(cd *lib.ContextData, m *lib.MainError) {
20+
variable := cd.Variables["variable"]
21+
query := fmt.Sprintf("((identifier) @symbol (#eq? @symbol \"%s\"))", variable)
22+
23+
lib.QueryNode(m.Nearest, strings.NewReader(query), func(ctx lib.QueryNodeCtx) bool {
24+
match := ctx.Cursor.FilterPredicates(ctx.Match, []byte(m.Nearest.Doc.Contents))
25+
for _, c := range match.Captures {
26+
node := lib.WrapNode(m.Nearest.Doc, c.Node)
27+
m.Context = unknownVarErrorCtx{m.Nearest, node.Parent()}
28+
m.Nearest = node
29+
return false
30+
}
31+
return true
32+
})
33+
},
1134
OnGenExplainFn: func(cd *lib.ContextData, gen *lib.ExplainGenerator) {
1235
gen.Add(`The program cannot find variable "%s"`, cd.Variables["variable"])
1336
},
1437
OnGenBugFixFn: func(cd *lib.ContextData, gen *lib.BugFixGenerator) {
15-
// TODO:
38+
ctx := cd.MainError.Context.(unknownVarErrorCtx)
39+
variable := cd.Variables["variable"]
40+
gen.AddStep("(%s) Create a variable named \"%s\". For example: ", ctx.parentNode.Type(), variable).
41+
// TODO: use variable type from the inferred parameter
42+
AddFix(fmt.Sprintf("String %s = \"\";", variable), ctx.rootNode.StartPosition(), false)
1643
},
1744
}

0 commit comments

Comments
 (0)