Skip to content

Commit 86afdfe

Browse files
committed
Merge branch 'spotbugs' into findbugs
Conflicts: .classpath pom.xml
2 parents e883629 + 6fe03d3 commit 86afdfe

File tree

7 files changed

+64
-17
lines changed

7 files changed

+64
-17
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ Contributors
4949
* Pavel Roskin
5050
* Kevin Seymour
5151
* Piotrek Żygieło
52+
* Guillaume Toison
5253

5354

5455
fb-contrib has two main branches, 'findbugs' and 'spotbugs'. Code is committed to spotbugs, and then merged back to findbugs.

build.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@
3131
<property name="javac.debug" value="on" />
3232
<property name="test_reports.dir" value="${target.dir}/reports/test"/>
3333

34-
<property name="fb-contrib.version" value="7.6.6" />
35-
<property name="sb-contrib.version" value="7.6.6" />
34+
<property name="fb-contrib.version" value="7.6.7-SNAPSHOT" />
35+
<property name="sb-contrib.version" value="7.6.7-SNAPSHOT" />
3636

3737
<property name="sonatype.dir" value="${user.home}/.fb-contrib-${fb-contrib.version}-sonatype" />
3838

etc/findbugs.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,15 @@
2323

2424
<!-- Detectors -->
2525

26-
<!-- COMMENT OUT FOR RELEASE
26+
<!-- COMMENT OUT FOR RELEASE -->
2727

2828
<Detector class="com.mebigfatguy.fbcontrib.debug.OCSDebugger" speed="fast"/>
2929

3030
<Detector class="com.mebigfatguy.fbcontrib.detect.BloatedSynchronizedBlock" speed="fast" reports="BSB_BLOATED_SYNCHRONIZED_BLOCK" hidden="true" />
3131

3232
<Detector class="com.mebigfatguy.fbcontrib.detect.BloatedAssignmentScope" speed="fast" reports="BAS_BLOATED_ASSIGNMENT_SCOPE" hidden="true" />
3333

34-
COMMENT OUT FOR RELEASE -->
34+
<!-- COMMENT OUT FOR RELEASE -->
3535

3636
<Detector class="com.mebigfatguy.fbcontrib.collect.CollectStatistics" speed="fast" reports="" hidden="true" />
3737

@@ -644,7 +644,7 @@
644644
<BugPattern abbrev="LUI" type="LUI_USE_COLLECTION_ADD" category="CORRECTNESS" />
645645
<BugPattern abbrev="LUI" type="LUI_USE_GET0" category="CORRECTNESS" />
646646
<BugPattern abbrev="FII" type="FII_USE_METHOD_REFERENCE" category="CORRECTNESS" />
647-
<BugPattern abbrev="FII" type="FII_AVOID_CONTAINS_ON_COLLECTED_STREAM"/>
647+
<BugPattern abbrev="FII" type="FII_AVOID_CONTAINS_ON_COLLECTED_STREAM" category="CORRECTNESS" />
648648
<BugPattern abbrev="FII" type="FII_USE_ANY_MATCH" category="CORRECTNESS"/>
649649
<BugPattern abbrev="FII" type="FII_USE_FIND_FIRST" category="CORRECTNESS"/>
650650
<BugPattern abbrev="FII" type="FII_COMBINE_FILTERS" category="CORRECTNESS"/>

etc/messages.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2708,7 +2708,7 @@ static {
27082708

27092709
<BugPattern type="PMB_LOCAL_BASED_JAXB_CONTEXT">
27102710
<ShortDescription>Local JAXBContext created on demand</ShortDescription>
2711-
<LongDescription>Method {1} createa a local JAXBContext on demand</LongDescription>
2711+
<LongDescription>Method {1} creates a local JAXBContext on demand</LongDescription>
27122712
<Details>
27132713
<![CDATA[
27142714
<p>This method creates a JAXBContext and stores it in a local variable. This
@@ -6208,6 +6208,7 @@ if (shouldCalcHalting && (calculateHaltingProbability() &gt; 0) { }
62086208
<p>Unfortunately there isn't just one @Nullable annotation, but this detector will recognize:</p>
62096209
<ul>
62106210
<li>org.jetbrains.annotations.Nullable</li>
6211+
<li>jakarta.annotation.Nullable</li>
62116212
<li>javax.annotation.Nullable</li>
62126213
<li>javax.annotation.CheckForNull</li>
62136214
<li>edu.umd.cs.findbugs.annotations.Nullable</li>

pom.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77

88
<groupId>com.mebigfatguy.fb-contrib</groupId>
99
<artifactId>fb-contrib</artifactId>
10-
<version>7.6.6</version>
10+
<version>7.6.7-SNAPSHOT</version>
11+
1112

1213
<!-- TODO: Oss parent is obsolete, define items directly for releasing -->
1314
<parent>

src/main/java/com/mebigfatguy/fbcontrib/detect/OptionalIssues.java

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
import java.util.ArrayDeque;
2222
import java.util.BitSet;
2323
import java.util.Deque;
24+
import java.util.HashMap;
25+
import java.util.Map;
2426
import java.util.Set;
2527

2628
import javax.annotation.Nullable;
@@ -35,6 +37,8 @@
3537
import org.apache.bcel.classfile.JavaClass;
3638
import org.apache.bcel.classfile.Method;
3739

40+
import com.mebigfatguy.fbcontrib.collect.MethodInfo;
41+
import com.mebigfatguy.fbcontrib.collect.Statistics;
3842
import com.mebigfatguy.fbcontrib.utils.BugType;
3943
import com.mebigfatguy.fbcontrib.utils.FQMethod;
4044
import com.mebigfatguy.fbcontrib.utils.SignatureBuilder;
@@ -46,7 +50,9 @@
4650
import edu.umd.cs.findbugs.BytecodeScanningDetector;
4751
import edu.umd.cs.findbugs.OpcodeStack;
4852
import edu.umd.cs.findbugs.OpcodeStack.CustomUserValue;
53+
import edu.umd.cs.findbugs.SourceLineAnnotation;
4954
import edu.umd.cs.findbugs.ba.ClassContext;
55+
import edu.umd.cs.findbugs.ba.SignatureParser;
5056
import edu.umd.cs.findbugs.ba.XMethod;
5157

5258
/**
@@ -55,6 +61,12 @@
5561
@CustomUserValue
5662
public class OptionalIssues extends BytecodeScanningDetector {
5763

64+
private enum OptionalType {
65+
PLAIN,
66+
BOXED
67+
}
68+
69+
private static final String OPTIONAL_SIGNATURE = "Ljava/util/Optional;";
5870
private static Set<String> BOXED_OPTIONAL_TYPES = UnmodifiableSet.create("Ljava/lang/Integer;", "Ljava/lang/Long;",
5971
"Ljava/lang/Double;");
6072

@@ -99,6 +111,8 @@ OPTIONAL_OR_ELSE_METHOD, new FQMethod("java/util/OptionalDouble", "orElse", "(D)
99111
private OpcodeStack stack;
100112
private JavaClass currentClass;
101113
private Deque<ActiveStackOp> activeStackOps;
114+
private Map<OpcodeStack.Item, SourceLineAnnotation> boxedItems = new HashMap<>();
115+
private boolean methodIsConstrained;
102116

103117
static {
104118
INVOKE_OPS.set(Const.INVOKEINTERFACE);
@@ -155,7 +169,21 @@ public void visitClassContext(ClassContext classContext) {
155169
public void visitCode(Code obj) {
156170
stack.resetForMethodEntry(this);
157171
activeStackOps.clear();
172+
boxedItems.clear();
173+
methodIsConstrained = false;
174+
175+
String returnType = new SignatureParser(getMethodSig()).getReturnTypeSignature();
176+
if (OPTIONAL_SIGNATURE.equals(returnType)) {
177+
MethodInfo mi = Statistics.getStatistics().getMethodStatistics(getClassName(), getMethodName(), getMethodSig());
178+
methodIsConstrained = mi != null && mi.isDerived();
179+
}
158180
super.visitCode(obj);
181+
182+
for (SourceLineAnnotation slAnno : boxedItems.values()) {
183+
bugReporter.reportBug(
184+
new BugInstance(this, BugType.OI_OPTIONAL_ISSUES_PRIMITIVE_VARIANT_PREFERRED.name(),
185+
LOW_PRIORITY).addClass(this).addMethod(this).addSourceLine(slAnno));
186+
}
159187
}
160188

161189
/**
@@ -168,7 +196,7 @@ public void visitCode(Code obj) {
168196
@Override
169197
public void sawOpcode(int seen) {
170198
FQMethod curCalledMethod = null;
171-
Boolean sawPlainOptional = null;
199+
OptionalType optionalType = null;
172200

173201
try {
174202
switch (seen) {
@@ -207,9 +235,7 @@ public void sawOpcode(int seen) {
207235
OpcodeStack.Item itm = stack.getStackItem(0);
208236
String itmSig = itm.getSignature();
209237
if (BOXED_OPTIONAL_TYPES.contains(itmSig)) {
210-
bugReporter.reportBug(
211-
new BugInstance(this, BugType.OI_OPTIONAL_ISSUES_PRIMITIVE_VARIANT_PREFERRED.name(),
212-
LOW_PRIORITY).addClass(this).addMethod(this).addSourceLine(this));
238+
optionalType = OptionalType.BOXED;
213239
}
214240
}
215241
}
@@ -245,7 +271,7 @@ public void sawOpcode(int seen) {
245271
}
246272
}
247273
if (OPTIONAL_OR_ELSE_METHOD.equals(curCalledMethod)) {
248-
sawPlainOptional = Boolean.TRUE;
274+
; optionalType = OptionalType.PLAIN;
249275
}
250276
} else if (OR_ELSE_GET_METHODS.contains(curCalledMethod)) {
251277
if (!activeStackOps.isEmpty()) {
@@ -276,12 +302,18 @@ public void sawOpcode(int seen) {
276302
}
277303
}
278304
if (OPTIONAL_OR_ELSE_GET_METHOD.equals(curCalledMethod)) {
279-
sawPlainOptional = Boolean.TRUE;
305+
optionalType = OptionalType.PLAIN;
280306
}
281307
} else if (OPTIONAL_GET_METHOD.equals(curCalledMethod)) {
282-
sawPlainOptional = Boolean.TRUE;
308+
optionalType = OptionalType.PLAIN;
283309
}
284310
break;
311+
312+
case Const.ARETURN:
313+
if (methodIsConstrained && stack.getStackDepth() > 0) {
314+
boxedItems.remove(stack.getStackItem(0));
315+
}
316+
break;
285317
}
286318
} catch (ClassNotFoundException e) {
287319
bugReporter.reportMissingClass(e);
@@ -295,9 +327,13 @@ public void sawOpcode(int seen) {
295327
while (activeStackOps.size() > stackDepth) {
296328
activeStackOps.removeFirst();
297329
}
298-
if (sawPlainOptional != null) {
330+
if (optionalType != null) {
299331
OpcodeStack.Item itm = stack.getStackItem(0);
300-
itm.setUserValue(sawPlainOptional);
332+
itm.setUserValue(optionalType);
333+
if (optionalType == OptionalType.BOXED) {
334+
boxedItems.put(itm, SourceLineAnnotation.fromVisitedInstruction(OptionalIssues.this.getClassContext(),
335+
OptionalIssues.this, getPC()));
336+
}
301337
}
302338
}
303339
}

src/samples/java/ex/OI_Sample.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import java.util.OptionalInt;
77
import java.util.function.Supplier;
88

9-
public class OI_Sample {
9+
public class OI_Sample implements OptInf386 {
1010

1111
public String useDelayedExecution(Optional<String> o, String a, String b) {
1212

@@ -73,4 +73,12 @@ public String fpGet384(String parameterName, Supplier<String> defaultValueSuppli
7373
public boolean equalsToEmpty(Optional<String> foo) {
7474
return foo.equals(Optional.empty());
7575
}
76+
77+
public Optional<Object> fpGetOpt386() {
78+
return Optional.of(Double.valueOf(10));
79+
}
80+
}
81+
82+
interface OptInf386 {
83+
Optional<Object> fpGetOpt386();
7684
}

0 commit comments

Comments
 (0)