Skip to content

Commit bac1046

Browse files
committed
Move methods to util classes
1 parent 305938f commit bac1046

File tree

3 files changed

+80
-16
lines changed

3 files changed

+80
-16
lines changed

spotbugs/src/main/java/edu/umd/cs/findbugs/detect/AtomicOperationsCombinedDetector.java

+5-16
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
package edu.umd.cs.findbugs.detect;
2020

21-
import java.util.Arrays;
2221
import java.util.HashMap;
2322
import java.util.HashSet;
2423
import java.util.Iterator;
@@ -63,9 +62,9 @@
6362
import edu.umd.cs.findbugs.ba.ca.CallListDataflow;
6463
import edu.umd.cs.findbugs.bcel.BCELUtil;
6564
import edu.umd.cs.findbugs.classfile.CheckedAnalysisException;
66-
import edu.umd.cs.findbugs.classfile.Global;
6765
import edu.umd.cs.findbugs.classfile.MethodDescriptor;
68-
import edu.umd.cs.findbugs.classfile.engine.bcel.FinallyDuplicatesInfoFactory;
66+
import edu.umd.cs.findbugs.util.CollectionAnalysis;
67+
import edu.umd.cs.findbugs.util.MethodAnalysis;
6968

7069
public class AtomicOperationsCombinedDetector implements Detector {
7170

@@ -157,11 +156,7 @@ private static boolean isInterestingField(ClassMember classMember) {
157156
if (classMember == null) {
158157
return false;
159158
}
160-
Set<String> interestingCollectionMethodNames = new HashSet<>(Arrays.asList(
161-
"synchronizedCollection", "synchronizedSet", "synchronizedSortedSet",
162-
"synchronizedNavigableSet", "synchronizedList", "synchronizedMap",
163-
"synchronizedSortedMap", "synchronizedNavigableMap"));
164-
return ("java.util.Collections".equals(classMember.getClassName()) && interestingCollectionMethodNames.contains(classMember.getName()))
159+
return CollectionAnalysis.isSynchronizedCollection(classMember)
165160
|| (classMember.getClassName().startsWith("java.util.concurrent.atomic") && classMember.getSignature().endsWith(")V"));
166161
}
167162

@@ -175,8 +170,6 @@ private void analyzeInterestingFields(ClassContext classContext, Method method)
175170
CallListDataflow callListDataflow = classContext.getCallListDataflow(method);
176171
JavaClass javaClass = classContext.getJavaClass();
177172
MethodDescriptor methodDescriptor = BCELUtil.getMethodDescriptor(javaClass, method);
178-
FinallyDuplicatesInfoFactory.FinallyDuplicatesInfo methodAnalysis =
179-
Global.getAnalysisCache().getMethodAnalysis(FinallyDuplicatesInfoFactory.FinallyDuplicatesInfo.class, methodDescriptor);
180173
boolean synchronizedBlock = false;
181174

182175
for (Location location : cfg.orderedLocations()) {
@@ -189,7 +182,7 @@ private void analyzeInterestingFields(ClassContext classContext, Method method)
189182
synchronizedBlock = true;
190183
} else if (instruction instanceof MONITOREXIT) {
191184
synchronizedBlock = false;
192-
} else if (instruction instanceof PUTFIELD && !synchronizedBlock && !isDuplicatedLocation(methodAnalysis, pc)) {
185+
} else if (instruction instanceof PUTFIELD && !synchronizedBlock && !MethodAnalysis.isDuplicatedLocation(methodDescriptor, pc)) {
193186
XField xField = XFactory.createXField((FieldInstruction) instruction, cpg);
194187
if (interestingFields.contains(xField)) {
195188
bugPrototype.invokedField = xField;
@@ -198,7 +191,7 @@ private void analyzeInterestingFields(ClassContext classContext, Method method)
198191
.add(bugPrototype);
199192
}
200193
} else if (instruction instanceof InvokeInstruction && !(instruction instanceof INVOKEDYNAMIC)
201-
&& !synchronizedBlock && !isDuplicatedLocation(methodAnalysis, pc)) {
194+
&& !synchronizedBlock && !MethodAnalysis.isDuplicatedLocation(methodDescriptor, pc)) {
202195
OpcodeStack stack = OpcodeStackScanner.getStackAt(javaClass, method, pc);
203196
Optional<XField> interestingField = getInterestingField(stack);
204197
XMethod xMethod = XFactory.createXMethod((InvokeInstruction) instruction, cpg);
@@ -230,10 +223,6 @@ private void analyzeInterestingFields(ClassContext classContext, Method method)
230223
}
231224
}
232225

233-
private boolean isDuplicatedLocation(FinallyDuplicatesInfoFactory.FinallyDuplicatesInfo methodAnalysis, int pc) {
234-
return methodAnalysis.getDuplicates(pc).stream().anyMatch(duplicatePc -> duplicatePc < pc);
235-
}
236-
237226
private Optional<XField> getInterestingField(OpcodeStack stack) {
238227
if (stack.getStackDepth() > 1 && stack.getStackItem(0).getReturnValueOf() != null) {
239228
return Optional.empty();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package edu.umd.cs.findbugs.util;
2+
3+
import java.util.Arrays;
4+
import java.util.HashSet;
5+
import java.util.Set;
6+
7+
import edu.umd.cs.findbugs.ba.ClassMember;
8+
9+
/**
10+
* Utility class for analyzing collections.
11+
*/
12+
public final class CollectionAnalysis {
13+
14+
private CollectionAnalysis() {
15+
}
16+
17+
/**
18+
* Check if a class member is a synchronized collection.
19+
*
20+
* @param classMember the class member
21+
* @return {@code true} if the class member is a synchronized collection, {@code false} otherwise
22+
*/
23+
public static boolean isSynchronizedCollection(ClassMember classMember) {
24+
Set<String> interestingCollectionMethodNames = new HashSet<>(Arrays.asList(
25+
"synchronizedCollection", "synchronizedSet", "synchronizedSortedSet",
26+
"synchronizedNavigableSet", "synchronizedList", "synchronizedMap",
27+
"synchronizedSortedMap", "synchronizedNavigableMap"));
28+
return "java.util.Collections".equals(classMember.getClassName()) && interestingCollectionMethodNames.contains(classMember.getName());
29+
}
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* SpotBugs - Find bugs in Java programs
3+
*
4+
* This library is free software; you can redistribute it and/or
5+
* modify it under the terms of the GNU Lesser General Public
6+
* License as published by the Free Software Foundation; either
7+
* version 2.1 of the License, or (at your option) any later version.
8+
*
9+
* This library is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12+
* Lesser General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU Lesser General Public
15+
* License along with this library; if not, write to the Free Software
16+
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17+
*/
18+
19+
package edu.umd.cs.findbugs.util;
20+
21+
import edu.umd.cs.findbugs.classfile.CheckedAnalysisException;
22+
import edu.umd.cs.findbugs.classfile.Global;
23+
import edu.umd.cs.findbugs.classfile.MethodDescriptor;
24+
import edu.umd.cs.findbugs.classfile.engine.bcel.FinallyDuplicatesInfoFactory;
25+
26+
/**
27+
* Utility class for method analysis.
28+
*/
29+
public final class MethodAnalysis {
30+
31+
private MethodAnalysis() {
32+
}
33+
34+
/**
35+
* Check if the location is duplicated in the method.
36+
*
37+
* @return {@code true} if the location is duplicated in the method, {@code false} otherwise
38+
* @throws CheckedAnalysisException if an error occurs during the analysis
39+
*/
40+
public static boolean isDuplicatedLocation(MethodDescriptor methodDescriptor, int pc) throws CheckedAnalysisException {
41+
FinallyDuplicatesInfoFactory.FinallyDuplicatesInfo methodAnalysis =
42+
Global.getAnalysisCache().getMethodAnalysis(FinallyDuplicatesInfoFactory.FinallyDuplicatesInfo.class, methodDescriptor);
43+
return methodAnalysis.getDuplicates(pc).stream().anyMatch(duplicatePc -> duplicatePc < pc);
44+
}
45+
}

0 commit comments

Comments
 (0)