-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmethod_analyzer.py
More file actions
75 lines (67 loc) · 3.23 KB
/
method_analyzer.py
File metadata and controls
75 lines (67 loc) · 3.23 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
from typing import Dict, Any, List
import javalang
from java_type_converter import convert_type
def extract_methods(methods: List[javalang.tree.MethodDeclaration]) -> List[Dict[str, Any]]:
return [{
"name": method.name,
"return_type": convert_type(method.return_type) if method.return_type else "void",
"parameters": extract_parameters(method.parameters),
"modifiers": list(method.modifiers),
"throws": [exception for exception in method.throws] if method.throws else [],
"body": extract_method_body(method.body) if method.body else None
} for method in methods]
def extract_constructors(constructors: List[javalang.tree.ConstructorDeclaration]) -> List[Dict[str, Any]]:
return [{
"name": constructor.name,
"parameters": extract_parameters(constructor.parameters),
"modifiers": list(constructor.modifiers),
"throws": [exception for exception in constructor.throws] if constructor.throws else [],
"body": extract_method_body(constructor.body) if constructor.body else None
} for constructor in constructors]
def extract_parameters(parameters: List[javalang.tree.FormalParameter]) -> List[Dict[str, str]]:
return [{
"name": param.name,
"type": convert_type(param.type)
} for param in parameters]
def extract_method_body(body):
statements = []
for statement in body:
if isinstance(statement, javalang.tree.StatementExpression):
if isinstance(statement.expression, javalang.tree.MethodInvocation):
if statement.expression.member == 'println' and statement.expression.qualifier == 'System.out':
statements.append("System.out.println call")
else:
statements.append(f"Method call: {statement.expression.member}")
elif isinstance(statement.expression, javalang.tree.Assignment):
statements.append("Assignment operation")
elif isinstance(statement, javalang.tree.ReturnStatement):
statements.append("Return statement")
# Add more statement types as needed
return statements if statements else "Empty method body"
def get_visibility(modifiers):
visibility_modifiers = ['public', 'protected', 'private']
for modifier in modifiers:
if modifier in visibility_modifiers:
return modifier
return 'package-private' # default visibility in Java
# These functions are kept for backwards compatibility
def extract_class_info(node: javalang.tree.ClassDeclaration) -> Dict[str, Any]:
return {
"name": node.name,
"methods": extract_methods(node.methods),
"constructors": extract_constructors(node.constructors),
"modifiers": list(node.modifiers)
}
def extract_interface_info(node: javalang.tree.InterfaceDeclaration) -> Dict[str, Any]:
return {
"name": node.name,
"methods": extract_methods(node.methods),
"modifiers": list(node.modifiers)
}
def extract_enum_info(node: javalang.tree.EnumDeclaration) -> Dict[str, Any]:
return {
"name": node.name,
"constants": [const.name for const in node.body.constants],
"methods": extract_methods(node.methods),
"modifiers": list(node.modifiers)
}