[GitHub] dtrebbien commented on a change in pull request #314: Add a new fix to BoxedIdentityComparison

2017-12-05 Thread GitBox
dtrebbien commented on a change in pull request #314: Add a new fix to 
BoxedIdentityComparison
URL: https://github.com/apache/incubator-netbeans/pull/314#discussion_r155105638
 
 

 ##
 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")
 
 Review comment:
   Changed the local var name to `baseReceiverString`.


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


[GitHub] dtrebbien commented on a change in pull request #314: Add a new fix to BoxedIdentityComparison

2017-12-05 Thread GitBox
dtrebbien commented on a change in pull request #314: Add a new fix to 
BoxedIdentityComparison
URL: https://github.com/apache/incubator-netbeans/pull/314#discussion_r155105474
 
 

 ##
 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 + ")");
+this.argString = (lhsIsReceiver ? bt.getRightOperand() : 
bt.getLeftOperand()).toString();
+this.invert = (bt.getKind() == Tree.Kind.NOT_EQUAL_TO);
+}
+
+@Override
+protected String getText() {
+return (invert ? FIX_NegateCallEquals(receiverString, argString) : 
FIX_CallEquals(receiverString, argString));
+}
+
+@Override
+protected void performRewrite(TransformationContext ctx) throws 
Exception {
+TreePath p = ctx.getPath();
+if (p.getLeaf().getKind() != Tree.Kind.EQUAL_TO  && 
p.getLeaf().getKind() != Tree.Kind.NOT_EQUAL_TO) {
+// TODO - report ?
+return;
+}
+BinaryTree bt = (BinaryTree)p.getLeaf();
+TreeMaker mk = ctx.getWorkingCopy().getTreeMaker();
+ExpressionTree receiver = lhsIsReceiver ? bt.getLeftOperand() : 
bt.getRightOperand();
+if (!JavaFixUtilities.isPrimary(receiver)) {
+receiver = mk.Parenthesized(receiver);
+  

[GitHub] dtrebbien commented on a change in pull request #314: Add a new fix to BoxedIdentityComparison

2017-12-05 Thread GitBox
dtrebbien commented on a change in pull request #314: Add a new fix to 
BoxedIdentityComparison
URL: https://github.com/apache/incubator-netbeans/pull/314#discussion_r155105454
 
 

 ##
 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 + ")");
+this.argString = (lhsIsReceiver ? bt.getRightOperand() : 
bt.getLeftOperand()).toString();
+this.invert = (bt.getKind() == Tree.Kind.NOT_EQUAL_TO);
+}
+
+@Override
+protected String getText() {
+return (invert ? FIX_NegateCallEquals(receiverString, argString) : 
FIX_CallEquals(receiverString, argString));
+}
+
+@Override
+protected void performRewrite(TransformationContext ctx) throws 
Exception {
+TreePath p = ctx.getPath();
+if (p.getLeaf().getKind() != Tree.Kind.EQUAL_TO  && 
p.getLeaf().getKind() != Tree.Kind.NOT_EQUAL_TO) {
+// TODO - report ?
+return;
+}
+BinaryTree bt = (BinaryTree)p.getLeaf();
+TreeMaker mk = ctx.getWorkingCopy().getTreeMaker();
+ExpressionTree receiver = lhsIsReceiver ? bt.getLeftOperand() : 
bt.getRightOperand();
+if (!JavaFixUtilities.isPrimary(receiver)) {
+receiver = mk.Parenthesized(receiver);
+  

[GitHub] dtrebbien commented on a change in pull request #314: Add a new fix to BoxedIdentityComparison

2017-12-05 Thread GitBox
dtrebbien commented on a change in pull request #314: Add a new fix to 
BoxedIdentityComparison
URL: https://github.com/apache/incubator-netbeans/pull/314#discussion_r155105495
 
 

 ##
 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:
   Done


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


[GitHub] dtrebbien commented on a change in pull request #314: Add a new fix to BoxedIdentityComparison

2017-12-05 Thread GitBox
dtrebbien commented on a change in pull request #314: Add a new fix to 
BoxedIdentityComparison
URL: https://github.com/apache/incubator-netbeans/pull/314#discussion_r155104942
 
 

 ##
 File path: spi.java.hints/src/org/netbeans/spi/java/hints/JavaFixUtilities.java
 ##
 @@ -1394,7 +1394,18 @@ private void rewrite(Tree from, Tree to) {
 OPERATOR_PRIORITIES.put(Kind.UNSIGNED_RIGHT_SHIFT_ASSIGNMENT, 15);
 OPERATOR_PRIORITIES.put(Kind.XOR_ASSIGNMENT, 15);
 }
-
+
+/**
+ * Checks whether {@code tree} can be used in places where a Primary is
+ * required (for instance, as the receiver expression of a method 
invocation).
+ * @param tree the tree to check
+ * @return {@code true} iff {@code tree} can be used where a Primary is
+ * required.
+ */
+public static boolean isPrimary(@NonNull Tree tree) {
 
 Review comment:
   I created PR #320 for the API change.


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