31
31
from cldk .analysis .commons .treesitter import TreesitterJava
32
32
from cldk .models .java import JGraphEdges
33
33
from cldk .models .java .enums import CRUDOperationType
34
- from cldk .models .java .models import JApplication , JCRUDOperation , JCallable , JField , JMethodDetail , JType , JCompilationUnit , JGraphEdgesST
34
+ from cldk .models .java .models import JApplication , JCRUDOperation , JCallable , JCallableParameter , JComment , JField , JMethodDetail , JType , JCompilationUnit , JGraphEdgesST
35
35
from cldk .utils .exceptions .exceptions import CodeanalyzerExecutionException
36
36
37
37
logger = logging .getLogger (__name__ )
@@ -47,7 +47,6 @@ class JCodeanalyzer:
47
47
analysis_json_path (str or Path, optional): The path to save the intermediate code analysis outputs.
48
48
If None, the analysis will be read from the pipe.
49
49
analysis_level (str): The level of analysis ('symbol_table' or 'call_graph').
50
- use_graalvm_binary (bool): If True, the GraalVM binary will be used instead of the codeanalyzer jar.
51
50
eager_analysis (bool): If True, the analysis will be performed every time the object is created.
52
51
53
52
Methods:
@@ -92,15 +91,13 @@ def __init__(
92
91
analysis_backend_path : Union [str , Path , None ],
93
92
analysis_json_path : Union [str , Path , None ],
94
93
analysis_level : str ,
95
- use_graalvm_binary : bool ,
96
94
eager_analysis : bool ,
97
95
target_files : List [str ] | None ,
98
96
) -> None :
99
97
self .project_dir = project_dir
100
98
self .source_code = source_code
101
99
self .analysis_backend_path = analysis_backend_path
102
100
self .analysis_json_path = analysis_json_path
103
- self .use_graalvm_binary = use_graalvm_binary
104
101
self .eager_analysis = eager_analysis
105
102
self .analysis_level = analysis_level
106
103
self .target_files = target_files
@@ -128,27 +125,22 @@ def _get_codeanalyzer_exec(self) -> List[str]:
128
125
List[str]: The executable command for codeanalyzer.
129
126
130
127
Notes:
131
- - If the use_graalvm_binary flag is set, the codeanalyzer binary from GraalVM will be used.
132
128
- If the analysis_backend_path is provided, the codeanalyzer jar from that path will be used.
133
129
- If not provided, the latest codeanalyzer jar from GitHub will be downloaded.
134
130
"""
135
131
136
- if self .use_graalvm_binary :
137
- with resources .as_file (resources .files ("cldk.analysis.java.codeanalyzer.bin" ) / "codeanalyzer" ) as codeanalyzer_bin_path :
138
- codeanalyzer_exec = shlex .split (codeanalyzer_bin_path .__str__ ())
132
+ if self .analysis_backend_path :
133
+ analysis_backend_path = Path (self .analysis_backend_path )
134
+ logger .info (f"Using codeanalyzer jar from { analysis_backend_path } " )
135
+ codeanalyzer_jar_file = next (analysis_backend_path .rglob ("codeanalyzer-*.jar" ), None )
136
+ if codeanalyzer_jar_file is None :
137
+ raise CodeanalyzerExecutionException ("Codeanalyzer jar not found in the provided path." )
138
+ codeanalyzer_exec = shlex .split (f"java -jar { codeanalyzer_jar_file } " )
139
139
else :
140
- if self .analysis_backend_path :
141
- analysis_backend_path = Path (self .analysis_backend_path )
142
- logger .info (f"Using codeanalyzer jar from { analysis_backend_path } " )
143
- codeanalyzer_jar_file = next (analysis_backend_path .rglob ("codeanalyzer-*.jar" ), None )
144
- if codeanalyzer_jar_file is None :
145
- raise CodeanalyzerExecutionException ("Codeanalyzer jar not found in the provided path." )
140
+ # Since the path to codeanalyzer.jar we will use the default jar from the cldk/analysis/java/codeanalyzer/jar folder
141
+ with resources .as_file (resources .files ("cldk.analysis.java.codeanalyzer.jar" )) as codeanalyzer_jar_path :
142
+ codeanalyzer_jar_file = next (codeanalyzer_jar_path .rglob ("codeanalyzer-*.jar" ), None )
146
143
codeanalyzer_exec = shlex .split (f"java -jar { codeanalyzer_jar_file } " )
147
- else :
148
- # Since the path to codeanalyzer.jar we will use the default jar from the cldk/analysis/java/codeanalyzer/jar folder
149
- with resources .as_file (resources .files ("cldk.analysis.java.codeanalyzer.jar" )) as codeanalyzer_jar_path :
150
- codeanalyzer_jar_file = next (codeanalyzer_jar_path .rglob ("codeanalyzer-*.jar" ), None )
151
- codeanalyzer_exec = shlex .split (f"java -jar { codeanalyzer_jar_file } " )
152
144
return codeanalyzer_exec
153
145
154
146
@staticmethod
@@ -497,6 +489,29 @@ def get_method(self, qualified_class_name, method_signature) -> JCallable:
497
489
if cd == method_signature :
498
490
return ci .callable_declarations [cd ]
499
491
492
+ def get_method_parameters (self , qualified_class_name , method_signature ) -> List [JCallableParameter ]:
493
+ """Should return a dictionary of method parameters given the qualified class name and method signature.
494
+
495
+ Args:
496
+ qualified_class_name (str): The qualified name of the class.
497
+ method_signature (str): The signature of the method.
498
+
499
+ Returns:
500
+ Dict[str, str]: A dictionary of method parameters for the given qualified class name and method signature.
501
+ """
502
+ return self .get_method (qualified_class_name , method_signature ).parameters
503
+
504
+ def get_parameters_from_callable (self , callable : JCallable ) -> List [JCallableParameter ]:
505
+ """Should return a dictionary of method parameters given the callable.
506
+
507
+ Args:
508
+ callable (JCallable): The callable object.
509
+
510
+ Returns:
511
+ Dict[str, str]: A dictionary of method parameters for the given callable.
512
+ """
513
+ return callable .parameters
514
+
500
515
def get_java_file (self , qualified_class_name ) -> str :
501
516
"""Should return java file name given the qualified class name.
502
517
@@ -1006,3 +1021,66 @@ def get_all_delete_operations(self) -> List[Dict[str, Union[JType, JCallable, Li
1006
1021
}
1007
1022
)
1008
1023
return crud_delete_operations
1024
+
1025
+ # Some APIs to process comments
1026
+ def get_comments_in_a_method (self , qualified_class_name : str , method_signature : str ) -> List [JComment ]:
1027
+ """Get all comments in a method.
1028
+
1029
+ Args:
1030
+ qualified_class_name (str): Qualified name of the class.
1031
+ method_signature (str): Signature of the method.
1032
+
1033
+ Returns:
1034
+ List[str]: List of comments in the method.
1035
+ """
1036
+ callable = self .get_method (qualified_class_name , method_signature )
1037
+ return callable .comments
1038
+
1039
+ def get_comments_in_a_class (self , qualified_class_name : str ) -> List [JComment ]:
1040
+ """Get all comments in a class.
1041
+
1042
+ Args:
1043
+ qualified_class_name (str): Qualified name of the class.
1044
+
1045
+ Returns:
1046
+ List[str]: List of comments in the class.
1047
+ """
1048
+ klass = self .get_class (qualified_class_name )
1049
+ return klass .comments
1050
+
1051
+ def get_comment_in_file (self , file_path : str ) -> List [JComment ]:
1052
+ """Get all comments in a file.
1053
+
1054
+ Args:
1055
+ file_path (str): Path to the file.
1056
+
1057
+ Returns:
1058
+ List[str]: List of comments in the file.
1059
+ """
1060
+ compilation_unit = self .get_symbol_table ().get (file_path , None )
1061
+ if compilation_unit is None :
1062
+ raise CodeanalyzerExecutionException (f"File { file_path } not found in the symbol table." )
1063
+ return compilation_unit .comments
1064
+
1065
+ def get_all_comments (self ) -> Dict [str , List [JComment ]]:
1066
+ """Get all comments in the Java application.
1067
+
1068
+ Returns:
1069
+ Dict[str, List[str]]: Dictionary of file paths and their corresponding comments.
1070
+ """
1071
+ comments = {}
1072
+ for file_path , _ in self .get_symbol_table ().items ():
1073
+ comments [file_path ] = self .get_comment_in_file (file_path )
1074
+ return comments
1075
+
1076
+ def get_all_docstrings (self ) -> List [Tuple [str , JComment ]]:
1077
+ """Get all docstrings in the Java application.
1078
+
1079
+ Returns:
1080
+ Dict[str, List[str]]: Dictionary of file paths and their corresponding docstrings.
1081
+ """
1082
+ docstrings = []
1083
+ for file_path , list_of_comments in self .get_all_comments ().items ():
1084
+ docstrings += [(file_path , docstring ) for docstring in list_of_comments if docstring .is_javadoc ]
1085
+
1086
+ return docstrings
0 commit comments