Skip to content

fixing caller method #35

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 12, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 25 additions & 23 deletions cldk/analysis/java/codeanalyzer/codeanalyzer.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,15 @@ class JCodeanalyzer:
"""

def __init__(
self,
project_dir: Union[str, Path],
source_code: str | None,
analysis_backend_path: Union[str, Path, None],
analysis_json_path: Union[str, Path, None],
analysis_level: str,
use_graalvm_binary: bool,
eager_analysis: bool,
target_files: List[str] | None
self,
project_dir: Union[str, Path],
source_code: str | None,
analysis_backend_path: Union[str, Path, None],
analysis_json_path: Union[str, Path, None],
analysis_level: str,
use_graalvm_binary: bool,
eager_analysis: bool,
target_files: List[str] | None
) -> None:
self.project_dir = project_dir
self.source_code = source_code
Expand Down Expand Up @@ -173,7 +173,7 @@ def _get_codeanalyzer_exec(self) -> List[str]:
resources.files("cldk.analysis.java.codeanalyzer.bin") / "codeanalyzer") as codeanalyzer_bin_path:
codeanalyzer_exec = shlex.split(codeanalyzer_bin_path.__str__())
else:

if self.analysis_backend_path:
analysis_backend_path = Path(self.analysis_backend_path)
logger.info(f"Using codeanalyzer.jar from {analysis_backend_path}")
Expand Down Expand Up @@ -207,7 +207,7 @@ def _init_codeanalyzer(self, analysis_level=1) -> JApplication:
if self.target_files:
target_file_options = ' -t '.join([s.strip() for s in self.target_files])
codeanalyzer_args = codeanalyzer_exec + shlex.split(
f"-i {Path(self.project_dir)} --analysis-level={analysis_level} -t {target_file_options}"
f"-i {Path(self.project_dir)} --analysis-level={analysis_level} -t {target_file_options}"
)
else:
codeanalyzer_args = codeanalyzer_exec + shlex.split(
Expand Down Expand Up @@ -244,8 +244,8 @@ def _init_codeanalyzer(self, analysis_level=1) -> JApplication:
# of the existence of the analysis file.
# Create the executable command for codeanalyzer.
codeanalyzer_args = codeanalyzer_exec + shlex.split(
f"-i {Path(self.project_dir)} --analysis-level={analysis_level} -o {self.analysis_json_path}"
)
f"-i {Path(self.project_dir)} --analysis-level={analysis_level} -o {self.analysis_json_path}"
)
is_run_code_analyzer = True

if is_run_code_analyzer:
Expand Down Expand Up @@ -430,8 +430,9 @@ def get_all_callers(self, target_class_name: str, target_method_signature: str,
caller_detail_dict = {}
call_graph = None
if using_symbol_table:
call_graph = self.__raw_call_graph_using_symbol_table_target_method(target_class_name=target_class_name,
target_method_signature=target_method_signature)
call_graph = self.__call_graph_using_symbol_table(qualified_class_name=target_class_name,
method_signature=target_method_signature,
is_target_method=True)
else:
call_graph = self.call_graph
if (target_method_signature, target_class_name) not in call_graph.nodes():
Expand Down Expand Up @@ -768,7 +769,7 @@ def get_class_call_graph_using_symbol_table(self, qualified_class_name: str,

def __call_graph_using_symbol_table(self,
qualified_class_name: str,
method_signature: str, is_target_method: bool = False)-> DiGraph:
method_signature: str, is_target_method: bool = False) -> DiGraph:
"""
Generate call graph using symbol table
Args:
Expand All @@ -782,10 +783,11 @@ def __call_graph_using_symbol_table(self,
cg = nx.DiGraph()
sdg = None
if is_target_method:
sdg = None
sdg = self.__raw_call_graph_using_symbol_table_target_method(target_class_name=qualified_class_name,
target_method_signature=method_signature)
else:
sdg = self.__raw_call_graph_using_symbol_table(qualified_class_name=qualified_class_name,
method_signature=method_signature)
method_signature=method_signature)
tsu = JavaSitter()
edge_list = [
(
Expand All @@ -812,8 +814,8 @@ def __call_graph_using_symbol_table(self,
return cg

def __raw_call_graph_using_symbol_table_target_method(self,
target_class_name: str,
target_method_signature: str,
target_class_name: str,
target_method_signature: str,
cg=None) -> list[JGraphEdgesST]:
"""
Generates call graph using symbol table information given the target method and target class
Expand All @@ -832,7 +834,7 @@ def __raw_call_graph_using_symbol_table_target_method(self,
for class_name in self.get_all_classes():
for method in self.get_all_methods_in_class(qualified_class_name=class_name):
method_details = self.get_method(qualified_class_name=class_name,
method_signature=method)
method_signature=method)
for call_site in method_details.call_sites:
source_method_details = None
source_class = ''
Expand All @@ -856,9 +858,9 @@ def __raw_call_graph_using_symbol_table_target_method(self,
if call_site.receiver_type != "":
# call to any class
if self.get_class(qualified_class_name=call_site.receiver_type):
if callee_signature==target_method_signature and call_site.receiver_type == target_class_name:
if callee_signature == target_method_signature and call_site.receiver_type == target_class_name:
source_method_details = self.get_method(method_signature=method,
qualified_class_name=class_name)
qualified_class_name=class_name)
source_class = class_name
else:
# check if any method exists with the signature in the class even if the receiver type is blank
Expand Down