diff --git a/etc/bugrank.txt b/etc/bugrank.txt index 3522e0f9..13005cb8 100644 --- a/etc/bugrank.txt +++ b/etc/bugrank.txt @@ -174,6 +174,7 @@ +0 BugPattern OC_OVERZEALOUS_CASTING +0 BugPattern ODN_ORPHANED_DOM_NODE +0 BugPattern OI_OPTIONAL_ISSUES_CHECKING_REFERENCE ++0 BugPattern OI_OPTIONAL_ISSUES_ISPRESENT_PREFERRED +0 BugPattern OI_OPTIONAL_ISSUES_PRIMITIVE_VARIANT_PREFERRED +0 BugPattern OI_OPTIONAL_ISSUES_USES_DELAYED_EXECUTION +0 BugPattern OI_OPTIONAL_ISSUES_USES_IMMEDIATE_EXECUTION diff --git a/etc/findbugs.xml b/etc/findbugs.xml index 35ca5dce..1a61a18e 100755 --- a/etc/findbugs.xml +++ b/etc/findbugs.xml @@ -310,7 +310,7 @@ - + @@ -629,6 +629,7 @@ + diff --git a/etc/messages.xml b/etc/messages.xml index b061823d..a35ba3f6 100755 --- a/etc/messages.xml +++ b/etc/messages.xml @@ -6115,6 +6115,18 @@ if (shouldCalcHalting && (calculateHaltingProbability() > 0) { } ]]> + + + Method uses Optional.equals(Optional.empty()), when Optional.isPresent is more readable + Method {1} uses Optional.equals(Optional.empty()), when Optional.isPresent is more readable +
+ This method uses Optional.equals(Optional.empty()). It is more readable and more clear just to use the Optional.isPresent() + method to determine whether the reference exists or not. +

+ ]]> +
+
Method constructs a Date object, merely to convert it to an Instant object diff --git a/src/main/java/com/mebigfatguy/fbcontrib/detect/OptionalIssues.java b/src/main/java/com/mebigfatguy/fbcontrib/detect/OptionalIssues.java index ba336b93..01760c42 100644 --- a/src/main/java/com/mebigfatguy/fbcontrib/detect/OptionalIssues.java +++ b/src/main/java/com/mebigfatguy/fbcontrib/detect/OptionalIssues.java @@ -206,16 +206,23 @@ public void sawOpcode(int seen) { String methodName = getNameConstantOperand(); curCalledMethod = new FQMethod(clsName, methodName, getSigConstantOperand()); - if ("java/util/Optional".equals(clsName) && "of".equals(methodName)) { - if (stack.getStackDepth() > 0) { - OpcodeStack.Item itm = stack.getStackItem(0); - String itmSig = itm.getSignature(); - if (BOXED_OPTIONAL_TYPES.contains(itmSig)) { - bugReporter.reportBug( - new BugInstance(this, BugType.OI_OPTIONAL_ISSUES_PRIMITIVE_VARIANT_PREFERRED.name(), - LOW_PRIORITY).addClass(this).addMethod(this).addSourceLine(this)); - } - } + if ("java/util/Optional".equals(clsName)) { + if ("of".equals(methodName)) { + if (stack.getStackDepth() > 0) { + OpcodeStack.Item itm = stack.getStackItem(0); + String itmSig = itm.getSignature(); + if (BOXED_OPTIONAL_TYPES.contains(itmSig)) { + bugReporter.reportBug( + new BugInstance(this, BugType.OI_OPTIONAL_ISSUES_PRIMITIVE_VARIANT_PREFERRED.name(), + LOW_PRIORITY).addClass(this).addMethod(this).addSourceLine(this)); + } + } + } else if ("equals".equals(methodName)) { + bugReporter.reportBug( + new BugInstance(this, BugType.OI_OPTIONAL_ISSUES_ISPRESENT_PREFERRED.name(), + LOW_PRIORITY).addClass(this).addMethod(this).addSourceLine(this)); + + } } break; diff --git a/src/main/java/com/mebigfatguy/fbcontrib/utils/BugType.java b/src/main/java/com/mebigfatguy/fbcontrib/utils/BugType.java index 50f6b797..0d7a7822 100644 --- a/src/main/java/com/mebigfatguy/fbcontrib/utils/BugType.java +++ b/src/main/java/com/mebigfatguy/fbcontrib/utils/BugType.java @@ -211,6 +211,7 @@ public enum BugType { ODN_ORPHANED_DOM_NODE, OI_OPTIONAL_ISSUES_CHECKING_REFERENCE, OI_OPTIONAL_ISSUES_PRIMITIVE_VARIANT_PREFERRED, + OI_OPTIONAL_ISSUES_ISPRESENT_PREFERRED, OI_OPTIONAL_ISSUES_USES_DELAYED_EXECUTION, OI_OPTIONAL_ISSUES_USES_IMMEDIATE_EXECUTION, OI_OPTIONAL_ISSUES_USES_ORELSEGET_WITH_NULL, diff --git a/src/samples/java/ex/OI_Sample.java b/src/samples/java/ex/OI_Sample.java index 8ce8fab7..35b0ef03 100644 --- a/src/samples/java/ex/OI_Sample.java +++ b/src/samples/java/ex/OI_Sample.java @@ -69,4 +69,8 @@ private Optional get(String name) { public String fpGet384(String parameterName, Supplier defaultValueSupplier) { return this.get(parameterName).orElseGet(defaultValueSupplier); } + + public boolean equalsToEmpty(Optional foo) { + return foo.equals(Optional.empty()); + } }