Skip to content

Commit

Permalink
Feature: Support isJavaSource (#30)
Browse files Browse the repository at this point in the history
is_java_source attribute helps to filter java source code
  • Loading branch information
shivasurya authored May 21, 2024
1 parent 51ffd93 commit 07cc519
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 20 deletions.
53 changes: 33 additions & 20 deletions sourcecode-parser/construct.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ type GraphNode struct {
VariableValue string
hasAccess bool
File string
isJavaSourceFile bool
}

type GraphEdge struct {
Expand Down Expand Up @@ -89,6 +90,10 @@ func extractVisibilityModifier(modifiers string) string {
return "" // return an empty string if no visibility modifier is found
}

func isJavaSourceFile(filename string) bool {
return filepath.Ext(filename) == ".java"
}

func hasAccess(node *sitter.Node, variableName string, sourceCode []byte) bool {
if node == nil {
return false
Expand All @@ -110,6 +115,7 @@ func hasAccess(node *sitter.Node, variableName string, sourceCode []byte) bool {
}

func buildGraphFromAST(node *sitter.Node, sourceCode []byte, graph *CodeGraph, currentContext *GraphNode, file string) {
isJavaSourceFile := isJavaSourceFile(file)
switch node.Type() {
case "method_declaration":
methodName, methodID := extractMethodName(node, sourceCode)
Expand Down Expand Up @@ -155,6 +161,8 @@ func buildGraphFromAST(node *sitter.Node, sourceCode []byte, graph *CodeGraph, c
ReturnType: returnType,
MethodArgumentsType: methodArgumentType,
MethodArgumentsValue: methodArgumentValue,
File: file,
isJavaSourceFile: isJavaSourceFile,
// CodeSnippet and LineNumber are skipped as per the requirement
}
}
Expand Down Expand Up @@ -188,6 +196,8 @@ func buildGraphFromAST(node *sitter.Node, sourceCode []byte, graph *CodeGraph, c
CodeSnippet: node.Content(sourceCode),
LineNumber: node.StartPoint().Row + 1, // Lines start from 0 in the AST
MethodArgumentsValue: arguments,
File: file,
isJavaSourceFile: isJavaSourceFile,
}
graph.AddNode(invokedNode)
}
Expand Down Expand Up @@ -224,15 +234,17 @@ func buildGraphFromAST(node *sitter.Node, sourceCode []byte, graph *CodeGraph, c
}
}
classNode := &GraphNode{
ID: generateMethodID(className, []string{}),
Type: "class_declaration",
Name: className,
CodeSnippet: node.Content(sourceCode),
LineNumber: node.StartPoint().Row + 1,
PackageName: packageName,
Modifier: extractVisibilityModifier(accessModifier),
SuperClass: superClass,
Interface: implementedInterface,
ID: generateMethodID(className, []string{}),
Type: "class_declaration",
Name: className,
CodeSnippet: node.Content(sourceCode),
LineNumber: node.StartPoint().Row + 1,
PackageName: packageName,
Modifier: extractVisibilityModifier(accessModifier),
SuperClass: superClass,
Interface: implementedInterface,
File: file,
isJavaSourceFile: isJavaSourceFile,
}
graph.AddNode(classNode)
case "local_variable_declaration", "field_declaration":
Expand Down Expand Up @@ -280,17 +292,18 @@ func buildGraphFromAST(node *sitter.Node, sourceCode []byte, graph *CodeGraph, c
}
// Create a new node for the variable
variableNode := &GraphNode{
ID: generateMethodID(variableName, []string{}),
Type: "variable_declaration",
Name: variableName,
CodeSnippet: node.Content(sourceCode),
LineNumber: node.StartPoint().Row + 1,
Modifier: extractVisibilityModifier(variableModifier),
DataType: variableType,
Scope: scope,
VariableValue: variableValue,
hasAccess: hasAccessValue,
File: file,
ID: generateMethodID(variableName, []string{}),
Type: "variable_declaration",
Name: variableName,
CodeSnippet: node.Content(sourceCode),
LineNumber: node.StartPoint().Row + 1,
Modifier: extractVisibilityModifier(variableModifier),
DataType: variableType,
Scope: scope,
VariableValue: variableValue,
hasAccess: hasAccessValue,
File: file,
isJavaSourceFile: isJavaSourceFile,
}
graph.AddNode(variableNode)
}
Expand Down
5 changes: 5 additions & 0 deletions sourcecode-parser/source_sink.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,11 @@ func (gnc *GraphNodeContext) GetValue(key, val string) string {
return "true"
}
return "false"
case "is_java_source":
if gnc.Node.isJavaSourceFile {
return "true"
}
return "false"
default:
fmt.Printf("Unsupported attribute key: %s\n", key)
return ""
Expand Down

0 comments on commit 07cc519

Please sign in to comment.