highlight globally unused groovy methods (IDEA-75803)
authorpeter <peter@jetbrains.com>
Mon, 30 Jan 2012 19:21:20 +0000 (20:21 +0100)
committerpeter <peter@jetbrains.com>
Tue, 31 Jan 2012 17:30:25 +0000 (18:30 +0100)
java/java-impl/src/com/intellij/codeInsight/daemon/impl/PostHighlightingPass.java
plugins/groovy/src/org/jetbrains/plugins/groovy/codeInspection/local/GroovyPostHighlightingPass.java

index 74f7f6c5d949cd57bbd9eab91db98f65e4422d20..2c40a1aa2c7899ca0c8b3af4c6d50c0aee4e007b 100644 (file)
@@ -524,13 +524,11 @@ public class PostHighlightingPass extends TextEditorHighlightingPass {
 
   @Nullable
   private HighlightInfo processMethod(final PsiMethod method, ProgressIndicator progress, GlobalUsageHelper helper) {
-    boolean isPrivate = method.hasModifierProperty(PsiModifier.PRIVATE);
-    PsiClass containingClass = method.getContainingClass();
-    if (isMethodReferenced(method, progress, isPrivate, containingClass, helper)) return null;
+    if (isMethodReferenced(method, progress, helper)) return null;
     HighlightInfoType highlightInfoType;
     HighlightDisplayKey highlightDisplayKey;
     String key;
-    if (isPrivate) {
+    if (method.hasModifierProperty(PsiModifier.PRIVATE)) {
       highlightInfoType = HighlightInfoType.UNUSED_SYMBOL;
       highlightDisplayKey = myUnusedSymbolKey;
       key = method.isConstructor() ? "private.constructor.is.not.used" : "private.method.is.not.used";
@@ -552,6 +550,7 @@ public class PostHighlightingPass extends TextEditorHighlightingPass {
         return true;
       }
     });
+    PsiClass containingClass = method.getContainingClass();
     if (method.getReturnType() != null || containingClass != null && Comparing.strEqual(containingClass.getName(), method.getName())) {
       //ignore methods with deleted return types as they are always marked as unused without any reason
       ChangeSignatureGestureDetector.getInstance(myProject).dismissForElement(method);
@@ -559,13 +558,13 @@ public class PostHighlightingPass extends TextEditorHighlightingPass {
     return highlightInfo;
   }
 
-  private static boolean isMethodReferenced(PsiMethod method,
+  public static boolean isMethodReferenced(PsiMethod method,
                                             ProgressIndicator progress,
-                                            boolean aPrivate,
-                                            PsiClass containingClass,
                                             GlobalUsageHelper helper) {
     if (helper.isLocallyUsed(method)) return true;
 
+    boolean aPrivate = method.hasModifierProperty(PsiModifier.PRIVATE);
+    PsiClass containingClass = method.getContainingClass();
     if (HighlightMethodUtil.isSerializationRelatedMethod(method, containingClass)) return true;
     if (aPrivate) {
       if (isIntentionalPrivateConstructor(method, containingClass)) {
@@ -628,8 +627,7 @@ public class PostHighlightingPass extends TextEditorHighlightingPass {
     if (containingClass == null) return true;
     final PsiMethod valuesMethod = containingClass.getValuesMethod();
     if (valuesMethod == null) return true;
-    boolean isPrivate = valuesMethod.hasModifierProperty(PsiModifier.PRIVATE);
-    return isMethodReferenced(valuesMethod, progress, isPrivate, containingClass, helper);
+    return isMethodReferenced(valuesMethod, progress, helper);
   }
 
   private static boolean canBeReferencedViaWeirdNames(PsiMember member) {
index 5f90199da46ccbc2dbd572b6725466555162377e..4dd5726262f3c307a5e5c1ac9ced643aa46f1c78 100644 (file)
@@ -48,12 +48,14 @@ import org.jetbrains.annotations.NotNull;
 import org.jetbrains.plugins.groovy.codeInspection.GroovyInspectionBundle;
 import org.jetbrains.plugins.groovy.codeInspection.GroovyUnusedDeclarationInspection;
 import org.jetbrains.plugins.groovy.lang.editor.GroovyImportOptimizer;
+import org.jetbrains.plugins.groovy.lang.lexer.GroovyTokenTypes;
 import org.jetbrains.plugins.groovy.lang.psi.GrNamedElement;
 import org.jetbrains.plugins.groovy.lang.psi.GrReferenceElement;
 import org.jetbrains.plugins.groovy.lang.psi.GroovyFile;
 import org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement;
 import org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult;
 import org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition;
+import org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod;
 import org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement;
 
 import java.util.ArrayList;
@@ -110,10 +112,19 @@ public class GroovyPostHighlightingPass extends TextEditorHighlightingPass {
 
         if (deadCodeEnabled && element instanceof GrNamedElement && !PostHighlightingPass.isImplicitUsage((GrNamedElement)element, progress)) {
           PsiElement nameId = ((GrNamedElement)element).getNameIdentifierGroovy();
-          String name = ((GrNamedElement)element).getName();
-          if (element instanceof GrTypeDefinition && PostHighlightingPass.isClassUsed((GrTypeDefinition)element, progress, usageHelper)) {
-            unusedDeclarations.add(
-              PostHighlightingPass.createUnusedSymbolInfo(nameId, "Class " + name + " is unused", HighlightInfoType.UNUSED_SYMBOL));
+          if (nameId.getNode().getElementType() == GroovyTokenTypes.mIDENT) {
+            String name = ((GrNamedElement)element).getName();
+            if (element instanceof GrTypeDefinition && PostHighlightingPass.isClassUsed((GrTypeDefinition)element, progress, usageHelper)) {
+              unusedDeclarations.add(
+                PostHighlightingPass.createUnusedSymbolInfo(nameId, "Class " + name + " is unused", HighlightInfoType.UNUSED_SYMBOL));
+            }
+            else if (element instanceof GrMethod) {
+              GrMethod method = (GrMethod)element;
+              if (!PostHighlightingPass.isMethodReferenced(method, progress, usageHelper)) {
+                unusedDeclarations.add(
+                  PostHighlightingPass.createUnusedSymbolInfo(nameId, (method.isConstructor() ? "Constructor" : "Method") +" " + name + " is unused", HighlightInfoType.UNUSED_SYMBOL));
+              }
+            }
           }
         }