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
2 changes: 1 addition & 1 deletion cldk/analysis/java/codeanalyzer/codeanalyzer.py
Original file line number Diff line number Diff line change
Expand Up @@ -552,7 +552,7 @@ def get_java_compilation_unit(self, file_path: str) -> JCompilationUnit:

if self.application is None:
self.application = self._init_codeanalyzer()
return self.application[file_path]
return self.application.symbol_table[file_path]

def get_all_methods_in_class(self, qualified_class_name) -> Dict[str, JCallable]:
"""
Expand Down
73 changes: 73 additions & 0 deletions cldk/analysis/java/treesitter/javasitter.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,79 @@ def get_methods_with_annotations(self, source_class_code: str, annotations: List
annotation_method_dict[annotation] = [method]
return annotation_method_dict

def get_fields_with_annotations(self, source_class_code: str) -> Dict[str, Dict]:
"""
Returns a dictionary of field names and field bodies.

Parameters:
-----------
source_class_code : str
String containing code for a java class.
Returns:
--------
Dict[str,Dict]
Dictionary with field names as keys and a dictionary of annotation and body as values.
"""
query = """
(field_declaration
(variable_declarator
name: (identifier) @field_name
)
)
"""
captures: Captures = self.frame_query_and_capture_output(query, source_class_code)
field_dict = {}
for capture in captures:
if capture.name == "field_name":
field_name = capture.node.text.decode()
inner_dict = {}
annotation = None
field_node = self.safe_ascend(capture.node, 2)
body = field_node.text.decode()
for fc in field_node.children:
if fc.type == "modifiers":
for mc in fc.children:
if mc.type == "marker_annotation":
annotation = mc.text.decode()
inner_dict["annotation"] = annotation
inner_dict["body"] = body
field_dict[field_name] = inner_dict
return field_dict

def get_field_accesses(self, source_class_code: str) -> Dict[str, list[list[int]]]:
"""
Returns a dictionary of field names with start and end positions of field accesses.

Parameters:
-----------
source_class_code : str
String containing code for a java class.
Returns:
--------
Dict[str, [[int, int], [int, int]]]
Dictionary with field names as keys and a list of starting and ending line, and starting and ending column.
"""
query = """
(field_access
field:(identifier) @field_name
)
"""
captures: Captures = self.frame_query_and_capture_output(query, source_class_code)
field_dict = {}
for capture in captures:
if capture.name == "field_name":
field_name = capture.node.text.decode()
field_node = self.safe_ascend(capture.node, 2)
start_line = field_node.start_point[0]
start_column = field_node.start_point[1]
end_line = field_node.end_point[0]
end_column = field_node.end_point[1]
start_list = [start_line, start_column]
end_list = [end_line, end_column]
position = [start_list, end_list]
field_dict[field_name] = position
return field_dict

def get_all_type_invocations(self, source_code: str) -> Set[str]:
"""
Given the source code, get all the type invocations in the source code.
Expand Down
3 changes: 2 additions & 1 deletion cldk/models/java/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ class JCallSite(BaseModel):
receiver_type: str
argument_types: List[str]
return_type: str = ""
callee_signature: str = ""
is_static_call: bool
is_private: bool
is_public: bool
Expand Down Expand Up @@ -299,7 +300,7 @@ class JType(BaseModel):
nested_type_declerations: List[str] = []
callable_declarations: Dict[str, JCallable] = {}
field_declarations: List[JField] = []
enum_JavaEEEntryPoints: List[JEnumConstant] = []
enum_constants: List[JEnumConstant] = []

# first get the data in raw form and check if the class is concrete or not, before any model validation is done
# for this we assume if a class is not an interface or abstract it is concrete
Expand Down