Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Support isJavaSource #30

Merged
merged 1 commit into from
May 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading