From d7a685bb47f7e7ddd911a07e880d93b58d49e075 Mon Sep 17 00:00:00 2001 From: Alexey Date: Fri, 3 Sep 2010 21:52:51 +0400 Subject: [PATCH] Updated version of the "comparingReference" sample plugin. --- .../comparingReferences.iml | 8 +- .../comparingReferences.ipr | 127 ++++++++++-------- .../{ => source}/META-INF/plugin.xml | 12 +- .../ComparingReferencesInspection.java | 68 +++------- .../ComparingReferencesProvider.java | 2 +- .../ComparingReferences.html | 5 +- 6 files changed, 104 insertions(+), 118 deletions(-) rename samples/comparingReferences/{ => source}/META-INF/plugin.xml (58%) diff --git a/samples/comparingReferences/comparingReferences.iml b/samples/comparingReferences/comparingReferences.iml index b33c9efa2fed..5000ad3ec83d 100644 --- a/samples/comparingReferences/comparingReferences.iml +++ b/samples/comparingReferences/comparingReferences.iml @@ -1,8 +1,7 @@ - - - - + + + @@ -10,7 +9,6 @@ - diff --git a/samples/comparingReferences/comparingReferences.ipr b/samples/comparingReferences/comparingReferences.ipr index 40d20bf1ffc3..dacd55a8c87b 100644 --- a/samples/comparingReferences/comparingReferences.ipr +++ b/samples/comparingReferences/comparingReferences.ipr @@ -1,16 +1,10 @@ - + - - + + - - + + + @@ -40,13 +40,19 @@ - - - diff --git a/samples/comparingReferences/META-INF/plugin.xml b/samples/comparingReferences/source/META-INF/plugin.xml similarity index 58% rename from samples/comparingReferences/META-INF/plugin.xml rename to samples/comparingReferences/source/META-INF/plugin.xml index 6b6582816d3d..50d5cf8cb315 100644 --- a/samples/comparingReferences/META-INF/plugin.xml +++ b/samples/comparingReferences/source/META-INF/plugin.xml @@ -1,12 +1,14 @@ - Comparing References Inspection - Inspection for (probably) inapporpriate use of equality relation operation + Inspection for (probably) inappropriate use of equality relation operation. 1.0 JetBrains - - - + + diff --git a/samples/comparingReferences/source/com/intellij/codeInspection/ComparingReferencesInspection.java b/samples/comparingReferences/source/com/intellij/codeInspection/ComparingReferencesInspection.java index 4c99465a2176..d900809cfb4f 100644 --- a/samples/comparingReferences/source/com/intellij/codeInspection/ComparingReferencesInspection.java +++ b/samples/comparingReferences/source/com/intellij/codeInspection/ComparingReferencesInspection.java @@ -31,7 +31,8 @@ public class ComparingReferencesInspection extends BaseJavaLocalInspectionTool { @NotNull public String getDisplayName() { - return InspectionsBundle.message("inspection.comparing.references.display.name"); + // return InspectionsBundle.message("inspection.comparing.references.display.name"); + return "'==' or '!=' instead of 'equals()'"; } @NotNull @@ -56,59 +57,34 @@ public class ComparingReferencesInspection extends BaseJavaLocalInspectionTool { return false; } - public ProblemDescriptor[] checkMethod(@NotNull PsiMethod method, @NotNull InspectionManager manager, boolean isOnTheFly) { - return analyzeCode(method.getBody(), manager); - } + @NotNull + @Override + public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder holder, boolean isOnTheFly) { + return new JavaElementVisitor() { - public ProblemDescriptor[] checkClass(@NotNull PsiClass aClass, @NotNull InspectionManager manager, boolean isOnTheFly) { - ArrayList problemList = null; - PsiClassInitializer[] initializers = aClass.getInitializers(); - for (PsiClassInitializer initializer : initializers) { - ProblemDescriptor[] problemDescriptors = analyzeCode(initializer, manager); - if (problemDescriptors != null) { - if (problemList == null) problemList = new ArrayList(); - problemList.addAll(Arrays.asList(problemDescriptors)); - } - } + @Override + public void visitReferenceExpression(PsiReferenceExpression psiReferenceExpression) { + } - return problemList == null - ? null - : problemList.toArray(new ProblemDescriptor[problemList.size()]); - } - - private ProblemDescriptor[] analyzeCode(PsiElement where, final InspectionManager manager) { - if (where == null) return null; - - final Ref> problemList = new Ref>(); - where.accept(new JavaRecursiveElementWalkingVisitor() { - @Override public void visitMethod(PsiMethod method) {} - - @Override public void visitClass(PsiClass aClass) {} @Override public void visitBinaryExpression(PsiBinaryExpression expression) { - super.visitBinaryExpression(expression); - IElementType opSign = expression.getOperationSign().getTokenType(); - if (opSign == JavaTokenType.EQEQ || opSign == JavaTokenType.NE) { - PsiExpression lOperand = expression.getLOperand(); - PsiExpression rOperand = expression.getROperand(); - if (rOperand == null || isNullLiteral(lOperand) || isNullLiteral(rOperand)) return; + super.visitBinaryExpression(expression); + IElementType opSign = expression.getOperationSign().getTokenType(); + if (opSign == JavaTokenType.EQEQ || opSign == JavaTokenType.NE) { + PsiExpression lOperand = expression.getLOperand(); + PsiExpression rOperand = expression.getROperand(); + if (rOperand == null || isNullLiteral(lOperand) || isNullLiteral(rOperand)) return; - PsiType lType = lOperand.getType(); - PsiType rType = rOperand.getType(); + PsiType lType = lOperand.getType(); + PsiType rType = rOperand.getType(); - if (isCheckedType(lType) || isCheckedType(rType)) { - if (problemList.get() == null) problemList.set(new ArrayList()); - problemList.get().add(manager.createProblemDescriptor(expression, DESCRIPTION_TEMPLATE, - myQuickFix, - ProblemHighlightType.GENERIC_ERROR_OR_WARNING)); + if (isCheckedType(lType) || isCheckedType(rType)) { + holder.registerProblem(expression, + DESCRIPTION_TEMPLATE, myQuickFix); + } } - } } - }); - - return problemList.get() == null - ? null - : problemList.get().toArray(new ProblemDescriptor[problemList.get().size()]); + }; } private static boolean isNullLiteral(PsiExpression expr) { diff --git a/samples/comparingReferences/source/com/intellij/codeInspection/ComparingReferencesProvider.java b/samples/comparingReferences/source/com/intellij/codeInspection/ComparingReferencesProvider.java index e8ea926de1ab..37f091454ba2 100644 --- a/samples/comparingReferences/source/com/intellij/codeInspection/ComparingReferencesProvider.java +++ b/samples/comparingReferences/source/com/intellij/codeInspection/ComparingReferencesProvider.java @@ -5,6 +5,6 @@ package com.intellij.codeInspection; */ public class ComparingReferencesProvider implements InspectionToolProvider { public Class[] getInspectionClasses() { - return new Class[] {ComparingReferencesInspection.class}; + return new Class[] { ComparingReferencesInspection.class}; } } diff --git a/samples/comparingReferences/source/inspectionDescriptions/ComparingReferences.html b/samples/comparingReferences/source/inspectionDescriptions/ComparingReferences.html index 461d513216ed..7c8ffd576c0f 100644 --- a/samples/comparingReferences/source/inspectionDescriptions/ComparingReferences.html +++ b/samples/comparingReferences/source/inspectionDescriptions/ComparingReferences.html @@ -1,6 +1,7 @@ -This inspection reports where == or != operations might be used between expressions of -reference types. Use the field below to specify classes to be catched as suspicous. Use semicolon as separator. +This inspection reports when the '==' or '!=' operator was used between expressions of +reference types.
+In the text field below, specify the semicolon separated list of classes to be considered as suspicious.