mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-02-06 09:36:56 +07:00
108 lines
3.9 KiB
Java
108 lines
3.9 KiB
Java
package com.intellij.codeInspection.equalsAndHashcode;
|
|
|
|
import com.intellij.analysis.AnalysisScope;
|
|
import com.intellij.codeInspection.ProblemDescriptor;
|
|
import com.intellij.codeInspection.ProblemHighlightType;
|
|
import com.intellij.codeInspection.ex.DescriptorProviderInspection;
|
|
import com.intellij.codeInspection.ex.InspectionManagerEx;
|
|
import com.intellij.codeInspection.ex.JobDescriptor;
|
|
import com.intellij.psi.*;
|
|
import com.intellij.psi.util.MethodSignatureUtil;
|
|
|
|
/**
|
|
* @author max
|
|
*/
|
|
public class EqualsAndHashcode extends DescriptorProviderInspection {
|
|
private static final JobDescriptor JOB_DESCRIPTOR = new JobDescriptor("Searching for equals() and hashCode() in ");
|
|
|
|
private PsiMethod myHashCode;
|
|
private PsiMethod myEquals;
|
|
|
|
public EqualsAndHashcode() {
|
|
}
|
|
|
|
public void initialize(InspectionManagerEx manager) {
|
|
super.initialize(manager);
|
|
myHashCode = null;
|
|
myEquals = null;
|
|
PsiManager psiManager = PsiManager.getInstance(getManager().getProject());
|
|
PsiClass psiObjectClass = psiManager.findClass("java.lang.Object");
|
|
PsiMethod[] methods = psiObjectClass.getMethods();
|
|
for (int i = 0; i < methods.length; i++) {
|
|
PsiMethod method = methods[i];
|
|
if ("equals".equals(method.getName())) {
|
|
myEquals = method;
|
|
} else if ("hashCode".equals(method.getName())) {
|
|
myHashCode = method;
|
|
}
|
|
}
|
|
}
|
|
|
|
public void runInspection(AnalysisScope scope) {
|
|
JOB_DESCRIPTOR.setTotalAmount(scope.getFileCount());
|
|
|
|
scope.accept(new PsiElementVisitor() {
|
|
public void visitFile(PsiFile file) {
|
|
if (file instanceof PsiJavaFile) {
|
|
getManager().incrementJobDoneAmount(JOB_DESCRIPTOR, file.getVirtualFile().getPresentableUrl());
|
|
super.visitFile(file);
|
|
}
|
|
}
|
|
|
|
public void visitReferenceExpression(PsiReferenceExpression expression) {
|
|
visitElement(expression);
|
|
}
|
|
|
|
public void visitElement(PsiElement element) {
|
|
PsiElement[] children = element.getChildren();
|
|
for (int i = 0; i < children.length; i++) {
|
|
PsiElement child = children[i];
|
|
child.accept(this);
|
|
}
|
|
}
|
|
|
|
public void visitClass(PsiClass aClass) {
|
|
super.visitClass(aClass);
|
|
|
|
boolean hasEquals = false;
|
|
boolean hasHashCode = false;
|
|
PsiMethod[] methods = aClass.getMethods();
|
|
for (int i = 0; i < methods.length; i++) {
|
|
PsiMethod method = methods[i];
|
|
if (MethodSignatureUtil.areSignaturesEqual(method, myEquals)) {
|
|
hasEquals = true;
|
|
} else if (MethodSignatureUtil.areSignaturesEqual(method, myHashCode)) {
|
|
hasHashCode = true;
|
|
}
|
|
}
|
|
|
|
if (hasEquals != hasHashCode) {
|
|
addProblemElement(getManager().getRefManager().getReference(aClass),
|
|
new ProblemDescriptor[]{getManager().createProblemDescriptor(aClass,
|
|
hasEquals
|
|
? "Class has <code>equals()</code> defined but " +
|
|
"does not define <code>hashCode()</code>."
|
|
: "Class has <code>hashCode()</code> defined but " +
|
|
"does not define <code>equals()</code>.", null,ProblemHighlightType.GENERIC_ERROR_OR_WARNING)});
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
public JobDescriptor[] getJobDescriptors() {
|
|
return new JobDescriptor[] {JOB_DESCRIPTOR};
|
|
}
|
|
|
|
public String getDisplayName() {
|
|
return "equals() and hashCode() not paired";
|
|
}
|
|
|
|
public String getGroupDisplayName() {
|
|
return "";
|
|
}
|
|
|
|
public String getShortName() {
|
|
return "EqualsAndHashcode";
|
|
}
|
|
}
|