[java-inspections] UseHashCodeMethodInspection: fix inspection name, support boxed Long type

IDEA-338114, IDEA-339366

GitOrigin-RevId: 5e51384d160e29f3c81b99c9c50260cb883d9c46
This commit is contained in:
Tagir Valeev
2023-12-01 15:37:22 +01:00
committed by intellij-monorepo-bot
parent 13239eecf6
commit b7d4e8a3b2
6 changed files with 42 additions and 4 deletions

View File

@@ -6,6 +6,7 @@ import com.intellij.modcommand.ModPsiUpdater;
import com.intellij.modcommand.PsiUpdateModCommandQuickFix;
import com.intellij.openapi.project.Project;
import com.intellij.psi.*;
import com.intellij.psi.util.PsiPrecedenceUtil;
import com.intellij.psi.util.PsiUtil;
import com.siyeh.ig.psiutils.CommentTracker;
import com.siyeh.ig.psiutils.EquivalenceChecker;
@@ -41,12 +42,11 @@ public class UseHashCodeMethodInspection extends AbstractBaseJavaLocalInspection
PsiJavaToken operationSign = binaryExpression.getOperationSign();
if (operationSign.getTokenType() != JavaTokenType.XOR) return null;
PsiExpression leftOperand = PsiUtil.skipParenthesizedExprDown(binaryExpression.getLOperand());
PsiExpression rightOperand = PsiUtil.skipParenthesizedExprDown(binaryExpression.getROperand());
if (leftOperand == null || rightOperand == null) return null;
if (!PsiTypes.longType().equals(leftOperand.getType())) return null;
if (!PsiTypes.longType().equals(PsiPrimitiveType.getOptionallyUnboxedType(leftOperand.getType()))) return null;
if (isXorShift(leftOperand, rightOperand)) return leftOperand;
if (isXorShift(rightOperand, leftOperand)) return rightOperand;
@@ -79,8 +79,12 @@ public class UseHashCodeMethodInspection extends AbstractBaseJavaLocalInspection
PsiTypeCastExpression element = (PsiTypeCastExpression)startElement;
PsiExpression operand = getHashCodeOperand(element);
if (operand != null) {
PsiType type = operand.getType();
CommentTracker ct = new CommentTracker();
ct.replace(element, "Long.hashCode(" + ct.text(operand) + ")");
String call = PsiTypes.longType().equals(type)
? "Long.hashCode(" + ct.text(operand) + ")"
: ct.text(operand, PsiPrecedenceUtil.METHOD_CALL_PRECEDENCE) + ".hashCode()";
ct.replace(element, call);
}
}
}

View File

@@ -1742,7 +1742,7 @@
<localInspection groupPathKey="group.path.names.java.language.level.specific.issues.and.migration.aids" language="JAVA" shortName="UseHashCodeMethodInspection"
groupKey="group.names.language.level.specific.issues.and.migration.aids8" groupBundle="messages.InspectionsBundle" enabledByDefault="true" level="WARNING"
implementationClass="com.intellij.codeInspection.UseHashCodeMethodInspection"
key="inspection.convert.2.streamapi.display.name" bundle="messages.JavaBundle"/>
key="inspection.name.can.be.replaced.with.long.hashcode" bundle="messages.JavaAnalysisBundle"/>
<localInspection groupPath="Java" language="JAVA" shortName="FuseStreamOperations"
bundle="messages.JavaBundle" key="inspection.fuse.stream.operations.display.name"
groupKey="group.names.code.style.issues" groupBundle="messages.InspectionsBundle" enabledByDefault="true" level="WARNING"

View File

@@ -0,0 +1,8 @@
// "Replace with 'Long.hashCode()'" "true-preview"
public class Test {
Long var = 1234567890123456789L;
public void testMethod() {
int result = var.hashCode();
}
}

View File

@@ -0,0 +1,9 @@
// "Replace with 'Long.hashCode()'" "true-preview"
public class Test {
Long var = 1234567890123456789L;
Long var1 = 1234567890123456784L;
public void testMethod(boolean f) {
int result = (f ? var : var1).hashCode();
}
}

View File

@@ -0,0 +1,8 @@
// "Replace with 'Long.hashCode()'" "true-preview"
public class Test {
Long var = 1234567890123456789L;
public void testMethod() {
int result = (int<caret>)(var ^ (var >>> /*shift amount*/ 32));
}
}

View File

@@ -0,0 +1,9 @@
// "Replace with 'Long.hashCode()'" "true-preview"
public class Test {
Long var = 1234567890123456789L;
Long var1 = 1234567890123456784L;
public void testMethod(boolean f) {
int result = (int<caret>)((f ? var : var1) ^ ((f ? var : var1) >>> /*shift amount*/ 32));
}
}