Skip to content

Commit c8777ad

Browse files
Rangeet PanRangeet Pan
authored andcommitted
fixing caller method
1 parent 0640b84 commit c8777ad

File tree

1 file changed

+33
-11
lines changed

1 file changed

+33
-11
lines changed

cldk/analysis/java/codeanalyzer/codeanalyzer.py

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ def __init__(
5959
analysis_level: str,
6060
use_graalvm_binary: bool,
6161
eager_analysis: bool,
62+
target_files: List[str] | None
6263
) -> None:
6364
self.project_dir = project_dir
6465
self.source_code = source_code
@@ -67,6 +68,7 @@ def __init__(
6768
self.use_graalvm_binary = use_graalvm_binary
6869
self.eager_analysis = eager_analysis
6970
self.analysis_level = analysis_level
71+
self.target_files = target_files
7072
self.application = self._init_codeanalyzer(
7173
analysis_level=1 if analysis_level == AnalysisLevel.symbol_table else 2)
7274
# Attributes related the Java code analysis...
@@ -198,11 +200,19 @@ def _init_codeanalyzer(self, analysis_level=1) -> JApplication:
198200
"""
199201

200202
codeanalyzer_exec = self._get_codeanalyzer_exec()
201-
203+
codeanalyzer_args = ''
202204
if self.analysis_json_path is None:
203205
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+
)
206216
try:
207217
logger.info(f"Running codeanalyzer: {' '.join(codeanalyzer_args)}")
208218
console_out: CompletedProcess[str] = subprocess.run(
@@ -216,15 +226,29 @@ def _init_codeanalyzer(self, analysis_level=1) -> JApplication:
216226
raise CodeanalyzerExecutionException(str(e)) from e
217227

218228
else:
229+
# Check if the code analyzer needs to be run
230+
is_run_code_analyzer = False
219231
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])
225235
codeanalyzer_args = codeanalyzer_exec + shlex.split(
226-
f"-i {Path(self.project_dir)} --analysis-level={analysis_level} -o {self.analysis_json_path}")
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
227250

251+
if is_run_code_analyzer:
228252
try:
229253
logger.info(f"Running codeanalyzer subprocess with args {codeanalyzer_args}")
230254
subprocess.run(
@@ -238,7 +262,6 @@ def _init_codeanalyzer(self, analysis_level=1) -> JApplication:
238262

239263
except Exception as e:
240264
raise CodeanalyzerExecutionException(str(e)) from e
241-
242265
with open(analysis_json_path_file) as f:
243266
data = json.load(f)
244267
return JApplication(**data)
@@ -252,7 +275,6 @@ def _codeanalyzer_single_file(self):
252275
JApplication
253276
The application view of the Java code with the analysis results.
254277
"""
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)
256278
codeanalyzer_exec = self._get_codeanalyzer_exec()
257279
codeanalyzer_args = ["--source-analysis", self.source_code]
258280
codeanalyzer_cmd = codeanalyzer_exec + codeanalyzer_args

0 commit comments

Comments
 (0)