@@ -86,10 +86,12 @@ public static void analyze(
86
86
LockEventListener listener ,
87
87
Predicate <Tree > isSuppressed ,
88
88
GuardedByFlags flags ,
89
- boolean reportMissingGuards ) {
89
+ boolean reportMissingGuards ,
90
+ boolean checkTryWithResources ) {
90
91
HeldLockSet locks = HeldLockSet .empty ();
91
92
locks = handleMonitorGuards (state , locks , flags );
92
- new LockScanner (state , listener , isSuppressed , flags , reportMissingGuards )
93
+ new LockScanner (
94
+ state , listener , isSuppressed , flags , reportMissingGuards , checkTryWithResources )
93
95
.scan (state .getPath (), locks );
94
96
}
95
97
@@ -126,6 +128,7 @@ private static class LockScanner extends TreePathScanner<Void, HeldLockSet> {
126
128
private final Predicate <Tree > isSuppressed ;
127
129
private final GuardedByFlags flags ;
128
130
private final boolean reportMissingGuards ;
131
+ private final boolean checkTryWithResources ;
129
132
130
133
private static final GuardedByExpression .Factory F = new GuardedByExpression .Factory ();
131
134
@@ -134,12 +137,14 @@ private LockScanner(
134
137
LockEventListener listener ,
135
138
Predicate <Tree > isSuppressed ,
136
139
GuardedByFlags flags ,
137
- boolean reportMissingGuards ) {
140
+ boolean reportMissingGuards ,
141
+ boolean checkTryWithResources ) {
138
142
this .visitorState = visitorState ;
139
143
this .listener = listener ;
140
144
this .isSuppressed = isSuppressed ;
141
145
this .flags = flags ;
142
146
this .reportMissingGuards = reportMissingGuards ;
147
+ this .checkTryWithResources = checkTryWithResources ;
143
148
}
144
149
145
150
@ Override
@@ -182,12 +187,11 @@ public Void visitTry(TryTree tree, HeldLockSet locks) {
182
187
// are held for the entirety of the try and catch statements.
183
188
Collection <GuardedByExpression > releasedLocks =
184
189
ReleasedLockFinder .find (tree .getFinallyBlock (), visitorState , flags );
185
- if (resources .isEmpty ()) {
190
+ // We don't know what to do with the try-with-resources block.
191
+ // TODO(cushon) - recognize common try-with-resources patterns. Currently there is no
192
+ // standard implementation of an AutoCloseable lock resource to detect.
193
+ if (checkTryWithResources || resources .isEmpty ()) {
186
194
scan (tree .getBlock (), locks .plusAll (releasedLocks ));
187
- } else {
188
- // We don't know what to do with the try-with-resources block.
189
- // TODO(cushon) - recognize common try-with-resources patterns. Currently there is no
190
- // standard implementation of an AutoCloseable lock resource to detect.
191
195
}
192
196
scan (tree .getCatches (), locks .plusAll (releasedLocks ));
193
197
scan (tree .getFinallyBlock (), locks );
0 commit comments