Skip to content

Commit

Permalink
Merge pull request #8 from shivasurya/shiva/java-method-declaration-a…
Browse files Browse the repository at this point in the history
…rgument-support

feat: Added method arguments type and argument value
  • Loading branch information
shivasurya authored Apr 24, 2024
2 parents f413784 + 159eea1 commit bd68e3e
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 29 deletions.
52 changes: 30 additions & 22 deletions sourcecode-parser/construct.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,17 @@ import (
)

type GraphNode struct {
ID string
Type string
Name string
CodeSnippet string
LineNumber uint32
OutgoingEdges []*GraphEdge
IsExternal bool
Modifier string
ReturnType string
MethodArguments []string
ID string
Type string
Name string
CodeSnippet string
LineNumber uint32
OutgoingEdges []*GraphEdge
IsExternal bool
Modifier string
ReturnType string
MethodArgumentsType []string
MethodArgumentsValue []string
}

type GraphEdge struct {
Expand Down Expand Up @@ -88,33 +89,40 @@ func buildGraphFromAST(node *sitter.Node, sourceCode []byte, graph *CodeGraph, c
methodName, methodId := extractMethodName(node, sourceCode, packageName, className)
invokedNode, exists := graph.Nodes[methodId]
modifiers := ""
ReturnType := ""
MethodArguments := []string{}
returnType := ""
methodArgumentType := []string{}
methodArgumentValue := []string{}

for i := 0; i < int(node.ChildCount()); i++ {
if node.Child(i).Type() == "modifiers" {
modifiers = node.Child(i).Content(sourceCode)
} else if node.Child(i).Type() == "void_type" || node.Child(i).Type() == "type_identifier" {
// get return type of method
ReturnType = node.Child(i).Content(sourceCode)
returnType = node.Child(i).Content(sourceCode)
} else if node.Child(i).Type() == "formal_parameters" {
// get method arguments
for j := 0; j < int(node.Child(i).NamedChildCount()); j++ {
param := node.Child(i).NamedChild(j)
MethodArguments = append(MethodArguments, param.Content(sourceCode))
// get type of argument and add to method arguments
paramType := param.Child(0).Content(sourceCode)
paramValue := param.Child(1).Content(sourceCode)
methodArgumentType = append(methodArgumentType, paramType)
methodArgumentValue = append(methodArgumentValue, paramValue)
}
}
}

if !exists || (exists && invokedNode.ID != methodId) {
invokedNode = &GraphNode{
ID: methodId, // In a real scenario, you would construct a unique ID, possibly using the method signature
Type: "method_declaration",
Name: methodName,
CodeSnippet: string(node.Content(sourceCode)),
LineNumber: node.StartPoint().Row + 1, // Lines start from 0 in the AST
Modifier: extractVisibilityModifier(modifiers),
ReturnType: ReturnType,
MethodArguments: MethodArguments,
ID: methodId, // In a real scenario, you would construct a unique ID, possibly using the method signature
Type: "method_declaration",
Name: methodName,
CodeSnippet: string(node.Content(sourceCode)),
LineNumber: node.StartPoint().Row + 1, // Lines start from 0 in the AST
Modifier: extractVisibilityModifier(modifiers),
ReturnType: returnType,
MethodArgumentsType: methodArgumentType,
MethodArgumentsValue: methodArgumentValue,
// CodeSnippet and LineNumber are skipped as per the requirement
}
}
Expand Down
4 changes: 2 additions & 2 deletions sourcecode-parser/queryparser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func (p *Parser) parseCondition() *Condition {
}

type EvalContext interface {
GetValue(key string) string // Retrieves a value based on a key, which helps in condition evaluation.
GetValue(key string, val string) string // Retrieves a value based on a key, which helps in condition evaluation.
}

type Expr interface {
Expand Down Expand Up @@ -167,7 +167,7 @@ func (b *BinaryExpr) Evaluate(ctx EvalContext) bool {
}

func (c *Condition) Evaluate(ctx EvalContext) bool {
fieldValue := ctx.GetValue(c.Field)
fieldValue := ctx.GetValue(c.Field, c.Value)
switch c.Operator {
case "=":
return fieldValue == c.Value
Expand Down
27 changes: 22 additions & 5 deletions sourcecode-parser/source_sink.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ type SourceSinkPath struct {
}

var MethodAttribute = map[string]int{
"name": 0,
"visibility": 1,
"returntype": 2,
"name": 0,
"visibility": 1,
"returntype": 2,
"argumentype": 3,
"argumentname": 4,
// Add more attributes as needed
}

Expand Down Expand Up @@ -82,15 +84,30 @@ type GraphNodeContext struct {
}

// GetValue returns the value of a field in a GraphNode based on the key.
func (gnc *GraphNodeContext) GetValue(key string) string {
func (gnc *GraphNodeContext) GetValue(key string, val string) string {
switch key {
case "visibility":
return gnc.Node.Modifier
case "returntype":
return gnc.Node.ReturnType
case "name":
return gnc.Node.Name
// add other cases as necessary for your application
case "argumentype":
// check value in MethodArgumentsType array
for i, arg := range gnc.Node.MethodArgumentsType {
if arg == val {
return gnc.Node.MethodArgumentsType[i]
}
}
return ""
case "argumentname":
// check value in MethodArgumentsValue array
for i, arg := range gnc.Node.MethodArgumentsValue {
if arg == val {
return gnc.Node.MethodArgumentsValue[i]
}
}
return ""
default:
fmt.Printf("Unsupported attribute key: %s\n", key)
return ""
Expand Down

0 comments on commit bd68e3e

Please sign in to comment.