/*
 * Copyright 2003-2017 Dave Griffith, 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.initialization;

import com.intellij.codeInspection.ProblemDescriptor;
import com.intellij.openapi.project.Project;
import com.intellij.psi.*;
import com.intellij.util.IncorrectOperationException;
import com.siyeh.InspectionGadgetsBundle;
import com.siyeh.ig.InspectionGadgetsFix;
import com.siyeh.ig.fixes.MakeClassFinalFix;
import com.siyeh.ig.psiutils.ClassUtils;
import com.siyeh.ig.psiutils.MethodUtils;
import org.jetbrains.annotations.NotNull;

public class OverridableMethodCallDuringObjectConstructionInspection extends OverridableMethodCallDuringObjectConstructionInspectionBase {

  @Override
  @NotNull
  protected InspectionGadgetsFix[] buildFixes(Object... infos) {
    final PsiMethodCallExpression methodCallExpression = (PsiMethodCallExpression)infos[0];
    final PsiClass callClass = ClassUtils.getContainingClass(methodCallExpression);
    final PsiMethod method = methodCallExpression.resolveMethod();
    if (method == null || MethodUtils.isOverriddenInHierarchy(method, callClass)) {
      return InspectionGadgetsFix.EMPTY_ARRAY;
    }
    return new InspectionGadgetsFix[]{
      new MakeClassFinalFix(callClass),
      new MakeMethodFinalFix(method.getName())
    };
  }

  private static class MakeMethodFinalFix extends InspectionGadgetsFix {

    private final String methodName;

    MakeMethodFinalFix(String methodName) {
      this.methodName = methodName;
    }

    @Override
    @NotNull
    public String getName() {
      return InspectionGadgetsBundle.message("make.method.final.fix.name", methodName);
    }

    @NotNull
    @Override
    public String getFamilyName() {
      return "Make method final";
    }

    @Override
    protected void doFix(Project project, ProblemDescriptor descriptor) throws IncorrectOperationException {
      final PsiElement methodName = descriptor.getPsiElement();
      final PsiElement methodExpression = methodName.getParent();
      final PsiMethodCallExpression methodCall = (PsiMethodCallExpression)methodExpression.getParent();
      final PsiMethod method = methodCall.resolveMethod();
      if (method == null) {
        return;
      }
      final PsiModifierList modifierList = method.getModifierList();
      modifierList.setModifierProperty(PsiModifier.FINAL, true);
    }
  }
}
@@ -677,159 +677,215 @@
 eIns
-pection.ProblemDescriptor;%0Aimport com.intellij.openapi.project.Project;%0Aimport com.intellij.psi.*;%0Aimport com.intellij.util.IncorrectOperationException
+ight.FileModificationService;%0Aimport com.intellij.codeInspection.ProblemDescriptor;%0Aimport com.intellij.openapi.application.WriteAction;%0Aimport com.intellij.openapi.project.Project;%0Aimport com.intellij.psi.*
 ;%0Aim
@@ -2323,21 +2323,23 @@
  method 
+'
 final
+'
 %22;%0A    %7D
@@ -2429,43 +2429,8 @@
 tor)
- throws IncorrectOperationException
  %7B%0A 
@@ -2749,78 +2749,140 @@
 null
-) %7B%0A        return;%0A      %7D%0A      final PsiModifierList modifierList =
+ %7C%7C !FileModificationService.getInstance().preparePsiElementsForWrite(method)) %7B%0A        return;%0A      %7D%0A      WriteAction.run(() -%3E
  met
@@ -2906,28 +2906,8 @@
 st()
-;%0A      modifierList
 .set
@@ -2947,16 +2947,100 @@
 L, true)
+);%0A    %7D%0A%0A    @Override%0A    public boolean startInWriteAction() %7B%0A      return false
 ;%0A    %7D%0A

handle own prepareForWrite() and write action (IDEA-177140)