-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathjava.py
613 lines (520 loc) · 23.6 KB
/
java.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
from pathlib import Path
from typing import Dict, List, Tuple, Set
from networkx import DiGraph
from cldk.analysis import SymbolTable, CallGraph, AnalysisLevel
from cldk.models.java import JCallable
from cldk.models.java import JApplication
from cldk.models.java.models import JCompilationUnit, JMethodDetail, JType, JField
from cldk.analysis.java.codeanalyzer import JCodeanalyzer
from cldk.analysis.java.codeql import JCodeQL
from cldk.utils.analysis_engine import AnalysisEngine
class JavaAnalysis(SymbolTable, CallGraph):
def __init__(
self,
project_dir: str | Path | None,
source_code: str | None,
analysis_backend: str,
analysis_backend_path: str | None,
analysis_json_path: str | Path | None,
analysis_level: str,
use_graalvm_binary: bool,
eager_analysis: bool,
) -> None:
"""
Parameters
----------
project_dir : str
The directory path of the project.
analysis_backend : str, optional
The analysis_backend used for analysis, defaults to "codeql".
analysis_backend_path : str, optional
The path to the analysis_backend, defaults to None and in the case of codeql, it is assumed that the cli is installed and
available in the PATH. In the case of codeanalyzer the codeanalyzer.jar is downloaded from the lastest release.
analysis_json_path : str or Path, optional
The path save the to the analysis database (analysis.json), defaults to None. If None, the analysis database is
not persisted.
use_graalvm_binary : bool, optional
A flag indicating whether to use the GraalVM binary for SDG analysis, defaults to False. If False, the default
Java binary is used and one needs to have Java 17 or higher installed.
eager_analysis : bool, optional
A flag indicating whether to perform eager analysis, defaults to False. If True, the analysis is performed
eagerly. That is, the analysis.json file is created during analysis every time even if it already exists.
Attributes
----------
analysis_backend : JCodeQL | JApplication
The analysis_backend used for analysis.
application : JApplication
The application view of the Java code.
"""
self.project_dir = project_dir
self.source_code = source_code
self.analysis_level = analysis_level
self.analysis_json_path = analysis_json_path
self.analysis_backend_path = analysis_backend_path
self.eager_analysis = eager_analysis
self.use_graalvm_binary = use_graalvm_binary
self.analysis_backend = analysis_backend
# Initialize the analysis analysis_backend
if analysis_backend.lower() == "codeql":
self.analysis_backend: JCodeQL = JCodeQL(self.project_dir, self.analysis_json_path)
elif analysis_backend.lower() == "codeanalyzer":
self.backend: JCodeanalyzer = JCodeanalyzer(
project_dir=self.project_dir,
source_code=self.source_code,
eager_analysis=self.eager_analysis,
analysis_level=self.analysis_level,
analysis_json_path=self.analysis_json_path,
use_graalvm_binary=self.use_graalvm_binary,
analysis_backend_path=self.analysis_backend_path,
)
else:
raise NotImplementedError(f"Support for {analysis_backend} has not been implemented yet.")
def get_imports(self) -> List[str]:
raise NotImplementedError(f"Support for this functionality has not been implemented yet.")
def get_variables(self, **kwargs):
raise NotImplementedError(f"Support for this functionality has not been implemented yet.")
def get_service_entry_point_classes(self, **kwargs):
raise NotImplementedError(f"Support for this functionality has not been implemented yet.")
def get_service_entry_point_methods(self, **kwargs):
raise NotImplementedError(f"Support for this functionality has not been implemented yet.")
def get_application_view(self) -> JApplication:
"""
Returns the application view of the Java code.
Returns:
--------
JApplication
The application view of the Java code.
"""
if self.source_code:
raise NotImplementedError(f"Support for this functionality has not been implemented yet.")
return self.backend.get_application_view()
def get_symbol_table(self) -> Dict[str, JCompilationUnit]:
"""
Returns the symbol table of the Java code.
Returns:
--------
Dict[str, JCompilationUnit]
The application view of the Java code.
"""
return self.backend.get_symbol_table()
def get_compilation_units(self) -> List[JCompilationUnit]:
"""
Returns the compilation units of the Java code.
Returns
-------
Dict[str, JCompilationUnit]
The compilation units of the Java code.
"""
if self.analysis_backend in [AnalysisEngine.CODEQL, AnalysisEngine.TREESITTER]:
raise NotImplementedError(f"Support for this functionality has not been implemented yet.")
return self.backend.get_compilation_units()
def get_class_hierarchy(self) -> DiGraph:
"""
Returns the class hierarchy of the Java code.
Returns:
--------
DiGraph
The class hierarchy of the Java code.
"""
if self.backend in [AnalysisEngine.CODEQL, AnalysisEngine.TREESITTER]:
raise NotImplementedError(f"Support for this functionality has not been implemented yet.")
raise NotImplementedError("Class hierarchy is not implemented yet.")
def get_call_graph(self) -> DiGraph:
"""
Returns the call graph of the Java code.
Returns:
--------
DiGraph
The call graph of the Java code.
"""
return self.backend.get_call_graph()
def get_call_graph_json(self) -> str:
"""
serialize callgraph to json
"""
if self.source_code:
raise NotImplementedError("Producing a call graph over a single file is not implemented yet.")
return self.backend.get_call_graph_json()
def get_callers(self, target_class_name: str, target_method_declaration: str,
using_symbol_table: bool = False) -> Dict:
"""
Get all the caller details for a given java method.
Returns:
--------
Dict
Caller details in a dictionary.
"""
if self.source_code:
raise NotImplementedError("Generating all callers over a single file is not implemented yet.")
return self.backend.get_all_callers(target_class_name, target_method_declaration, using_symbol_table)
def get_callees(self, source_class_name: str, source_method_declaration: str):
"""
Get all the callee details for a given java method.
Returns:
--------
Dict
Callee details in a dictionary.
"""
if self.source_code:
raise NotImplementedError("Generating all callees over a single file is not implemented yet.")
return self.backend.get_all_callees(source_class_name, source_method_declaration)
def get_methods(self) -> Dict[str, Dict[str, JCallable]]:
"""
Returns a dictionary of all methods in the Java code with
qualified class name as key and dictionary of methods in that class
as value
Returns:
--------
Dict[str, Dict[str, JCallable]]:
A dictionary of dictionaries of all methods in the Java code.
"""
if self.analysis_backend in [AnalysisEngine.CODEQL, AnalysisEngine.TREESITTER]:
raise NotImplementedError(f"Support for this functionality has not been implemented yet.")
return self.backend.get_all_methods_in_application()
def get_classes(self) -> Dict[str, JType]:
"""
Returns a dictionary of all classes in the Java code.
Returns:
--------
Dict[str, JType]
A dict of all classes in the Java code, with qualified class names as keys
"""
if self.analysis_backend in [AnalysisEngine.CODEQL, AnalysisEngine.TREESITTER]:
raise NotImplementedError(f"Support for this functionality has not been implemented yet.")
return self.backend.get_all_classes()
def get_classes_by_criteria(self, inclusions=None, exclusions=None) -> Dict[str, JType]:
"""
Returns a dictionary of all classes in the Java code.
#TODO: Document the parameters inclusions and exclusions.
Returns:
--------
Dict[str, JType]
A dict of all classes in the Java code, with qualified class names as keys
"""
if self.analysis_backend in [AnalysisEngine.CODEQL, AnalysisEngine.TREESITTER]:
raise NotImplementedError(f"Support for this functionality has not been implemented yet.")
if exclusions is None:
exclusions = []
if inclusions is None:
inclusions = []
class_dict: Dict[str, JType] = {}
all_classes = self.backend.get_all_classes()
for application_class in all_classes:
is_selected = False
for inclusion in inclusions:
if inclusion in application_class:
is_selected = True
for exclusion in exclusions:
if exclusion in application_class:
is_selected = False
if is_selected:
class_dict[application_class] = all_classes[application_class]
return class_dict
def get_class(self, qualified_class_name) -> JType:
"""
Returns a class given qualified class name.
Parameters:
-----------
qualified_class_name : str
The qualified name of the class.
Returns:
--------
JType
A class for the given qualified class name.
"""
if self.analysis_backend in [AnalysisEngine.CODEQL, AnalysisEngine.TREESITTER]:
raise NotImplementedError(f"Support for this functionality has not been implemented yet.")
return self.backend.get_class(qualified_class_name)
def get_method(self, qualified_class_name, qualified_method_name) -> JCallable:
"""
Returns a method given qualified method name.
Parameters:
-----------
qualified_method_name : str
The qualified name of the method.
Returns:
--------
JCallable
A method for the given qualified method name.
"""
if self.analysis_backend in [AnalysisEngine.CODEQL, AnalysisEngine.TREESITTER]:
raise NotImplementedError(f"Support for this functionality has not been implemented yet.")
return self.backend.get_method(qualified_class_name, qualified_method_name)
def get_java_file(self, qualified_class_name) -> str:
"""
Returns a class given qualified class name.
Parameters:
-----------
qualified_class_name : str
The qualified name of the class.
Returns:
--------
str
Java file name containing the given qualified class.
"""
if self.analysis_backend in [AnalysisEngine.CODEQL, AnalysisEngine.TREESITTER]:
raise NotImplementedError(f"Support for this functionality has not been implemented yet.")
return self.backend.get_java_file(qualified_class_name)
def get_java_compilation_unit(self, file_path: str) -> JCompilationUnit:
"""
Given the path of a Java source file, returns the compilation unit object from the symbol table.
Parameters
----------
file_path : str
Absolute path to Java source file
Returns
-------
JCompilationUnit
Compilation unit object for Java source file
"""
if self.analysis_backend in [AnalysisEngine.CODEQL, AnalysisEngine.TREESITTER]:
raise NotImplementedError(f"Support for this functionality has not been implemented yet.")
return self.backend.get_java_compilation_unit(file_path)
def get_methods_in_class(self, qualified_class_name) -> Dict[str, JCallable]:
"""
Returns a dictionary of all methods in the given class.
Parameters:
-----------
qualified_class_name : str
The qualified name of the class.
Returns:
--------
Dict[str, JCallable]
A dictionary of all methods in the given class.
"""
if self.analysis_backend in [AnalysisEngine.CODEQL, AnalysisEngine.TREESITTER]:
raise NotImplementedError(f"Support for this functionality has not been implemented yet.")
return self.backend.get_all_methods_in_class(qualified_class_name)
def get_constructors(self, qualified_class_name) -> Dict[str, JCallable]:
"""
Returns a dictionary of all constructors of the given class.
Parameters:
-----------
qualified_class_name : str
The qualified name of the class.
Returns:
--------
Dict[str, JCallable]
A dictionary of all constructors of the given class.
"""
if self.analysis_backend in [AnalysisEngine.CODEQL, AnalysisEngine.TREESITTER]:
raise NotImplementedError(f"Support for this functionality has not been implemented yet.")
return self.backend.get_all_constructors(qualified_class_name)
def get_fields(self, qualified_class_name) -> List[JField]:
"""
Returns a list of all fields of the given class.
Parameters:
-----------
qualified_class_name : str
The qualified name of the class.
Returns:
--------
List[JField]
A list of all fields of the given class.
"""
if self.analysis_backend in [AnalysisEngine.CODEQL, AnalysisEngine.TREESITTER]:
raise NotImplementedError(f"Support for this functionality has not been implemented yet.")
return self.backend.get_all_fields(qualified_class_name)
def get_nested_classes(self, qualified_class_name) -> List[JType]:
"""
Returns a list of all nested classes for the given class.
Parameters:
-----------
qualified_class_name : str
The qualified name of the class.
Returns:
--------
List[JType]
A list of nested classes for the given class.
"""
if self.analysis_backend in [AnalysisEngine.CODEQL, AnalysisEngine.TREESITTER]:
raise NotImplementedError(f"Support for this functionality has not been implemented yet.")
return self.backend.get_all_nested_classes(qualified_class_name)
def get_sub_classes(self, qualified_class_name) -> Dict[str, JType]:
"""
Returns a dictionary of all sub-classes of the given class
Parameters
----------
qualified_class_name
Returns
-------
Dict[str, JType]: A dictionary of all sub-classes of the given class, and class details
"""
return self.backend.get_all_sub_classes(qualified_class_name=qualified_class_name)
def get_extended_classes(self, qualified_class_name) -> List[str]:
"""
Returns a list of all extended classes for the given class.
Parameters:
-----------
qualified_class_name : str
The qualified name of the class.
Returns:
--------
List[JType]
A list of extended classes for the given class.
"""
if self.analysis_backend in [AnalysisEngine.CODEQL, AnalysisEngine.TREESITTER]:
raise NotImplementedError(f"Support for this functionality has not been implemented yet.")
return self.backend.get_extended_classes(qualified_class_name)
def get_implemented_interfaces(self, qualified_class_name) -> List[str]:
"""
Returns a list of all implemented interfaces for the given class.
Parameters:
-----------
qualified_class_name : str
The qualified name of the class.
Returns:
--------
List[JType]
A list of implemented interfaces for the given class.
"""
if self.analysis_backend in [AnalysisEngine.CODEQL, AnalysisEngine.TREESITTER]:
raise NotImplementedError(f"Support for this functionality has not been implemented yet.")
return self.backend.get_implemented_interfaces(qualified_class_name)
def __get_class_call_graph_using_symbol_table(self, qualified_class_name: str, method_signature: str | None = None) -> (List)[Tuple[JMethodDetail, JMethodDetail]]:
"""
A call graph using symbol table for a given class and a given method.
Args:
qualified_class_name:
method_signature:
Returns:
List[Tuple[JMethodDetail, JMethodDetail]]
An edge list of the call graph for the given class and method.
"""
if self.analysis_backend in [AnalysisEngine.CODEQL, AnalysisEngine.TREESITTER]:
raise NotImplementedError(f"Support for this functionality has not been implemented yet.")
return self.backend.get_class_call_graph_using_symbol_table(qualified_class_name, method_signature)
def get_class_call_graph(self, qualified_class_name: str, method_signature: str | None = None,
using_symbol_table: bool = False) -> List[Tuple[JMethodDetail, JMethodDetail]]:
"""
A call graph for a given class and (optionally) a given method.
Parameters
----------
using_symbol_table: bool
Generate call graph using symbol table
qualified_class_name : str
The qualified name of the class.
method_name : str, optional
The signature of the method in the class.
Returns
-------
List[Tuple[JMethodDetail, JMethodDetail]]
An edge list of the call graph for the given class and method.
Args:
using_symbol_table:
using_symbol_table:
"""
if using_symbol_table:
return self.__get_class_call_graph_using_symbol_table(qualified_class_name=qualified_class_name,
method_signature=method_signature)
if self.analysis_backend in [AnalysisEngine.CODEQL, AnalysisEngine.TREESITTER]:
raise NotImplementedError(f"Support for this functionality has not been implemented yet.")
return self.backend.get_class_call_graph(qualified_class_name, method_signature)
def get_entry_point_classes(self) -> Dict[str, JType]:
"""
Returns a dictionary of all entry point classes in the Java code.
Returns:
--------
Dict[str, JType]
A dict of all entry point classes in the Java code, with qualified class names as keys
"""
if self.analysis_backend in [AnalysisEngine.CODEQL, AnalysisEngine.TREESITTER]:
raise NotImplementedError(f"Support for this functionality has not been implemented yet.")
return self.backend.get_all_entry_point_classes()
def get_entry_point_methods(self) -> Dict[str, Dict[str, JCallable]]:
"""
Returns a dictionary of all entry point methods in the Java code with
qualified class name as key and dictionary of methods in that class
as value
Returns:
--------
Dict[str, Dict[str, JCallable]]:
A dictionary of dictionaries of entry point methods in the Java code.
"""
if self.analysis_backend in [AnalysisEngine.CODEQL, AnalysisEngine.TREESITTER]:
raise NotImplementedError(f"Support for this functionality has not been implemented yet.")
return self.backend.get_all_entry_point_methods()
def remove_all_comments(self) -> str:
"""
Remove all comments from the source code.
Parameters
----------
source_code : str
The source code to process.
Returns
-------
str
The source code with all comments removed.
"""
# Remove any prefix comments/content before the package declaration
if self.analysis_backend in [AnalysisEngine.CODEQL, AnalysisEngine.CODEANALYZER]:
raise NotImplementedError(f"Support for this functionality has not been implemented yet.")
return self.backend.remove_all_comments(self.source_code)
def get_methods_with_annotations(self, annotations: List[str]) -> Dict[str, List[Dict]]:
"""
Returns a dictionary of method names and method bodies.
Parameters:
-----------
source_class_code : str
String containing code for a java class.
annotations : List[str]
List of annotation strings.
Returns:
--------
Dict[str,List[Dict]]
Dictionary with annotations as keys and
a list of dictionaries containing method names and bodies, as values.
"""
if self.analysis_backend in [AnalysisEngine.CODEQL, AnalysisEngine.CODEANALYZER]:
raise NotImplementedError(f"Support for this functionality has not been implemented yet.")
return self.backend.get_methods_with_annotations(self.source_code, annotations)
def get_test_methods(self, source_class_code: str) -> Dict[str, str]:
"""
Returns a dictionary of method names and method bodies.
Parameters:
-----------
source_class_code : str
String containing code for a java class.
Returns:
--------
Dict[str,str]
Dictionary of method names and method bodies.
"""
if self.analysis_backend in [AnalysisEngine.CODEQL, AnalysisEngine.CODEANALYZER]:
raise NotImplementedError(f"Support for this functionality has not been implemented yet.")
return self.backend.get_test_methods(self.source_code)
def get_calling_lines(self, target_method_name: str) -> List[int]:
"""
Returns a list of line numbers in source method where target method is called.
Parameters:
-----------
source_method_code : str
source method code
target_method_code : str
target method code
Returns:
--------
List[int]
List of line numbers within in source method code block.
"""
if self.analysis_backend in [AnalysisEngine.CODEQL, AnalysisEngine.TREESITTER]:
raise NotImplementedError(f"Support for this functionality has not been implemented yet.")
return self.backend.get_calling_lines(self.source_code, target_method_name)
def get_call_targets(self, declared_methods: dict) -> Set[str]:
"""Generate a list of call targets from the method body.
Uses simple name resolution for finding the call targets. Nothing sophiscticed here. Just a simple search
over the AST.
Parameters
----------
method_body : Node
The method body.
declared_methods : dict
A dictionary of all declared methods in the class.
Returns
-------
List[str]
A list of call targets (methods).
"""
if self.analysis_backend in [AnalysisEngine.CODEQL, AnalysisEngine.TREESITTER]:
raise NotImplementedError(f"Support for this functionality has not been implemented yet.")
return self.backend.get_call_targets(self.source_code, declared_methods)