/*
 * Copyright 2000-2010 JetBrains s.r.o.
 *
 * 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.inheritance;

import com.intellij.codeInsight.FileModificationService;
import com.intellij.codeInsight.intention.IntentionAction;
import com.intellij.codeInsight.intention.QuickFixFactory;
import com.intellij.codeInspection.ProblemDescriptor;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.application.ModalityState;
import com.intellij.openapi.application.Result;
import com.intellij.openapi.command.WriteCommandAction;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.progress.ProgressIndicator;
import com.intellij.openapi.progress.ProgressManager;
import com.intellij.openapi.progress.Task;
import com.intellij.openapi.project.Project;
import com.intellij.psi.*;
import com.intellij.psi.impl.DebugUtil;
import com.intellij.psi.search.searches.ReferencesSearch;
import com.intellij.psi.util.InheritanceUtil;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.ui.GuiUtils;
import com.intellij.util.IncorrectOperationException;
import com.intellij.util.Query;
import com.siyeh.InspectionGadgetsBundle;
import com.siyeh.ig.InspectionGadgetsFix;
import com.siyeh.ig.psiutils.ClassUtils;
import org.jetbrains.annotations.NotNull;

import java.lang.reflect.InvocationTargetException;

/**
 * User: cdr
 */
class StaticInheritanceFix extends InspectionGadgetsFix {
  private static final Logger LOG = Logger.getInstance("#com.siyeh.ig.inheritance.StaticInheritanceFix");
  private final boolean myReplaceInWholeProject;

  StaticInheritanceFix(boolean replaceInWholeProject) {
    myReplaceInWholeProject = replaceInWholeProject;
  }

  @Override
  @NotNull
  public String getName() {
    String scope =
      myReplaceInWholeProject ? InspectionGadgetsBundle.message("the.whole.project") : InspectionGadgetsBundle.message("this.class");
    return InspectionGadgetsBundle.message("static.inheritance.replace.quickfix", scope);
  }

  @NotNull
  @Override
  public String getFamilyName() {
    return "Replace inheritance with qualified reference";
  }

  @Override
  public void doFix(final Project project, final ProblemDescriptor descriptor) throws IncorrectOperationException {
    ApplicationManager.getApplication().invokeLater(new Runnable() {
      @Override
      public void run() {
        dodoFix(project, descriptor);
      }
    }, ModalityState.NON_MODAL, project.getDisposed());
  }

  private void dodoFix(final Project project, ProblemDescriptor descriptor) throws IncorrectOperationException {
    final PsiJavaCodeReferenceElement referenceElement = (PsiJavaCodeReferenceElement)descriptor.getPsiElement();
    final PsiClass iface = (PsiClass)referenceElement.resolve();
    assert iface != null;
    final PsiField[] allFields = iface.getAllFields();

    final PsiClass implementingClass = ClassUtils.getContainingClass(referenceElement);
    final PsiManager manager = referenceElement.getManager();
    assert implementingClass != null;
    final PsiFile file = implementingClass.getContainingFile();

    ProgressManager.getInstance().run(new Task.Modal(project, "Replacing usages of " + iface.getName(), false) {

      @Override
      public void run(@NotNull ProgressIndicator indicator) {
        for (final PsiField field : allFields) {
          final Query<PsiReference> search = ReferencesSearch.search(field, implementingClass.getUseScope(), false);
          for (PsiReference reference : search) {
            if (!(reference instanceof PsiReferenceExpression)) {
              continue;
            }
            final PsiReferenceExpression referenceExpression = (PsiReferenceExpression)reference;
            if (!myReplaceInWholeProject) {
              PsiClass aClass = PsiTreeUtil.getParentOfType(referenceExpression, PsiClass.class);
              boolean isInheritor = false;
              while (aClass != null) {
                isInheritor = InheritanceUtil.isInheritorOrSelf(aClass, implementingClass, true);
                if (isInheritor) break;
                aClass = PsiTreeUtil.getParentOfType(aClass, PsiClass.class);
              }
              if (!isInheritor) continue;
            }
            final Runnable runnable = new Runnable() {
              @Override
              public void run() {
                if (!FileModificationService.getInstance().preparePsiElementsForWrite(referenceExpression)) {
                  return;
                }
                final PsiElementFactory elementFactory = JavaPsiFacade.getInstance(manager.getProject()).getElementFactory();
                final PsiReferenceExpression qualified =
                  (PsiReferenceExpression)elementFactory
                    .createExpressionFromText("xxx." + referenceExpression.getText(), referenceExpression);
                final PsiReferenceExpression newReference = (PsiReferenceExpression)referenceExpression.replace(qualified);
                final PsiReferenceExpression qualifier = (PsiReferenceExpression)newReference.getQualifierExpression();
                assert qualifier != null : DebugUtil.psiToString(newReference, false);
                final PsiClass containingClass = field.getContainingClass();
                qualifier.bindToElement(containingClass);
              }
            };
            invokeWriteAction(runnable, file);
          }
        }
        final Runnable runnable = new Runnable() {
          @Override
          public void run() {
            PsiClassType classType = JavaPsiFacade.getInstance(project).getElementFactory().createType(iface);
            IntentionAction fix = QuickFixFactory.getInstance().createExtendsListFix(implementingClass, classType, false);
            fix.invoke(project, null, file);
          }
        };
        invokeWriteAction(runnable, file);
      }
    });
  }

  private static void invokeWriteAction(final Runnable runnable, final PsiFile file) {
    try {
      GuiUtils.runOrInvokeAndWait(new Runnable() {
        @Override
        public void run() {
          new WriteCommandAction(file.getProject(), file) {
            @Override
            protected void run(Result result) throws Throwable {
              runnable.run();
            }
          }.execute();
        }
      });
    }
    catch (InvocationTargetException e) {
      LOG.error(e);
    }
    catch (InterruptedException e) {
      LOG.error(e);
    }
  }
}

@@ -17,17 +17,17 @@
 2000-201
-0
+3
  JetBrai
@@ -1322,16 +1322,61 @@
 roject;%0A
+import com.intellij.openapi.util.Computable;%0A
 import c
@@ -4312,24 +4312,272 @@
 eProject) %7B%0A
+              boolean isInheritor =%0A              ApplicationManager.getApplication().runReadAction(new Computable%3CBoolean%3E() %7B%0A                @Override%0A                public Boolean compute() %7B%0A                  boolean isInheritor = false;%0A    
             
@@ -4674,43 +4674,8 @@
     
-      boolean isInheritor = false;%0A
     
@@ -4680,20 +4680,16 @@
         
-    
 while (a
@@ -4701,24 +4701,28 @@
  != null) %7B%0A
+    
             
@@ -4803,24 +4803,28 @@
 ass, true);%0A
+    
             
@@ -4867,16 +4867,20 @@
         
+    
 aClass =
@@ -4939,33 +4939,111 @@
 ;%0A              
-%7D
+    %7D%0A                  return isInheritor;%0A                %7D%0A              %7D);
 %0A              i

read action