Skip to content

Commit

Permalink
feat: support for nonstaticmethodaccesserror.go
Browse files Browse the repository at this point in the history
  • Loading branch information
nedpals committed Nov 25, 2023
1 parent a7dd951 commit 005a241
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 13 deletions.
62 changes: 59 additions & 3 deletions error_templates/java/non_static_method_access_error.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,71 @@
package java

import lib "github.com/nedpals/errgoengine"
import (
"fmt"
"strings"

lib "github.com/nedpals/errgoengine"
)

type nonStaticMethodAccessErrorCtx struct {
class string
method string
parent lib.SyntaxNode
}

var NonStaticMethodAccessError = lib.ErrorTemplate{
Name: "NonStaticMethodAccessError",
Pattern: comptimeErrorPattern(`non-static method (?P<method>\S+)\(\) cannot be referenced from a static context`),
StackTracePattern: comptimeStackTracePattern,
OnAnalyzeErrorFn: func(cd *lib.ContextData, m *lib.MainError) {
nCtx := nonStaticMethodAccessErrorCtx{parent: m.Nearest}

// Get the class name
symbols := cd.Symbols[cd.MainDocumentPath()]
for _, sym := range symbols.Symbols {
if sym.Kind() == lib.SymbolKindClass && m.Nearest.Location().IsWithin(sym.Location()) {
nCtx.class = sym.Name()
break
}
}

m.Context = nCtx

lib.QueryNode(m.Nearest, strings.NewReader("(method_invocation name: (identifier) @method arguments: (argument_list))"), func(ctx lib.QueryNodeCtx) bool {
match := ctx.Cursor.FilterPredicates(ctx.Match, []byte(m.Nearest.Doc.Contents))
for _, c := range match.Captures {
node := lib.WrapNode(m.Nearest.Doc, c.Node)
m.Nearest = node
nCtx.method = node.Text()
return false
}
return true
})
},
OnGenExplainFn: func(cd *lib.ContextData, gen *lib.ExplainGenerator) {
// return "TODO:"
gen.Add("This error occurs when trying to access a non-static method from a static context. In Java, a non-static method belongs to an instance of the class and needs an object to be called upon.")
},
OnGenBugFixFn: func(cd *lib.ContextData, gen *lib.BugFixGenerator) {
// TODO
ctx := cd.MainError.Context.(nonStaticMethodAccessErrorCtx)

gen.Add("Instantiate and call the method", func(s *lib.BugFixSuggestion) {
s.AddStep("Create an instance of the class to access the non-static method").
AddFix(lib.FixSuggestion{
NewText: fmt.Sprintf("%s obj = new %s();\n", ctx.class, ctx.class),
StartPosition: lib.Position{
Line: ctx.parent.StartPosition().Line,
Column: ctx.parent.StartPosition().Column,
},
EndPosition: lib.Position{
Line: ctx.parent.EndPosition().Line,
Column: 0,
},
}).
AddFix(lib.FixSuggestion{
NewText: "obj.",
StartPosition: ctx.parent.StartPosition(),
EndPosition: ctx.parent.StartPosition(),
})
})
},
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,22 @@ template: "Java.NonStaticMethodAccessError"
---
# NonStaticMethodAccessError
This error occurs when trying to access a non-static method from a static context. In Java, a non-static method belongs to an instance of the class and needs an object to be called upon.

```
// Attempt to call the non-static method without creating an object
printMessage(); // This will result in an error
^^^^^^^^^^^^
}
}

```
## Steps to fix
1. **Create an instance of the class to access the non-static method**
```java
Main obj = new Main();
obj.printMessage(); // This will properly call the non-static method
### Instantiate and call the method
Create an instance of the class to access the non-static method
```diff
public static void main(String[] args) {
// Attempt to call the non-static method without creating an object
- printMessage(); // This will result in an error
+ Main obj = new Main();
+ obj.printMessage(); // This will result in an error
}
}
```

The issue arises because the `printMessage()` method is non-static, meaning it belongs to an instance of the class. To use this method within the `main` method, you must create an object of the `Main` class and then call the `printMessage()` method using that object.

This correction instantiates an object (`obj`) of the `Main` class and then calls the `printMessage()` method using this object, resolving the error.

0 comments on commit 005a241

Please sign in to comment.