Skip to content

Commit ab5592e

Browse files
committed
Implemented 36 test cases for JavaAnalyzer
Signed-off-by: John Rofrano <johnnyroy@johnrofrano.com>
1 parent f8f0cca commit ab5592e

4 files changed

Lines changed: 1332 additions & 72 deletions

File tree

cldk/analysis/java/java.py

Lines changed: 42 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@
1919
"""
2020

2121
from pathlib import Path
22-
2322
from typing import Dict, List, Tuple, Set
2423
from networkx import DiGraph
24+
from tree_sitter import Tree
2525

2626
from cldk.analysis import SymbolTable, CallGraph, AnalysisLevel
2727
from cldk.analysis.java.treesitter import JavaSitter
@@ -102,31 +102,31 @@ def get_imports(self) -> List[str]:
102102
Returns:
103103
List[str]: List of all the imports.
104104
"""
105-
raise NotImplementedError(f"Support for this functionality has not been implemented yet.")
105+
raise NotImplementedError("Support for this functionality has not been implemented yet.")
106106

107107
def get_variables(self, **kwargs):
108108
"""_Returns all the variables.
109109
110110
Raises:
111111
NotImplementedError: Raised when this functionality is not suported.
112112
"""
113-
raise NotImplementedError(f"Support for this functionality has not been implemented yet.")
113+
raise NotImplementedError("Support for this functionality has not been implemented yet.")
114114

115115
def get_service_entry_point_classes(self, **kwargs):
116116
"""Returns all service entry point classes.
117117
118118
Raises:
119119
NotImplementedError: Raised when this functionality is not suported.
120120
"""
121-
raise NotImplementedError(f"Support for this functionality has not been implemented yet.")
121+
raise NotImplementedError("Support for this functionality has not been implemented yet.")
122122

123123
def get_service_entry_point_methods(self, **kwargs):
124124
"""Returns all the service entry point methods.
125125
126126
Raises:
127127
NotImplementedError: Raised when this functionality is not suported.
128128
"""
129-
raise NotImplementedError(f"Support for this functionality has not been implemented yet.")
129+
raise NotImplementedError("Support for this functionality has not been implemented yet.")
130130

131131
def get_application_view(self) -> JApplication:
132132
"""Returns application view of the java code.
@@ -138,7 +138,7 @@ def get_application_view(self) -> JApplication:
138138
JApplication: Application view of the java code.
139139
"""
140140
if self.source_code:
141-
raise NotImplementedError(f"Support for this functionality has not been implemented yet.")
141+
raise NotImplementedError("Support for this functionality has not been implemented yet.")
142142
return self.backend.get_application_view()
143143

144144
def get_symbol_table(self) -> Dict[str, JCompilationUnit]:
@@ -153,14 +153,20 @@ def get_compilation_units(self) -> List[JCompilationUnit]:
153153
"""Returns a list of all compilation units in the java code.
154154
155155
Raises:
156-
NotImplementedError: Raised when this functionality is not suported.
156+
NotImplementedError: Raised when this functionality is not supported.
157157
158158
Returns:
159159
List[JCompilationUnit]: Compilation units of the Java code.
160160
"""
161-
if self.analysis_backend in [AnalysisEngine.CODEQL, AnalysisEngine.TREESITTER]:
162-
raise NotImplementedError(f"Support for this functionality has not been implemented yet.")
163-
return self.backend.get_compilation_units()
161+
162+
# TODO: This code is broken:
163+
# JCodeanalyzer does not have a get_compilation_units() method
164+
# Commenting out until implemented
165+
166+
# if self.analysis_backend in [AnalysisEngine.CODEQL, AnalysisEngine.TREESITTER]:
167+
# raise NotImplementedError("Support for this functionality has not been implemented yet.")
168+
# return self.backend.get_compilation_units()
169+
raise NotImplementedError("Support for this functionality has not been implemented yet.")
164170

165171
def get_class_hierarchy(self) -> DiGraph:
166172
"""Returns class hierarchy of the java code.
@@ -173,7 +179,7 @@ def get_class_hierarchy(self) -> DiGraph:
173179
"""
174180

175181
if self.backend in [AnalysisEngine.CODEQL, AnalysisEngine.TREESITTER]:
176-
raise NotImplementedError(f"Support for this functionality has not been implemented yet.")
182+
raise NotImplementedError("Support for this functionality has not been implemented yet.")
177183
raise NotImplementedError("Class hierarchy is not implemented yet.")
178184

179185
def is_parsable(self, source_code: str) -> bool:
@@ -185,9 +191,9 @@ def is_parsable(self, source_code: str) -> bool:
185191
Returns:
186192
True if the code is parsable, False otherwise
187193
"""
188-
return JavaSitter.is_parsable(self, source_code)
194+
return JavaSitter().is_parsable(source_code)
189195

190-
def get_raw_ast(self, source_code: str) -> str:
196+
def get_raw_ast(self, source_code: str) -> Tree:
191197
"""
192198
Get the raw AST
193199
Args:
@@ -196,7 +202,7 @@ def get_raw_ast(self, source_code: str) -> str:
196202
Returns:
197203
Tree: the raw AST
198204
"""
199-
return JavaSitter.get_raw_ast(self, source_code)
205+
return JavaSitter().get_raw_ast(source_code)
200206

201207
def get_call_graph(self) -> DiGraph:
202208
"""Returns the call graph of the Java code.
@@ -266,7 +272,7 @@ def get_methods(self) -> Dict[str, Dict[str, JCallable]]:
266272
Dict[str, Dict[str, JCallable]]: Dictionary of dictionaries of all methods in the Java code with qualified class name as key and dictionary of methods in that class.
267273
"""
268274
if self.analysis_backend in [AnalysisEngine.CODEQL, AnalysisEngine.TREESITTER]:
269-
raise NotImplementedError(f"Support for this functionality has not been implemented yet.")
275+
raise NotImplementedError("Support for this functionality has not been implemented yet.")
270276
return self.backend.get_all_methods_in_application()
271277

272278
def get_classes(self) -> Dict[str, JType]:
@@ -279,7 +285,7 @@ def get_classes(self) -> Dict[str, JType]:
279285
Dict[str, JType]: A dictionary of all classes in the Java code, with qualified class names as keys.
280286
"""
281287
if self.analysis_backend in [AnalysisEngine.CODEQL, AnalysisEngine.TREESITTER]:
282-
raise NotImplementedError(f"Support for this functionality has not been implemented yet.")
288+
raise NotImplementedError("Support for this functionality has not been implemented yet.")
283289
return self.backend.get_all_classes()
284290

285291
def get_classes_by_criteria(self, inclusions=None, exclusions=None) -> Dict[str, JType]:
@@ -297,7 +303,7 @@ def get_classes_by_criteria(self, inclusions=None, exclusions=None) -> Dict[str,
297303
"""
298304

299305
if self.analysis_backend in [AnalysisEngine.CODEQL, AnalysisEngine.TREESITTER]:
300-
raise NotImplementedError(f"Support for this functionality has not been implemented yet.")
306+
raise NotImplementedError("Support for this functionality has not been implemented yet.")
301307

302308
if exclusions is None:
303309
exclusions = []
@@ -332,7 +338,7 @@ def get_class(self, qualified_class_name: str) -> JType:
332338
"""
333339

334340
if self.analysis_backend in [AnalysisEngine.CODEQL, AnalysisEngine.TREESITTER]:
335-
raise NotImplementedError(f"Support for this functionality has not been implemented yet.")
341+
raise NotImplementedError("Support for this functionality has not been implemented yet.")
336342
return self.backend.get_class(qualified_class_name)
337343

338344
def get_method(self, qualified_class_name: str, qualified_method_name: str) -> JCallable:
@@ -349,7 +355,7 @@ def get_method(self, qualified_class_name: str, qualified_method_name: str) -> J
349355
JCallable: A method for the given qualified method name.
350356
"""
351357
if self.analysis_backend in [AnalysisEngine.CODEQL, AnalysisEngine.TREESITTER]:
352-
raise NotImplementedError(f"Support for this functionality has not been implemented yet.")
358+
raise NotImplementedError("Support for this functionality has not been implemented yet.")
353359
return self.backend.get_method(qualified_class_name, qualified_method_name)
354360

355361
def get_java_file(self, qualified_class_name: str) -> str:
@@ -365,7 +371,7 @@ def get_java_file(self, qualified_class_name: str) -> str:
365371
str: Java file name containing the given qualified class.
366372
"""
367373
if self.analysis_backend in [AnalysisEngine.CODEQL, AnalysisEngine.TREESITTER]:
368-
raise NotImplementedError(f"Support for this functionality has not been implemented yet.")
374+
raise NotImplementedError("Support for this functionality has not been implemented yet.")
369375
return self.backend.get_java_file(qualified_class_name)
370376

371377
def get_java_compilation_unit(self, file_path: str) -> JCompilationUnit:
@@ -381,7 +387,7 @@ def get_java_compilation_unit(self, file_path: str) -> JCompilationUnit:
381387
JCompilationUnit: Compilation unit object for Java source file
382388
"""
383389
if self.analysis_backend in [AnalysisEngine.CODEQL, AnalysisEngine.TREESITTER]:
384-
raise NotImplementedError(f"Support for this functionality has not been implemented yet.")
390+
raise NotImplementedError("Support for this functionality has not been implemented yet.")
385391
return self.backend.get_java_compilation_unit(file_path)
386392

387393
def get_methods_in_class(self, qualified_class_name) -> Dict[str, JCallable]:
@@ -397,7 +403,7 @@ def get_methods_in_class(self, qualified_class_name) -> Dict[str, JCallable]:
397403
Dict[str, JCallable]: A dictionary of all constructors of the given class.
398404
"""
399405
if self.analysis_backend in [AnalysisEngine.CODEQL, AnalysisEngine.TREESITTER]:
400-
raise NotImplementedError(f"Support for this functionality has not been implemented yet.")
406+
raise NotImplementedError("Support for this functionality has not been implemented yet.")
401407
return self.backend.get_all_methods_in_class(qualified_class_name)
402408

403409
def get_constructors(self, qualified_class_name) -> Dict[str, JCallable]:
@@ -413,7 +419,7 @@ def get_constructors(self, qualified_class_name) -> Dict[str, JCallable]:
413419
Dict[str, JCallable]: A dictionary of all constructors of the given class.
414420
"""
415421
if self.analysis_backend in [AnalysisEngine.CODEQL, AnalysisEngine.TREESITTER]:
416-
raise NotImplementedError(f"Support for this functionality has not been implemented yet.")
422+
raise NotImplementedError("Support for this functionality has not been implemented yet.")
417423
return self.backend.get_all_constructors(qualified_class_name)
418424

419425
def get_fields(self, qualified_class_name) -> List[JField]:
@@ -429,7 +435,7 @@ def get_fields(self, qualified_class_name) -> List[JField]:
429435
List[JField]: A list of all fields of the given class.
430436
"""
431437
if self.analysis_backend in [AnalysisEngine.CODEQL, AnalysisEngine.TREESITTER]:
432-
raise NotImplementedError(f"Support for this functionality has not been implemented yet.")
438+
raise NotImplementedError("Support for this functionality has not been implemented yet.")
433439
return self.backend.get_all_fields(qualified_class_name)
434440

435441
def get_nested_classes(self, qualified_class_name) -> List[JType]:
@@ -445,7 +451,7 @@ def get_nested_classes(self, qualified_class_name) -> List[JType]:
445451
List[JType]: A list of nested classes for the given class.
446452
"""
447453
if self.analysis_backend in [AnalysisEngine.CODEQL, AnalysisEngine.TREESITTER]:
448-
raise NotImplementedError(f"Support for this functionality has not been implemented yet.")
454+
raise NotImplementedError("Support for this functionality has not been implemented yet.")
449455
return self.backend.get_all_nested_classes(qualified_class_name)
450456

451457
def get_sub_classes(self, qualified_class_name) -> Dict[str, JType]:
@@ -471,7 +477,7 @@ def get_extended_classes(self, qualified_class_name) -> List[str]:
471477
List[str]: A list of extended classes for the given class.
472478
"""
473479
if self.analysis_backend in [AnalysisEngine.CODEQL, AnalysisEngine.TREESITTER]:
474-
raise NotImplementedError(f"Support for this functionality has not been implemented yet.")
480+
raise NotImplementedError("Support for this functionality has not been implemented yet.")
475481
return self.backend.get_extended_classes(qualified_class_name)
476482

477483
def get_implemented_interfaces(self, qualified_class_name: str) -> List[str]:
@@ -487,7 +493,7 @@ def get_implemented_interfaces(self, qualified_class_name: str) -> List[str]:
487493
List[str]: A list of implemented interfaces for the given class.
488494
"""
489495
if self.analysis_backend in [AnalysisEngine.CODEQL, AnalysisEngine.TREESITTER]:
490-
raise NotImplementedError(f"Support for this functionality has not been implemented yet.")
496+
raise NotImplementedError("Support for this functionality has not been implemented yet.")
491497
return self.backend.get_implemented_interfaces(qualified_class_name)
492498

493499
def __get_class_call_graph_using_symbol_table(self, qualified_class_name: str, method_signature: str | None = None) -> (List)[Tuple[JMethodDetail, JMethodDetail]]:
@@ -504,7 +510,7 @@ def __get_class_call_graph_using_symbol_table(self, qualified_class_name: str, m
504510
List[Tuple[JMethodDetail, JMethodDetail]]: An edge list of the call graph for the given class and method.
505511
"""
506512
if self.analysis_backend in [AnalysisEngine.CODEQL, AnalysisEngine.TREESITTER]:
507-
raise NotImplementedError(f"Support for this functionality has not been implemented yet.")
513+
raise NotImplementedError("Support for this functionality has not been implemented yet.")
508514
return self.backend.get_class_call_graph_using_symbol_table(qualified_class_name, method_signature)
509515

510516
def get_class_call_graph(self, qualified_class_name: str, method_signature: str | None = None, using_symbol_table: bool = False) -> List[Tuple[JMethodDetail, JMethodDetail]]:
@@ -524,7 +530,7 @@ def get_class_call_graph(self, qualified_class_name: str, method_signature: str
524530
if using_symbol_table:
525531
return self.__get_class_call_graph_using_symbol_table(qualified_class_name=qualified_class_name, method_signature=method_signature)
526532
if self.analysis_backend in [AnalysisEngine.CODEQL, AnalysisEngine.TREESITTER]:
527-
raise NotImplementedError(f"Support for this functionality has not been implemented yet.")
533+
raise NotImplementedError("Support for this functionality has not been implemented yet.")
528534
return self.backend.get_class_call_graph(qualified_class_name, method_signature)
529535

530536
def get_entry_point_classes(self) -> Dict[str, JType]:
@@ -537,7 +543,7 @@ def get_entry_point_classes(self) -> Dict[str, JType]:
537543
Dict[str, JType]: A dict of all entry point classes in the Java code, with qualified class names as keys
538544
"""
539545
if self.analysis_backend in [AnalysisEngine.CODEQL, AnalysisEngine.TREESITTER]:
540-
raise NotImplementedError(f"Support for this functionality has not been implemented yet.")
546+
raise NotImplementedError("Support for this functionality has not been implemented yet.")
541547
return self.backend.get_all_entry_point_classes()
542548

543549
def get_entry_point_methods(self) -> Dict[str, Dict[str, JCallable]]:
@@ -550,7 +556,7 @@ def get_entry_point_methods(self) -> Dict[str, Dict[str, JCallable]]:
550556
Dict[str, Dict[str, JCallable]]: A dictionary of dictionaries of entry point methods in the Java code.
551557
"""
552558
if self.analysis_backend in [AnalysisEngine.CODEQL, AnalysisEngine.TREESITTER]:
553-
raise NotImplementedError(f"Support for this functionality has not been implemented yet.")
559+
raise NotImplementedError("Support for this functionality has not been implemented yet.")
554560
return self.backend.get_all_entry_point_methods()
555561

556562
def remove_all_comments(self) -> str:
@@ -564,7 +570,7 @@ def remove_all_comments(self) -> str:
564570
"""
565571
# Remove any prefix comments/content before the package declaration
566572
if self.analysis_backend in [AnalysisEngine.CODEQL, AnalysisEngine.CODEANALYZER]:
567-
raise NotImplementedError(f"Support for this functionality has not been implemented yet.")
573+
raise NotImplementedError("Support for this functionality has not been implemented yet.")
568574
return self.backend.remove_all_comments(self.source_code)
569575

570576
def get_methods_with_annotations(self, annotations: List[str]) -> Dict[str, List[Dict]]:
@@ -580,7 +586,7 @@ def get_methods_with_annotations(self, annotations: List[str]) -> Dict[str, List
580586
Dict[str, List[Dict]]: Dictionary with annotations as keys and a list of dictionaries containing method names and bodies, as values.
581587
"""
582588
if self.analysis_backend in [AnalysisEngine.CODEQL, AnalysisEngine.CODEANALYZER]:
583-
raise NotImplementedError(f"Support for this functionality has not been implemented yet.")
589+
raise NotImplementedError("Support for this functionality has not been implemented yet.")
584590
return self.backend.get_methods_with_annotations(self.source_code, annotations)
585591

586592
def get_test_methods(self) -> Dict[str, str]:
@@ -597,7 +603,7 @@ def get_test_methods(self) -> Dict[str, str]:
597603
"""
598604

599605
if self.analysis_backend in [AnalysisEngine.CODEQL, AnalysisEngine.CODEANALYZER]:
600-
raise NotImplementedError(f"Support for this functionality has not been implemented yet.")
606+
raise NotImplementedError("Support for this functionality has not been implemented yet.")
601607
return self.backend.get_test_methods(self.source_code)
602608

603609
def get_calling_lines(self, target_method_name: str) -> List[int]:
@@ -614,7 +620,7 @@ def get_calling_lines(self, target_method_name: str) -> List[int]:
614620
"""
615621

616622
if self.analysis_backend in [AnalysisEngine.CODEQL, AnalysisEngine.TREESITTER]:
617-
raise NotImplementedError(f"Support for this functionality has not been implemented yet.")
623+
raise NotImplementedError("Support for this functionality has not been implemented yet.")
618624
return self.backend.get_calling_lines(self.source_code, target_method_name)
619625

620626
def get_call_targets(self, declared_methods: dict) -> Set[str]:
@@ -630,5 +636,5 @@ def get_call_targets(self, declared_methods: dict) -> Set[str]:
630636
Set[str]: A list of call targets (methods).
631637
"""
632638
if self.analysis_backend in [AnalysisEngine.CODEQL, AnalysisEngine.TREESITTER]:
633-
raise NotImplementedError(f"Support for this functionality has not been implemented yet.")
639+
raise NotImplementedError("Support for this functionality has not been implemented yet.")
634640
return self.backend.get_call_targets(self.source_code, declared_methods)

cldk/analysis/java/treesitter/javasitter.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,17 @@
1717
"""
1818
JavaSitter module
1919
"""
20+
import logging
2021
from itertools import groupby
2122
from typing import List, Set, Dict
2223
from tree_sitter import Language, Node, Parser, Query, Tree
2324
import tree_sitter_java as tsjava
24-
from tree_sitter import Language, Node, Parser, Query
25-
2625
from cldk.models.treesitter import Captures
2726

28-
import logging
29-
3027
logger = logging.getLogger(__name__)
3128

3229

30+
# pylint: disable=too-many-public-methods
3331
class JavaSitter:
3432
"""
3533
Treesitter for Java usecases.
@@ -75,7 +73,7 @@ def syntax_error(node):
7573
for child in node.children:
7674
if syntax_error(child):
7775
return True
78-
except RecursionError as err:
76+
except RecursionError:
7977
return True
8078

8179
return False

0 commit comments

Comments
 (0)