/* * Copyright 2000-2015 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 org.jetbrains.plugins.groovy.refactoring.changeSignature; import com.intellij.openapi.application.ApplicationManager; import com.intellij.openapi.diagnostic.Logger; import com.intellij.openapi.project.Project; import com.intellij.openapi.util.Ref; import com.intellij.psi.PsiElement; import com.intellij.psi.PsiMethod; import com.intellij.refactoring.changeSignature.ChangeSignatureProcessorBase; import com.intellij.refactoring.changeSignature.ChangeSignatureViewDescriptor; import com.intellij.refactoring.rename.RenameUtil; import com.intellij.refactoring.ui.ConflictsDialog; import com.intellij.usageView.UsageInfo; import com.intellij.usageView.UsageViewDescriptor; import com.intellij.util.containers.HashSet; import com.intellij.util.containers.MultiMap; import org.jetbrains.annotations.NotNull; import java.util.Arrays; import java.util.Set; /** * @author Maxim.Medvedev */ public class GrChangeSignatureProcessor extends ChangeSignatureProcessorBase { public static final Logger LOG = Logger.getInstance("#org.jetbrains.plugins.groovy.refactoring.changeSignature.GrChangeSignatureProcessor"); public GrChangeSignatureProcessor(Project project, GrChangeInfoImpl changeInfo) { super(project, changeInfo); } @Override public GrChangeInfoImpl getChangeInfo() { return (GrChangeInfoImpl)super.getChangeInfo(); } @NotNull @Override protected UsageViewDescriptor createUsageViewDescriptor(@NotNull UsageInfo[] usages) { return new ChangeSignatureViewDescriptor(getChangeInfo().getMethod()); } @Override protected void refreshElements(@NotNull PsiElement[] elements) { boolean condition = elements.length == 1 && elements[0] instanceof PsiMethod; LOG.assertTrue(condition); getChangeInfo().updateMethod((PsiMethod)elements[0]); } @Override protected boolean preprocessUsages(@NotNull Ref<UsageInfo[]> refUsages) { MultiMap<PsiElement, String> conflictDescriptions = new MultiMap<>(); collectConflictsFromExtensions(refUsages, conflictDescriptions, myChangeInfo); final UsageInfo[] usagesIn = refUsages.get(); RenameUtil.addConflictDescriptions(usagesIn, conflictDescriptions); Set<UsageInfo> usagesSet = new HashSet<>(Arrays.asList(usagesIn)); RenameUtil.removeConflictUsages(usagesSet); if (!conflictDescriptions.isEmpty()) { if (ApplicationManager.getApplication().isUnitTestMode()) { throw new ConflictsInTestsException(conflictDescriptions.values()); } ConflictsDialog dialog = prepareConflictsDialog(conflictDescriptions, usagesIn); if (!dialog.showAndGet()) { if (dialog.isShowConflicts()) prepareSuccessful(); return false; } } refUsages.set(usagesSet.toArray(new UsageInfo[usagesSet.size()])); prepareSuccessful(); return true; } }