Repository navigation
Expand file tree
/
Copy pathTransactionalMethodCheckedExceptionCheckSample.java
More file actions
616 lines (500 loc) · 20.5 KB
/
Copy pathTransactionalMethodCheckedExceptionCheckSample.java
File metadata and controls
616 lines (500 loc) · 20.5 KB
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
614
615
616
package checks.spring;
import java.io.IOException;
import java.sql.SQLException;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import static org.springframework.transaction.annotation.Propagation.NOT_SUPPORTED;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
// Composed/meta-annotation for testing
@Retention(RetentionPolicy.RUNTIME)
@Transactional
@interface MyTransactional {
}
class Order {}
class NotificationException extends Exception {}
class CustomCheckedException extends Exception {}
public class TransactionalMethodCheckedExceptionCheckSample {
@Transactional
public void processOrder(Order order) throws IOException, SQLException { // Noncompliant [[secondary=24;quickfixes=qf1,qf2]]
// ^^^^^^^^^^^^
// fix@qf1 {{Add rollbackFor attribute}}
// edit@qf1 [[sl=-1;sc=3;el=-1;ec=17]] {{@Transactional(rollbackFor = {java.io.IOException.class, java.sql.SQLException.class})}}
// fix@qf2 {{Add rollbackFor = Exception.class}}
// edit@qf2 [[sl=-1;sc=3;el=-1;ec=17]] {{@Transactional(rollbackFor = java.lang.Exception.class)}}
}
@Transactional
public void importData() throws Exception { // Noncompliant [[secondary=33;quickfixes=qf3]]
// ^^^^^^^^^^
// fix@qf3 {{Add rollbackFor = Exception.class}}
// edit@qf3 [[sl=-1;sc=3;el=-1;ec=17]] {{@Transactional(rollbackFor = java.lang.Exception.class)}}
}
@Transactional(timeout = 30)
public void withOtherAttributes() throws SQLException { // Noncompliant [[secondary=40]]
// ^^^^^^^^^^^^^^^^^^^
}
@Transactional
public void customException() throws CustomCheckedException { // Noncompliant [[secondary=45;quickfixes=qf7,qf8]]
// ^^^^^^^^^^^^^^^
// fix@qf7 {{Add rollbackFor attribute}}
// edit@qf7 [[sl=-1;sc=3;el=-1;ec=17]] {{@Transactional(rollbackFor = checks.spring.CustomCheckedException.class)}}
// fix@qf8 {{Add rollbackFor = Exception.class}}
// edit@qf8 [[sl=-1;sc=3;el=-1;ec=17]] {{@Transactional(rollbackFor = java.lang.Exception.class)}}
}
@Transactional
public void mixedExceptions() throws IOException, RuntimeException { // Noncompliant [[secondary=54;quickfixes=qf9,qf10]]
// ^^^^^^^^^^^^^^^
// fix@qf9 {{Add rollbackFor attribute}}
// edit@qf9 [[sl=-1;sc=3;el=-1;ec=17]] {{@Transactional(rollbackFor = java.io.IOException.class)}}
// fix@qf10 {{Add rollbackFor = Exception.class}}
// edit@qf10 [[sl=-1;sc=3;el=-1;ec=17]] {{@Transactional(rollbackFor = java.lang.Exception.class)}}
}
@Transactional(rollbackFor = IOException.class)
public void withRollbackFor() throws IOException {
}
@Transactional(rollbackFor = {IOException.class, SQLException.class})
public void withMultipleRollbackFor() throws IOException, SQLException {
}
@Transactional(rollbackFor = Exception.class)
public void rollbackForAll() throws Exception {
}
@Transactional(noRollbackFor = NotificationException.class)
public void withNoRollbackFor() throws NotificationException {
}
@Transactional(rollbackFor = Exception.class, noRollbackFor = NotificationException.class)
public void withBoth() throws Exception {
}
@Transactional(rollbackForClassName = "java.io.IOException")
public void rollbackForClassName() throws IOException {
}
@Transactional(noRollbackForClassName = "checks.spring.NotificationException")
public void noRollbackForClassName() throws NotificationException {
}
@Transactional
public void noExceptions() {
}
@Transactional
public void uncheckedOnly() throws RuntimeException {
}
public void noAnnotation() throws IOException {
}
@Transactional(rollbackFor = Exception.class)
static class ClassLevelConfig {
public void inherited() throws IOException {
}
}
@Transactional
static class ClassLevelNoConfig {
public void noConfig() throws IOException { // Compliant - method name does not suggest a transactional operation
}
public void saveOrder() throws IOException { // Noncompliant [[secondary=108]]
// ^^^^^^^^^
}
public void deleteRecord() throws IOException { // Noncompliant [[secondary=108]]
// ^^^^^^^^^^^^
}
public void updateStatus() throws IOException { // Noncompliant [[secondary=108]]
// ^^^^^^^^^^^^
}
public void persistEntity() throws IOException { // Noncompliant [[secondary=108]]
// ^^^^^^^^^^^^^
}
public void mergeData() throws IOException { // Noncompliant [[secondary=108]]
// ^^^^^^^^^
}
public void flushChanges() throws IOException { // Noncompliant [[secondary=108]]
// ^^^^^^^^^^^^
}
public void insertRecord() throws IOException { // Noncompliant [[secondary=108]]
// ^^^^^^^^^^^^
}
public void createEntity() throws IOException { // Compliant - "create" is not a transactional prefix
}
public void removeItem() throws IOException { // Compliant - "remove" is not a transactional prefix
}
public void storeData() throws IOException { // Compliant - "store" is not a transactional prefix
}
public void executeUpdate() throws IOException { // Compliant - "execute" is not a transactional prefix
}
public void save() throws IOException { // Noncompliant [[secondary=108]]
// ^^^^
}
public void readData() throws IOException { // Compliant - no transactional prefix
}
public void savedSearch() throws IOException { // Compliant - "saved" has no word boundary after "save"
}
public void persisted() throws IOException { // Compliant - "persisted" has no word boundary after "persist"
}
public void deletes() throws IOException { // Compliant - "deletes" has no word boundary after "delete"
}
@Transactional(rollbackFor = IOException.class)
public void methodOverrides() throws IOException {
}
}
@Transactional
public void errorNotChecked() throws Error {
}
@org.springframework.transaction.annotation.Transactional
public void fullyQualified() throws IOException { // Noncompliant [[secondary=178;quickfixes=qf13,qf14]]
// ^^^^^^^^^^^^^^
// fix@qf13 {{Add rollbackFor attribute}}
// edit@qf13 [[sl=-1;sc=3;el=-1;ec=60]] {{@org.springframework.transaction.annotation.Transactional(rollbackFor = java.io.IOException.class)}}
// fix@qf14 {{Add rollbackFor = Exception.class}}
// edit@qf14 [[sl=-1;sc=3;el=-1;ec=60]] {{@org.springframework.transaction.annotation.Transactional(rollbackFor = java.lang.Exception.class)}}
}
@Transactional(rollbackFor = IOException.class)
public void partialConfig() throws SQLException {
}
@Transactional(value = "txManager")
public void withValueAttribute() throws IOException { // Noncompliant [[secondary=191]]
// ^^^^^^^^^^^^^^^^^^
}
// Test nested structure to ensure parent traversal works
static class OuterClass {
@Transactional
static class InnerClassWithAnnotation {
public void nestedMethod() throws IOException { // Compliant - no transactional prefix
}
public void saveNestedEntity() throws IOException { // Noncompliant [[secondary=198]]
// ^^^^^^^^^^^^^^^^
}
}
static class InnerClassNoAnnotation {
public void nestedMethodNoAnnotation() throws IOException {
// No @Transactional at any level, so no issue
}
}
}
@Transactional
interface TransactionalInterface {
void interfaceMethod() throws IOException; // Compliant - no transactional prefix
void saveEntity() throws IOException; // Noncompliant [[secondary=215]]
// ^^^^^^^^^^ {{Specify rollback behavior for checked exceptions using "rollbackFor" or "noRollbackFor" attributes on the class-level @Transactional.}}
}
// Test meta-annotated (composed) annotation
@MyTransactional
public void metaAnnotated() throws IOException { // Noncompliant [[secondary=224]]
// ^^^^^^^^^^^^^
}
// Test annotation with value attribute (transaction manager name)
@Transactional("txManager")
public void valueShorthand() throws IOException { // Noncompliant [[secondary=230]]
// ^^^^^^^^^^^^^^
// Has value attribute but no rollback configuration
}
// @Transactional has no effect on non-public methods (Spring proxy-based AOP only intercepts public methods)
@Transactional
private void privateMethod() throws IOException { // Compliant - private methods are not proxied by Spring
}
@Transactional
private void privateMethodMultipleExceptions() throws IOException, SQLException { // Compliant
}
@Transactional
protected void protectedMethod() throws IOException { // Compliant - protected methods are not proxied by Spring
}
@Transactional
void packagePrivateMethod() throws IOException { // Compliant - package-private methods are not proxied by Spring
}
@Transactional
static class ClassLevelWithNonPublicMethods {
private void privateInClassLevel() throws IOException { // Compliant - private methods are not proxied
}
protected void protectedInClassLevel() throws IOException { // Compliant - protected methods are not proxied
}
void packagePrivateInClassLevel() throws IOException { // Compliant - package-private methods are not proxied
}
public void publicInClassLevel() throws IOException { // Compliant - no transactional prefix
}
public void savePublicInClassLevel() throws IOException { // Noncompliant [[secondary=253]]
// ^^^^^^^^^^^^^^^^^^^^^^
}
}
// Compliant: propagation = NOT_SUPPORTED means no transaction is created
@Transactional(propagation = Propagation.NOT_SUPPORTED)
public void notSupported() throws IOException { // Compliant
}
// Compliant: propagation = NEVER means no transaction is created
@Transactional(propagation = Propagation.NEVER)
public void neverPropagation() throws IOException { // Compliant
}
// Compliant: readOnly = true means no writes can occur
@Transactional(readOnly = true)
public void readOnlyTransaction() throws IOException { // Compliant
}
// Compliant: combination of readOnly and NOT_SUPPORTED
@Transactional(readOnly = true, propagation = Propagation.NOT_SUPPORTED)
public void readOnlyAndNotSupported() throws IOException { // Compliant
}
// readOnly = false with default propagation is still noncompliant
@Transactional(readOnly = false)
public void readOnlyFalse() throws IOException { // Noncompliant [[secondary=293]]
// ^^^^^^^^^^^^^
}
// Explicit REQUIRED propagation still needs rollback config
@Transactional(propagation = Propagation.REQUIRED)
public void requiredPropagation() throws IOException { // Noncompliant [[secondary=299]]
// ^^^^^^^^^^^^^^^^^^^
}
// Compliant: NOT_SUPPORTED with other attributes
@Transactional(propagation = Propagation.NOT_SUPPORTED, timeout = 30)
public void notSupportedWithTimeout() throws IOException { // Compliant
}
// Compliant: readOnly with other attributes
@Transactional(readOnly = true, timeout = 30)
public void readOnlyWithTimeout() throws IOException { // Compliant
}
// Compliant: static-imported NOT_SUPPORTED propagation (identifier without member select)
@Transactional(propagation = NOT_SUPPORTED)
public void notSupportedStaticImport() throws IOException { // Compliant
}
@Transactional(propagation = Propagation.NOT_SUPPORTED)
static class ClassLevelNotSupported {
public void methodInNotSupportedClass() throws IOException { // Compliant
}
}
@Transactional(readOnly = true)
static class ClassLevelReadOnly {
public void methodInReadOnlyClass() throws IOException { // Compliant
}
}
// Method with non-Transactional annotations, class-level @Transactional found after iterating class annotations
@Deprecated
@Transactional
static class ClassWithMultipleAnnotations {
@Deprecated
@SuppressWarnings("unused")
public void methodWithOtherAnnotations() throws IOException { // Compliant - no transactional prefix
}
@Deprecated
public void deleteWithAnnotations() throws IOException { // Noncompliant [[secondary=333]]
// ^^^^^^^^^^^^^^^^^^^^^
}
}
@Transactional
static class ClassLevelWithMethodCalls {
public void processOrder(Object repository) throws IOException { // Noncompliant [[secondary=346]]
// ^^^^^^^^^^^^
repository.toString();
saveOrder();
}
public void doWork() throws IOException { // Compliant - no transactional prefix and no transactional calls
System.out.println("work");
}
private void saveOrder() {
}
public void delegateToFlush() throws IOException { // Noncompliant [[secondary=346]]
// ^^^^^^^^^^^^^^^
flushAll();
}
private void flushAll() {
}
public void delegateToDelete(Object entity) throws IOException { // Noncompliant [[secondary=346]]
// ^^^^^^^^^^^^^^^^
deleteEntity(entity);
}
private void deleteEntity(Object entity) {
}
public void noTransactionalCalls() throws IOException { // Compliant - no transactional method calls
System.out.println("no transaction");
computeResult();
}
private void computeResult() {
}
public void onlySavesInsideLambda() throws IOException { // Compliant - save() is in a lambda assigned to a variable, not directly executed
Runnable r = () -> saveOrder();
}
public void savesInsideLambdaArgument() throws IOException { // Noncompliant [[secondary=346]]
// ^^^^^^^^^^^^^^^^^^^^^^^^^
java.util.List.of().forEach(o -> saveOrder());
}
public void onlySavesInsideAnonymousClass() throws IOException { // Compliant - save() is in an anonymous class method
Runnable r = new Runnable() {
@Override
public void run() {
saveOrder();
}
};
}
public void onlySavesInsideLocalClass() throws IOException { // Compliant - save() is in a local class method
class Local {
void doSave() {
saveOrder();
}
}
}
public void directSaveAndLambda() throws IOException { // Noncompliant [[secondary=346]]
// ^^^^^^^^^^^^^^^^^^^
saveOrder();
Runnable r = () -> deleteEntity(null);
}
}
static class Repository {
void save(Object o) {
}
void saveChecked(Object o) throws IOException {
}
}
static class Validator {
static void validate(Object o) throws IOException {
}
Validator() throws SQLException {
}
}
@Transactional
static class ThrowsBeforeWrites {
private final Repository repository = new Repository();
public void saveAfterThrow(Object o) throws IOException { // Compliant - all checked exceptions are thrown before any write
if (o == null) {
throw new IOException("null");
}
repository.save(o);
}
public void saveAfterValidation(Object o) throws IOException { // Compliant - validation throwing checked exception happens before any write
Validator.validate(o);
repository.save(o);
}
public void saveAfterConstructor(Object o) throws SQLException { // Compliant - constructor throwing checked exception happens before any write
new Validator();
repository.save(o);
}
public void saveAfterThrowInLoop(java.util.List<Object> list) throws IOException { // Compliant
for (Object o : list) {
if (o == null) {
throw new IOException("null");
}
}
list.forEach(repository::save);
list.forEach(o -> repository.save(o));
}
public void saveBeforeThrow(Object o) throws IOException { // Noncompliant
// ^^^^^^^^^^^^^^^
repository.save(o);
if (o == null) {
throw new IOException("null");
}
}
public void saveBeforeValidation(Object o) throws IOException { // Noncompliant
// ^^^^^^^^^^^^^^^^^^^^
repository.save(o);
Validator.validate(o);
}
public void saveBeforeConstructor(Object o) throws SQLException { // Noncompliant
// ^^^^^^^^^^^^^^^^^^^^^
repository.save(o);
new Validator();
}
public void saveThrowingChecked(Object o) throws IOException { // Noncompliant
// ^^^^^^^^^^^^^^^^^^^
repository.saveChecked(o);
}
public void saveInLoopWithThrow(java.util.List<Object> list) throws IOException { // Noncompliant
// ^^^^^^^^^^^^^^^^^^^
for (Object o : list) {
if (o == null) {
throw new IOException("null");
}
repository.save(o);
}
}
public void saveInLambdaBeforeThrow(java.util.List<Object> list) throws IOException { // Noncompliant
// ^^^^^^^^^^^^^^^^^^^^^^^
list.forEach(o -> repository.save(o));
Validator.validate(list);
}
public void saveInMethodReferenceBeforeThrow(java.util.List<Object> list) throws IOException { // Noncompliant
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
list.forEach(repository::save);
Validator.validate(list);
repository.save(list);
}
public void saveInTryThenThrowInCatch(Object o) throws IOException { // Noncompliant
// ^^^^^^^^^^^^^^^^^^^^^^^^^
try {
repository.save(o);
} catch (RuntimeException e) {
throw new IOException(e);
}
}
public void saveThenRethrowUnknownType(Object o, Exception e) throws Exception { // Noncompliant
// ^^^^^^^^^^^^^^^^^^^^^^^^^^
repository.save(o);
throw e;
}
public void saveInTryWithResources(java.nio.file.Path path, Object o) throws IOException { // Noncompliant
// ^^^^^^^^^^^^^^^^^^^^^^
try (java.io.BufferedReader reader = java.nio.file.Files.newBufferedReader(path)) {
repository.save(o);
}
}
public void saveInTryWithResourcesNotThrowingOnClose(java.nio.file.Path path, java.util.List<Object> list) throws IOException { // Compliant - close() of Stream does not throw checked exceptions
try (java.util.stream.Stream<String> lines = java.nio.file.Files.lines(path)) {
repository.save(lines);
}
}
public void saveBeforeTryWithResources(Object o) throws IOException { // Noncompliant
// ^^^^^^^^^^^^^^^^^^^^^^^^^^
if (o == null) {
throw new IOException();
}
repository.save(o);
try (java.io.ByteArrayInputStream in = new java.io.ByteArrayInputStream(new byte[0])) {
in.available();
}
}
public void saveAfterTryWithResources(Object o) throws IOException { // Compliant - the resource is closed before the write
try (java.io.ByteArrayInputStream in = new java.io.ByteArrayInputStream(new byte[0])) {
in.available();
}
repository.save(o);
}
public void executeUpdateBeforeValidation(jakarta.persistence.EntityManager em, Object o) throws IOException { // Noncompliant
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
em.createQuery("UPDATE Order o SET o.status = 'DONE'").executeUpdate();
Validator.validate(o);
repository.save(o);
}
public void batchUpdateBeforeValidation(org.springframework.jdbc.core.JdbcTemplate jdbcTemplate, Object o) throws IOException { // Noncompliant
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^
jdbcTemplate.batchUpdate("UPDATE orders SET status = 'DONE'");
Validator.validate(o);
repository.save(o);
}
public void readBeforeValidation(org.springframework.jdbc.core.JdbcTemplate jdbcTemplate, Object o) throws IOException { // Compliant - only reads before the checked exception
jdbcTemplate.queryForList("SELECT * FROM orders");
Validator.validate(o);
jdbcTemplate.execute("UPDATE orders SET status = 'DONE'");
}
public void saveAfterUncheckedThrow(Object o) throws IOException { // Noncompliant
// ^^^^^^^^^^^^^^^^^^^^^^^
if (o == null) {
throw new IllegalArgumentException();
}
repository.save(o);
throw new IllegalStateException();
}
}
private final Repository repository = new Repository();
@Transactional
public void methodLevelThrowBeforeWrite(Object o) throws IOException { // Compliant - all checked exceptions are thrown before any write
if (o == null) {
throw new IOException("null");
}
repository.save(o);
}
@Transactional
public void methodLevelThrowWithoutWrite(Object o) throws IOException { // Noncompliant
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
if (o == null) {
throw new IOException("null");
}
o.toString();
}
@Transactional
public void methodLevelWriteBeforeThrow(Object o) throws IOException { // Noncompliant
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^
repository.save(o);
if (o == null) {
throw new IOException("null");
}
}
}