From 009ae8b6bb71cdc720152c58ebe6bc6c0d2ce08d Mon Sep 17 00:00:00 2001 From: Saurabh Sinha Date: Thu, 15 Aug 2024 11:26:09 -0400 Subject: [PATCH 1/3] Added callee_signature and enum_constants fields to JCallSite Signed-off-by: Saurabh Sinha --- cldk/models/java/models.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/cldk/models/java/models.py b/cldk/models/java/models.py index 94635517..f0b84b9b 100644 --- a/cldk/models/java/models.py +++ b/cldk/models/java/models.py @@ -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 @@ -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 From 7fd4dd2198442ce13ab9f59df81cbb9e704f8983 Mon Sep 17 00:00:00 2001 From: Saurabh Sinha Date: Thu, 15 Aug 2024 11:50:31 -0400 Subject: [PATCH 2/3] Added javasitter methods for getting fields with annotations and field accesses Signed-off-by: Saurabh Sinha --- cldk/analysis/java/treesitter/javasitter.py | 73 +++++++++++++++++++++ 1 file changed, 73 insertions(+) diff --git a/cldk/analysis/java/treesitter/javasitter.py b/cldk/analysis/java/treesitter/javasitter.py index 55fa3d89..3d8dee48 100644 --- a/cldk/analysis/java/treesitter/javasitter.py +++ b/cldk/analysis/java/treesitter/javasitter.py @@ -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. From beaf009646c8f67d38eb6fd461d87fb19eb00956 Mon Sep 17 00:00:00 2001 From: Saurabh Sinha Date: Thu, 15 Aug 2024 13:14:44 -0400 Subject: [PATCH 3/3] Fixed dereference for JCompilationUnit object in symbol table Signed-off-by: Saurabh Sinha --- cldk/analysis/java/codeanalyzer/codeanalyzer.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cldk/analysis/java/codeanalyzer/codeanalyzer.py b/cldk/analysis/java/codeanalyzer/codeanalyzer.py index 671f9911..052622ea 100644 --- a/cldk/analysis/java/codeanalyzer/codeanalyzer.py +++ b/cldk/analysis/java/codeanalyzer/codeanalyzer.py @@ -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]: """