sdedic commented on a change in pull request #314: Add a new fix to 
BoxedIdentityComparison
URL: https://github.com/apache/incubator-netbeans/pull/314#discussion_r154865421
 
 

 ##########
 File path: 
java.hints/src/org/netbeans/modules/java/hints/bugs/BoxedIdentityComparison.java
 ##########
 @@ -82,31 +85,94 @@
 
     })
     public static ErrorDescription wrapperComparisonUsingIdentity(HintContext 
ctx) {
-        TreePath logExprPath = ctx.getPath();
-        BinaryTree bt = (BinaryTree)logExprPath.getLeaf();
+        TreePath comparisonExprPath = ctx.getPath();
+        BinaryTree bt = (BinaryTree)comparisonExprPath.getLeaf();
         TreePath wrapperPath = ctx.getVariables().get("$x"); // NOI18N
-        Tree otherOperand = bt.getRightOperand();
+        assert bt.getLeftOperand() == wrapperPath.getLeaf();
+        TreePath otherPath = ctx.getVariables().get("$y"); // NOI18N
+        assert bt.getRightOperand() == otherPath.getLeaf();
+
+        CompilationInfo info = ctx.getInfo();
+        Trees trees = info.getTrees();
 
         // JLS 15.21; if the other type is primitive, the comparison is correct
-        TypeMirror t = ctx.getInfo().getTrees().getTypeMirror(new 
TreePath(logExprPath, otherOperand));
+        TypeMirror t = trees.getTypeMirror(otherPath);
         if (t == null || t.getKind() != TypeKind.DECLARED) {
             return null;
         }
-        t = ctx.getInfo().getTrees().getTypeMirror(wrapperPath);
+        t = trees.getTypeMirror(wrapperPath);
         // primitive type is assignable to the wrapper, so it will trigger the 
hint
         if (t == null || t.getKind() != TypeKind.DECLARED) {
             return null;
         }
+
         final Fix fix;
-        
-        if 
(ctx.getInfo().getSourceVersion().compareTo(SourceVersion.RELEASE_7) < 0) {
-           fix = null;
+        if (NPECheck.isSafeToDereference(info, wrapperPath)) {
+            fix = new CallEqualsFix(info, comparisonExprPath, 
true).toEditorFix();
+        } else if (NPECheck.isSafeToDereference(info, otherPath)) {
+            fix = new CallEqualsFix(info, comparisonExprPath, 
false).toEditorFix();
+        } else if (info.getSourceVersion().compareTo(SourceVersion.RELEASE_7) 
< 0) {
+            fix = null;
         } else {
-            fix = new NullSafeEqualsFix(TreePathHandle.create(logExprPath, 
ctx.getInfo())).toEditorFix();
+            fix = new 
NullSafeEqualsFix(TreePathHandle.create(comparisonExprPath, 
info)).toEditorFix();
+        }
+
+        return ErrorDescriptionFactory.forTree(ctx, comparisonExprPath,
+                
TEXT_BoxedValueIdentityComparison(info.getTypeUtilities().getTypeName(t)), fix);
+    }
+
+    @NbBundle.Messages({
+        "# {0} - stringified receiver expression tree",
+        "# {1} - stringified equals() arg expression tree",
+        "FIX_CallEquals=Replace with ''{0}.equals({1})''",
+        "# {0} - stringified receiver expression tree",
+        "# {1} - stringified equals() arg expression tree",
+        "FIX_NegateCallEquals=Replace with ''!{0}.equals({1})''",
+    })
+    private static class CallEqualsFix extends JavaFix {
+
+        private final boolean lhsIsReceiver;
+        private final String receiverString;
+        private final String argString;
+        private final boolean invert;
+
+        public CallEqualsFix(CompilationInfo info, TreePath 
comparisonExprPath, boolean lhsIsReceiver) {
+            super(info, comparisonExprPath);
+            this.lhsIsReceiver = lhsIsReceiver;
+            BinaryTree bt = (BinaryTree) comparisonExprPath.getLeaf();
+            ExpressionTree receiver = (lhsIsReceiver ? bt.getLeftOperand() : 
bt.getRightOperand());
+            @SuppressWarnings("LocalVariableHidesMemberVariable")
+            String receiverString = receiver.toString();
+            this.receiverString = (JavaFixUtilities.isPrimary(receiver) ? 
receiverString : "(" + receiverString + ")");
 
 Review comment:
   add // NOI18N

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to