Skip to content

Commit ac65736

Browse files
authored
Merge pull request #35 from IBM/fix-get-callers
fixing caller method
2 parents e9587bc + c8777ad commit ac65736

File tree

1 file changed

+25
-23
lines changed

1 file changed

+25
-23
lines changed

cldk/analysis/java/codeanalyzer/codeanalyzer.py

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,15 @@ class JCodeanalyzer:
5151
"""
5252

5353
def __init__(
54-
self,
55-
project_dir: Union[str, Path],
56-
source_code: str | None,
57-
analysis_backend_path: Union[str, Path, None],
58-
analysis_json_path: Union[str, Path, None],
59-
analysis_level: str,
60-
use_graalvm_binary: bool,
61-
eager_analysis: bool,
62-
target_files: List[str] | None
54+
self,
55+
project_dir: Union[str, Path],
56+
source_code: str | None,
57+
analysis_backend_path: Union[str, Path, None],
58+
analysis_json_path: Union[str, Path, None],
59+
analysis_level: str,
60+
use_graalvm_binary: bool,
61+
eager_analysis: bool,
62+
target_files: List[str] | None
6363
) -> None:
6464
self.project_dir = project_dir
6565
self.source_code = source_code
@@ -173,7 +173,7 @@ def _get_codeanalyzer_exec(self) -> List[str]:
173173
resources.files("cldk.analysis.java.codeanalyzer.bin") / "codeanalyzer") as codeanalyzer_bin_path:
174174
codeanalyzer_exec = shlex.split(codeanalyzer_bin_path.__str__())
175175
else:
176-
176+
177177
if self.analysis_backend_path:
178178
analysis_backend_path = Path(self.analysis_backend_path)
179179
logger.info(f"Using codeanalyzer.jar from {analysis_backend_path}")
@@ -207,7 +207,7 @@ def _init_codeanalyzer(self, analysis_level=1) -> JApplication:
207207
if self.target_files:
208208
target_file_options = ' -t '.join([s.strip() for s in self.target_files])
209209
codeanalyzer_args = codeanalyzer_exec + shlex.split(
210-
f"-i {Path(self.project_dir)} --analysis-level={analysis_level} -t {target_file_options}"
210+
f"-i {Path(self.project_dir)} --analysis-level={analysis_level} -t {target_file_options}"
211211
)
212212
else:
213213
codeanalyzer_args = codeanalyzer_exec + shlex.split(
@@ -244,8 +244,8 @@ def _init_codeanalyzer(self, analysis_level=1) -> JApplication:
244244
# of the existence of the analysis file.
245245
# Create the executable command for codeanalyzer.
246246
codeanalyzer_args = codeanalyzer_exec + shlex.split(
247-
f"-i {Path(self.project_dir)} --analysis-level={analysis_level} -o {self.analysis_json_path}"
248-
)
247+
f"-i {Path(self.project_dir)} --analysis-level={analysis_level} -o {self.analysis_json_path}"
248+
)
249249
is_run_code_analyzer = True
250250

251251
if is_run_code_analyzer:
@@ -430,8 +430,9 @@ def get_all_callers(self, target_class_name: str, target_method_signature: str,
430430
caller_detail_dict = {}
431431
call_graph = None
432432
if using_symbol_table:
433-
call_graph = self.__raw_call_graph_using_symbol_table_target_method(target_class_name=target_class_name,
434-
target_method_signature=target_method_signature)
433+
call_graph = self.__call_graph_using_symbol_table(qualified_class_name=target_class_name,
434+
method_signature=target_method_signature,
435+
is_target_method=True)
435436
else:
436437
call_graph = self.call_graph
437438
if (target_method_signature, target_class_name) not in call_graph.nodes():
@@ -768,7 +769,7 @@ def get_class_call_graph_using_symbol_table(self, qualified_class_name: str,
768769

769770
def __call_graph_using_symbol_table(self,
770771
qualified_class_name: str,
771-
method_signature: str, is_target_method: bool = False)-> DiGraph:
772+
method_signature: str, is_target_method: bool = False) -> DiGraph:
772773
"""
773774
Generate call graph using symbol table
774775
Args:
@@ -782,10 +783,11 @@ def __call_graph_using_symbol_table(self,
782783
cg = nx.DiGraph()
783784
sdg = None
784785
if is_target_method:
785-
sdg = None
786+
sdg = self.__raw_call_graph_using_symbol_table_target_method(target_class_name=qualified_class_name,
787+
target_method_signature=method_signature)
786788
else:
787789
sdg = self.__raw_call_graph_using_symbol_table(qualified_class_name=qualified_class_name,
788-
method_signature=method_signature)
790+
method_signature=method_signature)
789791
tsu = JavaSitter()
790792
edge_list = [
791793
(
@@ -812,8 +814,8 @@ def __call_graph_using_symbol_table(self,
812814
return cg
813815

814816
def __raw_call_graph_using_symbol_table_target_method(self,
815-
target_class_name: str,
816-
target_method_signature: str,
817+
target_class_name: str,
818+
target_method_signature: str,
817819
cg=None) -> list[JGraphEdgesST]:
818820
"""
819821
Generates call graph using symbol table information given the target method and target class
@@ -832,7 +834,7 @@ def __raw_call_graph_using_symbol_table_target_method(self,
832834
for class_name in self.get_all_classes():
833835
for method in self.get_all_methods_in_class(qualified_class_name=class_name):
834836
method_details = self.get_method(qualified_class_name=class_name,
835-
method_signature=method)
837+
method_signature=method)
836838
for call_site in method_details.call_sites:
837839
source_method_details = None
838840
source_class = ''
@@ -856,9 +858,9 @@ def __raw_call_graph_using_symbol_table_target_method(self,
856858
if call_site.receiver_type != "":
857859
# call to any class
858860
if self.get_class(qualified_class_name=call_site.receiver_type):
859-
if callee_signature==target_method_signature and call_site.receiver_type == target_class_name:
861+
if callee_signature == target_method_signature and call_site.receiver_type == target_class_name:
860862
source_method_details = self.get_method(method_signature=method,
861-
qualified_class_name=class_name)
863+
qualified_class_name=class_name)
862864
source_class = class_name
863865
else:
864866
# check if any method exists with the signature in the class even if the receiver type is blank

0 commit comments

Comments
 (0)