forked from CodeGraphContext/CodeGraphContext
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjava.py
More file actions
336 lines (282 loc) · 12.8 KB
/
Copy pathjava.py
File metadata and controls
336 lines (282 loc) · 12.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
from pathlib import Path
from typing import Any, Dict, Optional, Tuple
import re
from codegraphcontext.utils.debug_log import debug_log, info_logger, error_logger, warning_logger
JAVA_QUERIES = {
"functions": """
(method_declaration
name: (identifier) @name
parameters: (formal_parameters) @params
) @function_node
(constructor_declaration
name: (identifier) @name
parameters: (formal_parameters) @params
) @function_node
""",
"classes": """
[
(class_declaration name: (identifier) @name)
(interface_declaration name: (identifier) @name)
(enum_declaration name: (identifier) @name)
(annotation_type_declaration name: (identifier) @name)
] @class
""",
"imports": """
(import_declaration) @import
""",
"calls": """
(method_invocation
name: (identifier) @name
)
(object_creation_expression
type: (type_identifier) @name
)
""",
}
class JavaTreeSitterParser:
def __init__(self, generic_parser_wrapper: Any):
self.generic_parser_wrapper = generic_parser_wrapper
self.language_name = "java"
self.language = generic_parser_wrapper.language
self.parser = generic_parser_wrapper.parser
self.queries = {
name: self.language.query(query_str)
for name, query_str in JAVA_QUERIES.items()
}
def parse(self, file_path: Path, is_dependency: bool = False) -> Dict[str, Any]:
try:
with open(file_path, 'r', encoding='utf-8', errors='ignore') as f:
source_code = f.read()
if not source_code.strip():
warning_logger(f"Empty or whitespace-only file: {file_path}")
return {
"file_path": str(file_path),
"functions": [],
"classes": [],
"variables": [],
"imports": [],
"function_calls": [],
"is_dependency": is_dependency,
"lang": self.language_name,
}
tree = self.parser.parse(bytes(source_code, "utf8"))
parsed_functions = []
parsed_classes = []
parsed_imports = []
parsed_calls = []
for capture_name, query in self.queries.items():
captures = query.captures(tree.root_node)
if capture_name == "functions":
parsed_functions = self._parse_functions(captures, source_code, file_path)
elif capture_name == "classes":
parsed_classes = self._parse_classes(captures, source_code, file_path)
elif capture_name == "imports":
parsed_imports = self._parse_imports(captures, source_code)
elif capture_name == "calls":
parsed_calls = self._parse_calls(captures, source_code)
return {
"file_path": str(file_path),
"functions": parsed_functions,
"classes": parsed_classes,
"variables": [],
"imports": parsed_imports,
"function_calls": parsed_calls,
"is_dependency": is_dependency,
"lang": self.language_name,
}
except Exception as e:
error_logger(f"Error parsing Java file {file_path}: {e}")
return {
"file_path": str(file_path),
"functions": [],
"classes": [],
"variables": [],
"imports": [],
"function_calls": [],
"is_dependency": is_dependency,
"lang": self.language_name,
}
def _parse_functions(self, captures: list, source_code: str, file_path: Path) -> list[Dict[str, Any]]:
functions = []
source_lines = source_code.splitlines()
for node, capture_name in captures:
if capture_name == "function_node":
try:
start_line = node.start_point[0] + 1
end_line = node.end_point[0] + 1
name_captures = [
(n, cn) for n, cn in captures
if cn == "name" and n.parent == node
]
if name_captures:
name_node = name_captures[0][0]
func_name = source_code[name_node.start_byte:name_node.end_byte]
params_captures = [
(n, cn) for n, cn in captures
if cn == "params" and n.parent == node
]
parameters = []
if params_captures:
params_node = params_captures[0][0]
params_text = source_code[params_node.start_byte:params_node.end_byte]
parameters = self._extract_parameter_names(params_text)
# Extract annotations applied to this function
annotations = []
for n, cn in captures:
if cn == "annotation" and n.parent == node:
ann_name_node = n.child_by_field_name("name")
if ann_name_node:
ann_name = source_code[ann_name_node.start_byte:ann_name_node.end_byte]
annotations.append(ann_name)
source_text = source_code[node.start_byte:node.end_byte]
functions.append({
"name": func_name,
"parameters": parameters,
"annotations":annotations,
"line_number": start_line,
"end_line": end_line,
"source": source_text,
"file_path": str(file_path),
"lang": self.language_name,
})
except Exception as e:
error_logger(f"Error parsing function in {file_path}: {e}")
continue
return functions
def _parse_classes(self, captures: list, source_code: str, file_path: Path) -> list[Dict[str, Any]]:
classes = []
for node, capture_name in captures:
if capture_name == "class":
try:
start_line = node.start_point[0] + 1
end_line = node.end_point[0] + 1
name_captures = [
(n, cn) for n, cn in captures
if cn == "name" and n.parent == node
]
if name_captures:
name_node = name_captures[0][0]
class_name = source_code[name_node.start_byte:name_node.end_byte]
source_text = source_code[node.start_byte:node.end_byte]
classes.append({
"name": class_name,
"line_number": start_line,
"end_line": end_line,
"source": source_text,
"file_path": str(file_path),
"lang": self.language_name,
})
except Exception as e:
error_logger(f"Error parsing class in {file_path}: {e}")
continue
return classes
def _parse_imports(self, captures: list, source_code: str) -> list[dict]:
imports = []
for node, capture_name in captures:
if capture_name == "import":
try:
import_text = source_code[node.start_byte:node.end_byte]
import_match = re.search(r'import\s+(?:static\s+)?([^;]+)', import_text)
if import_match:
import_path = import_match.group(1).strip()
import_data = {
"name": import_path,
"full_import_name": import_path,
"line_number": node.start_point[0] + 1,
"alias": None,
"context": (None, None),
"lang": self.language_name,
"is_dependency": False,
}
imports.append(import_data)
except Exception as e:
error_logger(f"Error parsing import: {e}")
continue
return imports
def _parse_calls(self, captures: list, source_code: str) -> list[dict]:
calls = []
seen_calls = set()
for node, capture_name in captures:
if capture_name == "name":
try:
call_name = source_code[node.start_byte:node.end_byte]
line_number = node.start_point[0] + 1
# Avoid duplicates
call_key = f"{call_name}_{line_number}"
if call_key in seen_calls:
continue
seen_calls.add(call_key)
call_data = {
"name": call_name,
"full_name": call_name,
"line_number": line_number,
"args": [],
"inferred_obj_type": None,
"context": (None, None),
"class_context": (None, None),
"lang": self.language_name,
"is_dependency": False,
}
calls.append(call_data)
except Exception as e:
error_logger(f"Error parsing call: {e}")
continue
return calls
def _extract_parameter_names(self, params_text: str) -> list[str]:
params = []
if not params_text or params_text.strip() == "()":
return params
params_content = params_text.strip("()")
if not params_content:
return params
for param in params_content.split(","):
param = param.strip()
if param:
parts = param.split()
if len(parts) >= 2:
param_name = parts[-1]
params.append(param_name)
return params
def pre_scan_java(files: list[Path], parser_wrapper) -> dict:
name_to_files = {}
for file_path in files:
try:
with open(file_path, 'r', encoding='utf-8', errors='ignore') as f:
content = f.read()
class_matches = re.finditer(r'\b(?:public\s+|private\s+|protected\s+)?(?:static\s+)?(?:abstract\s+)?(?:final\s+)?class\s+(\w+)', content)
for match in class_matches:
class_name = match.group(1)
if class_name not in name_to_files:
name_to_files[class_name] = []
name_to_files[class_name].append(str(file_path))
interface_matches = re.finditer(r'\b(?:public\s+|private\s+|protected\s+)?interface\s+(\w+)', content)
for match in interface_matches:
interface_name = match.group(1)
if interface_name not in name_to_files:
name_to_files[interface_name] = []
name_to_files[interface_name].append(str(file_path))
except Exception as e:
error_logger(f"Error pre-scanning Java file {file_path}: {e}")
return name_to_files
def _find_annotations(tree):
# Detect annotation definitions like @interface CustomAnnotation
annotations = []
for node in tree.find_all("annotation_type_declaration"):
name = node.child_by_field_name("name").text
annotations.append({
"type": "Annotation",
"name": name,
"location": node.start_point
})
return annotations
def _find_applied_annotations(tree):
# Detect usages like @Entity, @Override
applied = []
for node in tree.find_all("marker_annotation"):
name = node.child_by_field_name("name").text
applied.append({
"type": "AnnotationUsage",
"name": name,
"location": node.start_point
})
return applied