Skip to content

Commit

Permalink
Convert to Optional.isEmpty() - false positive #57 - simplified fix
Browse files Browse the repository at this point in the history
  • Loading branch information
markiewb committed Apr 3, 2015
1 parent a2bb685 commit bfa1a58
Showing 1 changed file with 1 addition and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public class AssignNullToOptional {

@TriggerPatterns({
@TriggerPattern(value = "$var1 = null", constraints = @ConstraintVariableType(variable = "$var1", type = "java.util.Optional")),
@TriggerPattern(value = "$class $var2 = null", constraints = @ConstraintVariableType(variable = "$var2", type = "java.util.Optional")),
@TriggerPattern(value = "$class $var2 = null", constraints = @ConstraintVariableType(variable = "$class", type = "java.util.Optional")),
})
@Hint(displayName = "#DN_AssignNull", description = "#DESC_AssignNull", category = "bugs", hintKind = Hint.Kind.INSPECTION, severity = Severity.ERROR)
@NbBundle.Messages("ERR_AssignNull=Replace with Optional.empty()")
Expand All @@ -74,18 +74,10 @@ public static ErrorDescription toFix(HintContext ctx) {
final Trees trees = ctx.getInfo().getTrees();

if (ctx.getVariables().containsKey("$var1")) {
String type = ctx.getInfo().getTypes().erasure(trees.getTypeMirror(ctx.getVariables().get("$var1"))).toString();

if ("java.util.Optional".equals(type)) {
result = "$var1 = Optional.empty()";
}
}
if (ctx.getVariables().containsKey("$var2")) {
String type = ctx.getInfo().getTypes().erasure(trees.getTypeMirror(ctx.getVariables().get("$var2"))).toString();

if ("java.util.Optional".equals(type)) {
result = "$class $var2 = Optional.empty()";
}
}
if (result != null) {
Fix fix = org.netbeans.spi.java.hints.JavaFixUtilities.rewriteFix(ctx, Bundle.ERR_AssignNull(), ctx.getPath(), result);
Expand Down

0 comments on commit bfa1a58

Please sign in to comment.