MCPcopy Create free account
hub / github.com/ChangeYourWay/Fenrir-CodeAuditTool / ParseFile

Method ParseFile

internal/utils/java_parser.go:20–67  ·  view source on GitHub ↗
(filePath string)

Source from the content-addressed store, hash-verified

18}
19
20func (p *JavaParser) ParseFile(filePath string) ([]UniversalASTNode, error) {
21 // 读取 Java 文件内容
22 code, err := os.ReadFile(filePath)
23 if err != nil {
24 return nil, err
25 }
26
27 // 使用 Tree-sitter 解析 Java 代码
28 parser := sitter.NewParser()
29 parser.SetLanguage(java.GetLanguage())
30 tree := parser.Parse(nil, code)
31
32 defer tree.Close()
33
34 root := tree.RootNode()
35 var nodes []UniversalASTNode
36
37 // 首先提取包名
38 packageName := p.extractPackageName(root, code)
39
40 // 收集 import 语句
41 importMap := map[string]string{}
42 importStar := []string{}
43 for i := 0; i < int(root.ChildCount()); i++ {
44 child := root.Child(i)
45 if child != nil && child.Type() == "import_declaration" {
46 importPath := ""
47 for j := 0; j < int(child.ChildCount()); j++ {
48 idNode := child.Child(j)
49 if idNode != nil && (idNode.Type() == "scoped_identifier" || idNode.Type() == "identifier") {
50 importPath = idNode.Content(code)
51 }
52 }
53 if strings.HasSuffix(importPath, ".*") {
54 importStar = append(importStar, strings.TrimSuffix(importPath, ".*"))
55 } else if importPath != "" {
56 parts := strings.Split(importPath, ".")
57 simpleName := parts[len(parts)-1]
58 importMap[simpleName] = importPath
59 }
60 }
61 }
62
63 // 遍历 AST 提取关键信息
64 p.traverseNode(root, filePath, code, packageName, importMap, importStar, &nodes)
65
66 return nodes, nil
67}
68
69// 提取包名 - 在文件级别提取,只执行一次
70func (p *JavaParser) extractPackageName(root *sitter.Node, code []byte) string {

Callers

nothing calls this directly

Calls 3

extractPackageNameMethod · 0.95
traverseNodeMethod · 0.95
ReadFileMethod · 0.80

Tested by

no test coverage detected