Skip to content
Merged
Show file tree
Hide file tree
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
3 changes: 2 additions & 1 deletion cldk/analysis/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
from .program_dependence_graph import ProgramDependenceGraph
from .system_dependence_graph import SystemDependenceGraph
from .symbol_table import SymbolTable
from .analysis_level import AnalysisLevel

__all__ = ["CallGraph", "ProgramDependenceGraph", "SystemDependenceGraph", "SymbolTable"]
__all__ = ["CallGraph", "ProgramDependenceGraph", "SystemDependenceGraph", "SymbolTable", "AnalysisLevel"]
9 changes: 9 additions & 0 deletions cldk/analysis/analysis_level.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from enum import Enum


class AnalysisLevel(str, Enum):
"""Analysis levels"""
symbol_table = "symbol-table"
call_graph = "call-graph"
program_dependency_graph = "program-dependency-graph"
system_dependency_graph = "system-dependency-graph"
316 changes: 284 additions & 32 deletions cldk/analysis/java/codeanalyzer/codeanalyzer.py

Large diffs are not rendered by default.

38 changes: 32 additions & 6 deletions cldk/analysis/java/java.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from typing import Dict, List, Tuple, Set
from networkx import DiGraph

from cldk.analysis import SymbolTable, CallGraph
from cldk.analysis import SymbolTable, CallGraph, AnalysisLevel
from cldk.models.java import JCallable
from cldk.models.java import JApplication
from cldk.models.java.models import JCompilationUnit, JMethodDetail, JType, JField
Expand Down Expand Up @@ -157,7 +157,8 @@ def get_call_graph_json(self) -> str:
raise NotImplementedError("Producing a call graph over a single file is not implemented yet.")
return self.backend.get_call_graph_json()

def get_callers(self, target_class_name: str, target_method_declaration: str):
def get_callers(self, target_class_name: str, target_method_declaration: str,
using_symbol_table: bool = False) -> Dict:
"""
Get all the caller details for a given java method.

Expand All @@ -168,7 +169,7 @@ def get_callers(self, target_class_name: str, target_method_declaration: str):
"""
if self.source_code:
raise NotImplementedError("Generating all callers over a single file is not implemented yet.")
return self.backend.get_all_callers(target_class_name, target_method_declaration)
return self.backend.get_all_callers(target_class_name, target_method_declaration, using_symbol_table)

def get_callees(self, source_class_name: str, source_method_declaration: str):
"""
Expand Down Expand Up @@ -437,25 +438,50 @@ def get_implemented_interfaces(self, qualified_class_name) -> List[str]:
raise NotImplementedError(f"Support for this functionality has not been implemented yet.")
return self.backend.get_implemented_interfaces(qualified_class_name)

def get_class_call_graph(self, qualified_class_name: str, method_name: str | None = None) -> (List)[Tuple[JMethodDetail, JMethodDetail]]:
def __get_class_call_graph_using_symbol_table(self, qualified_class_name: str, method_signature: str | None = None) -> (List)[Tuple[JMethodDetail, JMethodDetail]]:
"""
A call graph using symbol table for a given class and a given method.
Args:
qualified_class_name:
method_signature:

Returns:
List[Tuple[JMethodDetail, JMethodDetail]]
An edge list of the call graph for the given class and method.
"""
if self.analysis_backend in [AnalysisEngine.CODEQL, AnalysisEngine.TREESITTER]:
raise NotImplementedError(f"Support for this functionality has not been implemented yet.")
return self.backend.get_class_call_graph_using_symbol_table(qualified_class_name, method_signature)

def get_class_call_graph(self, qualified_class_name: str, method_signature: str | None = None,
using_symbol_table: bool = False) -> List[Tuple[JMethodDetail, JMethodDetail]]:
"""
A call graph for a given class and (optionally) a given method.

Parameters
----------
using_symbol_table: bool
Generate call graph using symbol table
qualified_class_name : str
The qualified name of the class.
method_name : str, optional
The name of the method in the class.
The signature of the method in the class.

Returns
-------
List[Tuple[JMethodDetail, JMethodDetail]]
An edge list of the call graph for the given class and method.

Args:
using_symbol_table:
using_symbol_table:
"""
if using_symbol_table:
return self.__get_class_call_graph_using_symbol_table(qualified_class_name=qualified_class_name,
method_signature=method_signature)
if self.analysis_backend in [AnalysisEngine.CODEQL, AnalysisEngine.TREESITTER]:
raise NotImplementedError(f"Support for this functionality has not been implemented yet.")
return self.backend.get_class_call_graph(qualified_class_name, method_name)
return self.backend.get_class_call_graph(qualified_class_name, method_signature)

def get_entry_point_classes(self) -> Dict[str, JType]:
"""
Expand Down
4 changes: 2 additions & 2 deletions cldk/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def analysis(
analysis_backend: str | None = "codeanalyzer",
analysis_level: str = "symbol_table",
analysis_backend_path: str | None = None,
analysis_json_path: str | Path | None = None,
analysis_json_path: str | Path = '.',
use_graalvm_binary: bool = False,
) -> JavaAnalysis:
"""
Expand Down Expand Up @@ -114,7 +114,7 @@ def treesitter_parser(self):
else:
raise NotImplementedError(f"Treesitter parser for {self.language} is not implemented yet.")

def tree_sitter_utils(self, source_code: str):
def tree_sitter_utils(self, source_code: str) -> [TreesitterSanitizer| NotImplementedError]:
"""
Parse the project using treesitter.

Expand Down
17 changes: 13 additions & 4 deletions cldk/models/java/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,10 @@ class JCallSite(BaseModel):
return_type: str = ""
callee_signature: str = ""
is_static_call: bool
is_private: bool
is_public: bool
is_protected: bool
is_unspecified: bool
is_private: bool
is_public: bool
is_protected: bool
is_unspecified: bool
is_constructor_call: bool
start_line: int
start_column: int
Expand Down Expand Up @@ -356,6 +356,15 @@ def __hash__(self):
return hash(tuple(self))


class JGraphEdgesST(BaseModel):
source: JMethodDetail
target: JMethodDetail
type: str
weight: str
source_kind: str | None = None
destination_kind: str | None = None


class JGraphEdges(BaseModel):
source: JMethodDetail
target: JMethodDetail
Expand Down
Loading