Navegador

navegador / navegador / ingestion / java.py
Source Blame History 298 lines
e52aae0… lmata 1 """
e52aae0… lmata 2 Java AST parser — extracts classes, interfaces, methods, constructors,
e52aae0… lmata 3 imports, inheritance, and call edges from .java files using tree-sitter.
e52aae0… lmata 4 """
e52aae0… lmata 5
e52aae0… lmata 6 import logging
e52aae0… lmata 7 from pathlib import Path
e52aae0… lmata 8
e52aae0… lmata 9 from navegador.graph.schema import EdgeType, NodeLabel
e52aae0… lmata 10 from navegador.graph.store import GraphStore
e52aae0… lmata 11 from navegador.ingestion.parser import LanguageParser
e52aae0… lmata 12
e52aae0… lmata 13 logger = logging.getLogger(__name__)
e52aae0… lmata 14
e52aae0… lmata 15
e52aae0… lmata 16 def _get_java_language():
e52aae0… lmata 17 try:
e52aae0… lmata 18 import tree_sitter_java as tsjava # type: ignore[import]
e52aae0… lmata 19 from tree_sitter import Language
758c6b7… lmata 20
e52aae0… lmata 21 return Language(tsjava.language())
e52aae0… lmata 22 except ImportError as e:
e52aae0… lmata 23 raise ImportError("Install tree-sitter-java: pip install tree-sitter-java") from e
e52aae0… lmata 24
e52aae0… lmata 25
e52aae0… lmata 26 def _node_text(node, source: bytes) -> str:
758c6b7… lmata 27 return source[node.start_byte : node.end_byte].decode("utf-8", errors="replace")
e52aae0… lmata 28
e52aae0… lmata 29
e52aae0… lmata 30 def _javadoc(node, source: bytes) -> str:
e52aae0… lmata 31 """Return the Javadoc (/** ... */) comment preceding a node, if any."""
e52aae0… lmata 32 parent = node.parent
e52aae0… lmata 33 if not parent:
e52aae0… lmata 34 return ""
e52aae0… lmata 35 siblings = list(parent.children)
e52aae0… lmata 36 idx = next((i for i, c in enumerate(siblings) if c.id == node.id), -1)
e52aae0… lmata 37 if idx <= 0:
e52aae0… lmata 38 return ""
e52aae0… lmata 39 prev = siblings[idx - 1]
e52aae0… lmata 40 if prev.type == "block_comment":
e52aae0… lmata 41 raw = _node_text(prev, source).strip()
e52aae0… lmata 42 if raw.startswith("/**"):
e52aae0… lmata 43 return raw.strip("/**").strip("*/").strip()
e52aae0… lmata 44 return ""
e52aae0… lmata 45
e52aae0… lmata 46
e52aae0… lmata 47 class JavaParser(LanguageParser):
e52aae0… lmata 48 """Parses Java source files into the navegador graph."""
e52aae0… lmata 49
e52aae0… lmata 50 def __init__(self) -> None:
e52aae0… lmata 51 from tree_sitter import Parser # type: ignore[import]
758c6b7… lmata 52
e52aae0… lmata 53 self._parser = Parser(_get_java_language())
e52aae0… lmata 54
e52aae0… lmata 55 def parse_file(self, path: Path, repo_root: Path, store: GraphStore) -> dict[str, int]:
e52aae0… lmata 56 source = path.read_bytes()
e52aae0… lmata 57 tree = self._parser.parse(source)
e52aae0… lmata 58 rel_path = str(path.relative_to(repo_root))
e52aae0… lmata 59
758c6b7… lmata 60 store.create_node(
758c6b7… lmata 61 NodeLabel.File,
758c6b7… lmata 62 {
758c6b7… lmata 63 "name": path.name,
758c6b7… lmata 64 "path": rel_path,
758c6b7… lmata 65 "language": "java",
758c6b7… lmata 66 "line_count": source.count(b"\n"),
758c6b7… lmata 67 },
758c6b7… lmata 68 )
e52aae0… lmata 69
e52aae0… lmata 70 stats = {"functions": 0, "classes": 0, "edges": 0}
e52aae0… lmata 71 self._walk(tree.root_node, source, rel_path, store, stats, class_name=None)
e52aae0… lmata 72 return stats
e52aae0… lmata 73
e52aae0… lmata 74 # ── AST walker ────────────────────────────────────────────────────────────
e52aae0… lmata 75
758c6b7… lmata 76 def _walk(
758c6b7… lmata 77 self,
758c6b7… lmata 78 node,
758c6b7… lmata 79 source: bytes,
758c6b7… lmata 80 file_path: str,
758c6b7… lmata 81 store: GraphStore,
758c6b7… lmata 82 stats: dict,
758c6b7… lmata 83 class_name: str | None,
758c6b7… lmata 84 ) -> None:
e52aae0… lmata 85 if node.type in ("class_declaration", "record_declaration"):
e52aae0… lmata 86 self._handle_class(node, source, file_path, store, stats)
e52aae0… lmata 87 return
e52aae0… lmata 88 if node.type == "interface_declaration":
e52aae0… lmata 89 self._handle_interface(node, source, file_path, store, stats)
e52aae0… lmata 90 return
e52aae0… lmata 91 if node.type == "import_declaration":
e52aae0… lmata 92 self._handle_import(node, source, file_path, store, stats)
e52aae0… lmata 93 return
e52aae0… lmata 94 for child in node.children:
e52aae0… lmata 95 self._walk(child, source, file_path, store, stats, class_name)
e52aae0… lmata 96
e52aae0… lmata 97 # ── Handlers ──────────────────────────────────────────────────────────────
e52aae0… lmata 98
758c6b7… lmata 99 def _handle_class(
758c6b7… lmata 100 self, node, source: bytes, file_path: str, store: GraphStore, stats: dict
758c6b7… lmata 101 ) -> None:
e52aae0… lmata 102 name_node = node.child_by_field_name("name")
e52aae0… lmata 103 if not name_node:
e52aae0… lmata 104 return
e52aae0… lmata 105 name = _node_text(name_node, source)
e52aae0… lmata 106 docstring = _javadoc(node, source)
e52aae0… lmata 107
758c6b7… lmata 108 store.create_node(
758c6b7… lmata 109 NodeLabel.Class,
758c6b7… lmata 110 {
758c6b7… lmata 111 "name": name,
758c6b7… lmata 112 "file_path": file_path,
758c6b7… lmata 113 "line_start": node.start_point[0] + 1,
758c6b7… lmata 114 "line_end": node.end_point[0] + 1,
758c6b7… lmata 115 "docstring": docstring,
758c6b7… lmata 116 },
758c6b7… lmata 117 )
e52aae0… lmata 118 store.create_edge(
758c6b7… lmata 119 NodeLabel.File,
758c6b7… lmata 120 {"path": file_path},
e52aae0… lmata 121 EdgeType.CONTAINS,
758c6b7… lmata 122 NodeLabel.Class,
758c6b7… lmata 123 {"name": name, "file_path": file_path},
e52aae0… lmata 124 )
e52aae0… lmata 125 stats["classes"] += 1
e52aae0… lmata 126 stats["edges"] += 1
e52aae0… lmata 127
e52aae0… lmata 128 # Superclass → INHERITS edge
e52aae0… lmata 129 superclass = node.child_by_field_name("superclass")
e52aae0… lmata 130 if superclass:
e52aae0… lmata 131 for child in superclass.children:
e52aae0… lmata 132 if child.type == "type_identifier":
e52aae0… lmata 133 parent_name = _node_text(child, source)
e52aae0… lmata 134 store.create_edge(
758c6b7… lmata 135 NodeLabel.Class,
758c6b7… lmata 136 {"name": name, "file_path": file_path},
e52aae0… lmata 137 EdgeType.INHERITS,
758c6b7… lmata 138 NodeLabel.Class,
758c6b7… lmata 139 {"name": parent_name, "file_path": file_path},
e52aae0… lmata 140 )
e52aae0… lmata 141 stats["edges"] += 1
e52aae0… lmata 142 break
e52aae0… lmata 143
e52aae0… lmata 144 # Walk class body for methods and constructors
e52aae0… lmata 145 body = node.child_by_field_name("body")
e52aae0… lmata 146 if body:
e52aae0… lmata 147 for child in body.children:
e52aae0… lmata 148 if child.type in ("method_declaration", "constructor_declaration"):
758c6b7… lmata 149 self._handle_method(child, source, file_path, store, stats, class_name=name)
758c6b7… lmata 150 elif child.type in (
758c6b7… lmata 151 "class_declaration",
758c6b7… lmata 152 "record_declaration",
758c6b7… lmata 153 "interface_declaration",
758c6b7… lmata 154 ):
e52aae0… lmata 155 # Nested class — register but don't recurse into methods
e52aae0… lmata 156 inner_name_node = child.child_by_field_name("name")
e52aae0… lmata 157 if inner_name_node:
e52aae0… lmata 158 inner_name = _node_text(inner_name_node, source)
758c6b7… lmata 159 store.create_node(
758c6b7… lmata 160 NodeLabel.Class,
758c6b7… lmata 161 {
758c6b7… lmata 162 "name": inner_name,
758c6b7… lmata 163 "file_path": file_path,
758c6b7… lmata 164 "line_start": child.start_point[0] + 1,
758c6b7… lmata 165 "line_end": child.end_point[0] + 1,
758c6b7… lmata 166 "docstring": "",
758c6b7… lmata 167 },
758c6b7… lmata 168 )
e52aae0… lmata 169 store.create_edge(
758c6b7… lmata 170 NodeLabel.Class,
758c6b7… lmata 171 {"name": name, "file_path": file_path},
e52aae0… lmata 172 EdgeType.CONTAINS,
758c6b7… lmata 173 NodeLabel.Class,
758c6b7… lmata 174 {"name": inner_name, "file_path": file_path},
e52aae0… lmata 175 )
e52aae0… lmata 176 stats["classes"] += 1
e52aae0… lmata 177 stats["edges"] += 1
e52aae0… lmata 178
758c6b7… lmata 179 def _handle_interface(
758c6b7… lmata 180 self, node, source: bytes, file_path: str, store: GraphStore, stats: dict
758c6b7… lmata 181 ) -> None:
e52aae0… lmata 182 name_node = node.child_by_field_name("name")
e52aae0… lmata 183 if not name_node:
e52aae0… lmata 184 return
e52aae0… lmata 185 name = _node_text(name_node, source)
e52aae0… lmata 186 docstring = _javadoc(node, source)
e52aae0… lmata 187
758c6b7… lmata 188 store.create_node(
758c6b7… lmata 189 NodeLabel.Class,
758c6b7… lmata 190 {
758c6b7… lmata 191 "name": name,
758c6b7… lmata 192 "file_path": file_path,
758c6b7… lmata 193 "line_start": node.start_point[0] + 1,
758c6b7… lmata 194 "line_end": node.end_point[0] + 1,
758c6b7… lmata 195 "docstring": f"interface: {docstring}".strip(": "),
758c6b7… lmata 196 },
758c6b7… lmata 197 )
e52aae0… lmata 198 store.create_edge(
758c6b7… lmata 199 NodeLabel.File,
758c6b7… lmata 200 {"path": file_path},
e52aae0… lmata 201 EdgeType.CONTAINS,
758c6b7… lmata 202 NodeLabel.Class,
758c6b7… lmata 203 {"name": name, "file_path": file_path},
e52aae0… lmata 204 )
e52aae0… lmata 205 stats["classes"] += 1
e52aae0… lmata 206 stats["edges"] += 1
e52aae0… lmata 207
e52aae0… lmata 208 # Walk interface body for method signatures
e52aae0… lmata 209 body = node.child_by_field_name("body")
e52aae0… lmata 210 if body:
e52aae0… lmata 211 for child in body.children:
e52aae0… lmata 212 if child.type == "method_declaration":
758c6b7… lmata 213 self._handle_method(child, source, file_path, store, stats, class_name=name)
e52aae0… lmata 214
758c6b7… lmata 215 def _handle_method(
758c6b7… lmata 216 self, node, source: bytes, file_path: str, store: GraphStore, stats: dict, class_name: str
758c6b7… lmata 217 ) -> None:
e52aae0… lmata 218 name_node = node.child_by_field_name("name")
e52aae0… lmata 219 if not name_node:
e52aae0… lmata 220 return
e52aae0… lmata 221 name = _node_text(name_node, source)
e52aae0… lmata 222 docstring = _javadoc(node, source)
e52aae0… lmata 223
758c6b7… lmata 224 store.create_node(
758c6b7… lmata 225 NodeLabel.Method,
758c6b7… lmata 226 {
758c6b7… lmata 227 "name": name,
758c6b7… lmata 228 "file_path": file_path,
758c6b7… lmata 229 "line_start": node.start_point[0] + 1,
758c6b7… lmata 230 "line_end": node.end_point[0] + 1,
758c6b7… lmata 231 "docstring": docstring,
758c6b7… lmata 232 "class_name": class_name,
758c6b7… lmata 233 },
758c6b7… lmata 234 )
e52aae0… lmata 235 store.create_edge(
758c6b7… lmata 236 NodeLabel.Class,
758c6b7… lmata 237 {"name": class_name, "file_path": file_path},
e52aae0… lmata 238 EdgeType.CONTAINS,
758c6b7… lmata 239 NodeLabel.Method,
758c6b7… lmata 240 {"name": name, "file_path": file_path},
e52aae0… lmata 241 )
e52aae0… lmata 242 stats["functions"] += 1
e52aae0… lmata 243 stats["edges"] += 1
e52aae0… lmata 244
e52aae0… lmata 245 self._extract_calls(node, source, file_path, name, store, stats)
e52aae0… lmata 246
758c6b7… lmata 247 def _handle_import(
758c6b7… lmata 248 self, node, source: bytes, file_path: str, store: GraphStore, stats: dict
758c6b7… lmata 249 ) -> None:
e52aae0… lmata 250 # import java.util.List; → strip keyword + semicolon
e52aae0… lmata 251 raw = _node_text(node, source)
758c6b7… lmata 252 module = raw.removeprefix("import").removeprefix(" static").removesuffix(";").strip()
758c6b7… lmata 253 store.create_node(
758c6b7… lmata 254 NodeLabel.Import,
758c6b7… lmata 255 {
758c6b7… lmata 256 "name": module,
758c6b7… lmata 257 "file_path": file_path,
758c6b7… lmata 258 "line_start": node.start_point[0] + 1,
758c6b7… lmata 259 "module": module,
758c6b7… lmata 260 },
758c6b7… lmata 261 )
e52aae0… lmata 262 store.create_edge(
758c6b7… lmata 263 NodeLabel.File,
758c6b7… lmata 264 {"path": file_path},
e52aae0… lmata 265 EdgeType.IMPORTS,
758c6b7… lmata 266 NodeLabel.Import,
758c6b7… lmata 267 {"name": module, "file_path": file_path},
e52aae0… lmata 268 )
e52aae0… lmata 269 stats["edges"] += 1
e52aae0… lmata 270
758c6b7… lmata 271 def _extract_calls(
758c6b7… lmata 272 self,
758c6b7… lmata 273 method_node,
758c6b7… lmata 274 source: bytes,
758c6b7… lmata 275 file_path: str,
758c6b7… lmata 276 method_name: str,
758c6b7… lmata 277 store: GraphStore,
758c6b7… lmata 278 stats: dict,
758c6b7… lmata 279 ) -> None:
e52aae0… lmata 280 def walk(node):
e52aae0… lmata 281 if node.type == "method_invocation":
e52aae0… lmata 282 name_node = node.child_by_field_name("name")
e52aae0… lmata 283 if name_node:
e52aae0… lmata 284 callee = _node_text(name_node, source)
e52aae0… lmata 285 store.create_edge(
758c6b7… lmata 286 NodeLabel.Method,
758c6b7… lmata 287 {"name": method_name, "file_path": file_path},
e52aae0… lmata 288 EdgeType.CALLS,
758c6b7… lmata 289 NodeLabel.Function,
758c6b7… lmata 290 {"name": callee, "file_path": file_path},
e52aae0… lmata 291 )
e52aae0… lmata 292 stats["edges"] += 1
e52aae0… lmata 293 for child in node.children:
e52aae0… lmata 294 walk(child)
e52aae0… lmata 295
e52aae0… lmata 296 body = method_node.child_by_field_name("body")
e52aae0… lmata 297 if body:
e52aae0… lmata 298 walk(body)

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button