@@ -51,14 +51,15 @@ class JCodeanalyzer:
51
51
"""
52
52
53
53
def __init__ (
54
- self ,
55
- project_dir : Union [str , Path ],
56
- source_code : str | None ,
57
- analysis_backend_path : Union [str , Path , None ],
58
- analysis_json_path : Union [str , Path , None ],
59
- analysis_level : str ,
60
- use_graalvm_binary : bool ,
61
- eager_analysis : bool ,
54
+ self ,
55
+ project_dir : Union [str , Path ],
56
+ source_code : str | None ,
57
+ analysis_backend_path : Union [str , Path , None ],
58
+ analysis_json_path : Union [str , Path , None ],
59
+ analysis_level : str ,
60
+ use_graalvm_binary : bool ,
61
+ eager_analysis : bool ,
62
+ target_files : List [str ] | None
62
63
) -> None :
63
64
self .project_dir = project_dir
64
65
self .source_code = source_code
@@ -67,6 +68,7 @@ def __init__(
67
68
self .use_graalvm_binary = use_graalvm_binary
68
69
self .eager_analysis = eager_analysis
69
70
self .analysis_level = analysis_level
71
+ self .target_files = target_files
70
72
self .application = self ._init_codeanalyzer (
71
73
analysis_level = 1 if analysis_level == AnalysisLevel .symbol_table else 2 )
72
74
# Attributes related the Java code analysis...
@@ -198,11 +200,19 @@ def _init_codeanalyzer(self, analysis_level=1) -> JApplication:
198
200
"""
199
201
200
202
codeanalyzer_exec = self ._get_codeanalyzer_exec ()
201
-
203
+ codeanalyzer_args = ''
202
204
if self .analysis_json_path is None :
203
205
logger .info ("Reading analysis from the pipe." )
204
- codeanalyzer_args = codeanalyzer_exec + shlex .split (
205
- f"-i { Path (self .project_dir )} --analysis-level={ analysis_level } " )
206
+ # If target file is provided, the input is merged into a single string and passed to codeanalyzer
207
+ if self .target_files :
208
+ target_file_options = ' -t ' .join ([s .strip () for s in self .target_files ])
209
+ codeanalyzer_args = codeanalyzer_exec + shlex .split (
210
+ f"-i { Path (self .project_dir )} --analysis-level={ analysis_level } -t { target_file_options } "
211
+ )
212
+ else :
213
+ codeanalyzer_args = codeanalyzer_exec + shlex .split (
214
+ f"-i { Path (self .project_dir )} --analysis-level={ analysis_level } "
215
+ )
206
216
try :
207
217
logger .info (f"Running codeanalyzer: { ' ' .join (codeanalyzer_args )} " )
208
218
console_out : CompletedProcess [str ] = subprocess .run (
@@ -216,15 +226,29 @@ def _init_codeanalyzer(self, analysis_level=1) -> JApplication:
216
226
raise CodeanalyzerExecutionException (str (e )) from e
217
227
218
228
else :
229
+ # Check if the code analyzer needs to be run
230
+ is_run_code_analyzer = False
219
231
analysis_json_path_file = Path (self .analysis_json_path ).joinpath ("analysis.json" )
220
- if not analysis_json_path_file .exists () or self .eager_analysis :
221
- # If the analysis file does not exist, we'll run the analysis. Alternately, if the eager_analysis
222
- # flag is set, we'll run the analysis every time the object is created. This will happen regradless
223
- # of the existence of the analysis file.
224
- # Create the executable command for codeanalyzer.
232
+ # If target file is provided, the input is merged into a single string and passed to codeanalyzer
233
+ if self .target_files :
234
+ target_file_options = ' -t ' .join ([s .strip () for s in self .target_files ])
225
235
codeanalyzer_args = codeanalyzer_exec + shlex .split (
226
- f"-i { Path (self .project_dir )} --analysis-level={ analysis_level } -o { self .analysis_json_path } " )
227
-
236
+ f"-i { Path (self .project_dir )} --analysis-level={ analysis_level } "
237
+ f" -o { self .analysis_json_path } -t { target_file_options } "
238
+ )
239
+ is_run_code_analyzer = True
240
+ else :
241
+ if not analysis_json_path_file .exists () or self .eager_analysis :
242
+ # If the analysis file does not exist, we'll run the analysis. Alternately, if the eager_analysis
243
+ # flag is set, we'll run the analysis every time the object is created. This will happen regradless
244
+ # of the existence of the analysis file.
245
+ # Create the executable command for codeanalyzer.
246
+ codeanalyzer_args = codeanalyzer_exec + shlex .split (
247
+ f"-i { Path (self .project_dir )} --analysis-level={ analysis_level } -o { self .analysis_json_path } "
248
+ )
249
+ is_run_code_analyzer = True
250
+
251
+ if is_run_code_analyzer :
228
252
try :
229
253
logger .info (f"Running codeanalyzer subprocess with args { codeanalyzer_args } " )
230
254
subprocess .run (
@@ -238,7 +262,6 @@ def _init_codeanalyzer(self, analysis_level=1) -> JApplication:
238
262
239
263
except Exception as e :
240
264
raise CodeanalyzerExecutionException (str (e )) from e
241
-
242
265
with open (analysis_json_path_file ) as f :
243
266
data = json .load (f )
244
267
return JApplication (** data )
@@ -252,7 +275,6 @@ def _codeanalyzer_single_file(self):
252
275
JApplication
253
276
The application view of the Java code with the analysis results.
254
277
"""
255
- # self.source_code: str = re.sub(r"[\r\n\t\f\v]+", lambda x: " " if x.group() in "\t\f\v" else " ", self.source_code)
256
278
codeanalyzer_exec = self ._get_codeanalyzer_exec ()
257
279
codeanalyzer_args = ["--source-analysis" , self .source_code ]
258
280
codeanalyzer_cmd = codeanalyzer_exec + codeanalyzer_args
0 commit comments