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

Method traverseNode

internal/utils/java_parser.go:103–291  ·  view source on GitHub ↗
(node *sitter.Node, filePath string, code []byte, packageName string, importMap map[string]string, importStar []string, nodes *[]UniversalASTNode)

Source from the content-addressed store, hash-verified

101}
102
103func (p *JavaParser) traverseNode(node *sitter.Node, filePath string, code []byte, packageName string, importMap map[string]string, importStar []string, nodes *[]UniversalASTNode) {
104 // 处理不同类型的节点
105 switch node.Type() {
106 case "class_declaration", "interface_declaration", "annotation_type_declaration":
107 nameNode := node.ChildByFieldName("name")
108 if nameNode != nil {
109 className := nameNode.Content(code)
110 id := fmt.Sprintf("%s:%s:%d", filePath, className, node.StartByte())
111
112 // 调试:打印 class_declaration 的所有子节点
113 fmt.Printf("=== 分析类: %s ===\n", className)
114 fmt.Printf("类节点类型: %s, 子节点数: %d\n", node.Type(), node.ChildCount())
115 for i := 0; i < int(node.ChildCount()); i++ {
116 child := node.Child(i)
117 if child != nil {
118 fmt.Printf(" 子节点 %d: type=%s, content=%s\n",
119 i, child.Type(), child.Content(code))
120 }
121 }
122
123 // 创建类节点
124 classNode := UniversalASTNode{
125 ID: id,
126 Language: "java",
127 Type: "Class",
128 Name: className,
129 File: filePath,
130 Package: packageName,
131 StartLine: int(node.StartPoint().Row),
132 EndLine: int(node.EndPoint().Row),
133 Fields: make([]FieldInfo, 0),
134 Metadata: map[string]string{},
135 }
136
137 // 获取类体节点
138 bodyNode := node.ChildByFieldName("body")
139 if bodyNode != nil {
140 // 收集类中的字段信息
141 p.collectClassFields(bodyNode, code, &classNode)
142 }
143
144 // 获取所有父类和接口
145 var superClassNames []string
146 // 1. 直接用 ChildByFieldName 拿 extends 对应的那棵子树
147 if extendsNode := node.ChildByFieldName("superclass"); extendsNode != nil {
148 // 通常 extendsNode 下的命名子节点才是真正的类型标识,如 Foo 或 com.Bar
149 for i := 0; i < int(extendsNode.NamedChildCount()); i++ {
150 typeNode := extendsNode.NamedChild(i)
151 superClassNames = append(superClassNames, p.extractTypeName(typeNode, code))
152 }
153 }
154
155 // 2. 拿 implements 对应的接口列表,注意 field name 要写对
156 if impls := node.ChildByFieldName("interfaces"); impls != nil {
157 for i := 0; i < int(impls.NamedChildCount()); i++ {
158 typeNode := impls.NamedChild(i)
159 superClassNames = append(superClassNames, p.extractTypeName(typeNode, code))
160 }

Callers 1

ParseFileMethod · 0.95

Calls 2

collectClassFieldsMethod · 0.95
extractTypeNameMethod · 0.95

Tested by

no test coverage detected