Skip to content

Commit ca21be1

Browse files
committed
Support optional fields in JType to accept null values.
Signed-off-by: Rahul Krishna <i.m.ralk@gmail.com>
1 parent ef3be3a commit ca21be1

3 files changed

Lines changed: 37 additions & 6 deletions

File tree

cldk/analysis/java/codeanalyzer/codeanalyzer.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,9 @@ def _init_japplication(data: str) -> JApplication:
160160
JApplication
161161
The application view of the Java code with the analysis results.
162162
"""
163+
# from ipdb import set_trace
164+
165+
# set_trace()
163166
return JApplication(**json.loads(data))
164167

165168
def _init_codeanalyzer(self, analysis_level=1) -> JApplication:
@@ -231,7 +234,7 @@ def _init_codeanalyzer(self, analysis_level=1) -> JApplication:
231234
raise CodeanalyzerExecutionException(str(e)) from e
232235
with open(analysis_json_path_file) as f:
233236
data = json.load(f)
234-
return JApplication(**data)
237+
return self._init_japplication(json.dumps(data))
235238

236239
def _codeanalyzer_single_file(self) -> JApplication:
237240
"""Invokes codeanalyzer in a single file mode.

cldk/models/java/models.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -260,13 +260,13 @@ class JType(BaseModel):
260260
is_concrete_class: bool = False
261261
is_entry_point: bool = False
262262
comment: str
263-
extends_list: List[str] = []
264-
implements_list: List[str] = []
265-
modifiers: List[str] = []
266-
annotations: List[str] = []
263+
extends_list: List[str] | None = []
264+
implements_list: List[str] | None = []
265+
modifiers: List[str] | None = []
266+
annotations: List[str] | None = []
267267
parent_type: str
268268
is_entrypoint_class: bool = False
269-
nested_type_declerations: List[str] = []
269+
nested_type_declerations: List[str] | None = []
270270
callable_declarations: Dict[str, JCallable] = {}
271271
field_declarations: List[JField] = []
272272
enum_constants: List[JEnumConstant] | None = []
@@ -359,6 +359,8 @@ def validate_source(cls, value) -> JMethodDetail:
359359
accessed_fields=[],
360360
call_sites=[],
361361
variable_declarations=[],
362+
crud_operations=[],
363+
crud_queries=[],
362364
cyclomatic_complexity=0,
363365
),
364366
)

tests/analysis/java/test_jcodeanalyzer.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import os
2222
import json
23+
from pdb import set_trace
2324
from typing import Dict, List, Tuple
2425
from unittest.mock import patch, MagicMock
2526
import networkx as nx
@@ -992,3 +993,28 @@ def test_get_all_get_crud_delete_operations(test_fixture_pbw, codeanalyzer_jar_p
992993
assert isinstance(crud_op, JCRUDOperation)
993994
assert crud_op.line_number > 0
994995
assert crud_op.operation_type.value == "DELETE"
996+
997+
998+
def test_get_all_get_crud_operations_daytrader8(test_fixture, codeanalyzer_jar_path):
999+
"""It should return all of the CRUD operations in an application"""
1000+
code_analyzer = JCodeanalyzer(
1001+
project_dir=test_fixture,
1002+
source_code=None,
1003+
analysis_backend_path=codeanalyzer_jar_path,
1004+
analysis_json_path=test_fixture / "build",
1005+
analysis_level=AnalysisLevel.symbol_table,
1006+
use_graalvm_binary=False,
1007+
eager_analysis=True,
1008+
target_files=None,
1009+
)
1010+
crud_operations = code_analyzer.get_all_crud_operations()
1011+
assert crud_operations is not None
1012+
for operation in crud_operations:
1013+
assert operation is not None
1014+
assert isinstance(operation, Dict)
1015+
assert isinstance(operation["crud_operations"], list)
1016+
for crud_op in operation["crud_operations"]:
1017+
assert crud_op is not None
1018+
assert isinstance(crud_op, JCRUDOperation)
1019+
assert crud_op.line_number > 0
1020+
assert crud_op.operation_type.value in ["CREATE", "READ", "UPDATE", "DELETE"]

0 commit comments

Comments
 (0)