/*
 * Copyright 2005 Bas Leijdekkers
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.siyeh.ig.imports;

import com.intellij.psi.*;
import org.jetbrains.annotations.NotNull;

import java.util.*;

class StaticImportsAreUsedVisitor extends PsiRecursiveElementVisitor {

    private Collection<PsiImportStaticStatement> importStatements;

    StaticImportsAreUsedVisitor(PsiImportStaticStatement[] importStatements) {
        super();
        this.importStatements = new ArrayList(Arrays.asList(importStatements));
    }

    public void visitElement(PsiElement element) {
        if (importStatements.isEmpty()) {
            return;
        }
        super.visitElement(element);
    }

    public void visitReferenceElement(
            @NotNull PsiJavaCodeReferenceElement reference) {
        followReferenceToImport(reference);
        super.visitReferenceElement(reference);
    }

    private void followReferenceToImport(
            PsiJavaCodeReferenceElement reference) {
        if (reference.getQualifier()!=null) {
            //it's already fully qualified, so the import statement wasn't
            // responsible
            return;
        }
        final String qualifiedName = reference.getQualifiedName();
        if (qualifiedName == null) {
            return;
        }
        final PsiElement element = reference.resolve();
        if (!(element instanceof PsiMember)) {
            return;
        }
        final PsiMember member = (PsiMember)element;
        final PsiClass containingClass = member.getContainingClass();
        if (containingClass == null) {
            return;
        }
        for (PsiImportStaticStatement importStatement : importStatements) {
            final String referenceName = importStatement.getReferenceName();
            if (!qualifiedName.equals(referenceName)) {
                continue;
            }
            final PsiClass targetClass =
                    importStatement.resolveTargetClass();
            if (containingClass.equals(targetClass)) {
                importStatements.remove(importStatement);
                break;
            }
        }
    }

    public PsiImportStaticStatement[] getUnusedImportStaticStatements() {
        if (importStatements.isEmpty()) {
            return PsiImportStaticStatement.EMPTY_ARRAY;
        } else {
            return importStatements.toArray(
                    new PsiImportStaticStatement[importStatements.size()]);
        }
    }
}
@@ -706,17 +706,79 @@
 va.util.
-*
+ArrayList;%0Aimport java.util.Arrays;%0Aimport java.util.Collection
 ;%0A%0Aclass
@@ -2367,73 +2367,32 @@
 if (
-!qualifiedName.equals(referenceName)) %7B%0A                continue;
+referenceName == null) %7B
 %0A   
@@ -2396,30 +2396,24 @@
         
-    %7D%0A
         
     fina
@@ -2400,28 +2400,24 @@
             
-    
 final PsiCla
@@ -2433,16 +2433,20 @@
 Class =%0A
+    
         
@@ -2499,32 +2499,36 @@
 ();%0A            
+    
 if (containingCl
@@ -2550,24 +2550,193 @@
 etClass)) %7B%0A
+                    importStatements.remove(importStatement);%0A                    break;%0A                %7D%0A            %7D else if (qualifiedName.equals(referenceName)) %7B%0A
             

fix fir IDEADEV-4084