Search in sources :

Example 11 with MoveRenameUsageInfo

use of com.intellij.refactoring.util.MoveRenameUsageInfo in project intellij-community by JetBrains.

the class RenameJavaVariableProcessor method renameElement.

public void renameElement(final PsiElement psiElement, final String newName, final UsageInfo[] usages, @Nullable RefactoringElementListener listener) throws IncorrectOperationException {
    PsiVariable variable = (PsiVariable) psiElement;
    List<MemberHidesOuterMemberUsageInfo> outerHides = new ArrayList<>();
    List<MemberHidesStaticImportUsageInfo> staticImportHides = new ArrayList<>();
    List<PsiElement> occurrencesToCheckForConflict = new ArrayList<>();
    // rename all references
    for (UsageInfo usage : usages) {
        final PsiElement element = usage.getElement();
        if (element == null)
            continue;
        if (usage instanceof MemberHidesStaticImportUsageInfo) {
            staticImportHides.add((MemberHidesStaticImportUsageInfo) usage);
        } else if (usage instanceof LocalHidesFieldUsageInfo) {
            PsiJavaCodeReferenceElement collidingRef = (PsiJavaCodeReferenceElement) element;
            PsiElement resolved = collidingRef.resolve();
            if (resolved instanceof PsiField) {
                qualifyMember((PsiField) resolved, collidingRef, newName);
            } else {
            // do nothing
            }
        } else if (usage instanceof MemberHidesOuterMemberUsageInfo) {
            PsiJavaCodeReferenceElement collidingRef = (PsiJavaCodeReferenceElement) element;
            PsiField resolved = (PsiField) collidingRef.resolve();
            outerHides.add(new MemberHidesOuterMemberUsageInfo(element, resolved));
        } else {
            final PsiReference ref;
            if (usage instanceof MoveRenameUsageInfo) {
                ref = usage.getReference();
            } else {
                ref = element.getReference();
            }
            if (ref != null) {
                PsiElement newElem = ref.handleElementRename(newName);
                if (variable instanceof PsiField) {
                    occurrencesToCheckForConflict.add(newElem);
                }
            }
        }
    }
    // do actual rename
    variable.setName(newName);
    if (listener != null) {
        listener.elementRenamed(variable);
    }
    if (variable instanceof PsiField) {
        for (PsiElement occurrence : occurrencesToCheckForConflict) {
            fixPossibleNameCollisionsForFieldRenaming((PsiField) variable, newName, occurrence);
        }
    }
    qualifyOuterMemberReferences(outerHides);
    qualifyStaticImportReferences(staticImportHides);
}
Also used : ArrayList(java.util.ArrayList) MoveRenameUsageInfo(com.intellij.refactoring.util.MoveRenameUsageInfo) UsageInfo(com.intellij.usageView.UsageInfo) MoveRenameUsageInfo(com.intellij.refactoring.util.MoveRenameUsageInfo)

Example 12 with MoveRenameUsageInfo

use of com.intellij.refactoring.util.MoveRenameUsageInfo in project intellij-community by JetBrains.

the class MoveJavaFileHandler method retargetUsages.

@Override
public void retargetUsages(List<UsageInfo> usageInfos, Map<PsiElement, PsiElement> oldToNewMap) {
    for (UsageInfo usage : usageInfos) {
        if (usage instanceof MoveRenameUsageInfo) {
            final MoveRenameUsageInfo moveRenameUsage = (MoveRenameUsageInfo) usage;
            final PsiElement oldElement = moveRenameUsage.getReferencedElement();
            final PsiElement newElement = oldToNewMap.get(oldElement);
            final PsiReference reference = moveRenameUsage.getReference();
            if (reference != null) {
                try {
                    LOG.assertTrue(newElement != null, oldElement != null ? oldElement : reference);
                    reference.bindToElement(newElement);
                } catch (IncorrectOperationException ex) {
                    LOG.error(ex);
                }
            }
        }
    }
}
Also used : MoveRenameUsageInfo(com.intellij.refactoring.util.MoveRenameUsageInfo) IncorrectOperationException(com.intellij.util.IncorrectOperationException) MoveRenameUsageInfo(com.intellij.refactoring.util.MoveRenameUsageInfo) UsageInfo(com.intellij.usageView.UsageInfo)

Example 13 with MoveRenameUsageInfo

use of com.intellij.refactoring.util.MoveRenameUsageInfo in project intellij-community by JetBrains.

the class MoveGroovyFileHandler method retargetUsages.

@Override
public void retargetUsages(List<UsageInfo> usageInfos, Map<PsiElement, PsiElement> oldToNewMap) {
    for (UsageInfo usage : usageInfos) {
        if (usage instanceof MoveRenameUsageInfo) {
            final MoveRenameUsageInfo moveRenameUsage = (MoveRenameUsageInfo) usage;
            final PsiElement oldElement = moveRenameUsage.getReferencedElement();
            final PsiElement newElement = oldToNewMap.get(oldElement);
            final PsiReference reference = moveRenameUsage.getReference();
            if (reference != null) {
                try {
                    LOG.assertTrue(newElement != null, oldElement != null ? oldElement : reference);
                    reference.bindToElement(newElement);
                } catch (IncorrectOperationException ex) {
                    LOG.error(ex);
                }
            }
        }
    }
}
Also used : MoveRenameUsageInfo(com.intellij.refactoring.util.MoveRenameUsageInfo) IncorrectOperationException(com.intellij.util.IncorrectOperationException) MoveRenameUsageInfo(com.intellij.refactoring.util.MoveRenameUsageInfo) UsageInfo(com.intellij.usageView.UsageInfo)

Example 14 with MoveRenameUsageInfo

use of com.intellij.refactoring.util.MoveRenameUsageInfo in project intellij-community by JetBrains.

the class RenameProcessor method classifyUsages.

public static MultiMap<PsiElement, UsageInfo> classifyUsages(Collection<? extends PsiElement> elements, UsageInfo[] usages) {
    final MultiMap<PsiElement, UsageInfo> result = new MultiMap<>();
    for (UsageInfo usage : usages) {
        LOG.assertTrue(usage instanceof MoveRenameUsageInfo);
        if (usage.getReference() instanceof LightElement) {
            //filter out implicit references (e.g. from derived class to super class' default constructor)
            continue;
        }
        MoveRenameUsageInfo usageInfo = (MoveRenameUsageInfo) usage;
        if (usage instanceof RelatedUsageInfo) {
            final PsiElement relatedElement = ((RelatedUsageInfo) usage).getRelatedElement();
            if (elements.contains(relatedElement)) {
                result.putValue(relatedElement, usage);
            }
        } else {
            PsiElement referenced = usageInfo.getReferencedElement();
            if (elements.contains(referenced)) {
                result.putValue(referenced, usage);
            } else if (referenced != null) {
                PsiElement indirect = referenced.getNavigationElement();
                if (elements.contains(indirect)) {
                    result.putValue(indirect, usage);
                }
            }
        }
    }
    return result;
}
Also used : MultiMap(com.intellij.util.containers.MultiMap) MoveRenameUsageInfo(com.intellij.refactoring.util.MoveRenameUsageInfo) UsageInfo(com.intellij.usageView.UsageInfo) MoveRenameUsageInfo(com.intellij.refactoring.util.MoveRenameUsageInfo) NonCodeUsageInfo(com.intellij.refactoring.util.NonCodeUsageInfo) RelatedUsageInfo(com.intellij.refactoring.util.RelatedUsageInfo) LightElement(com.intellij.psi.impl.light.LightElement) RelatedUsageInfo(com.intellij.refactoring.util.RelatedUsageInfo)

Example 15 with MoveRenameUsageInfo

use of com.intellij.refactoring.util.MoveRenameUsageInfo in project intellij-community by JetBrains.

the class JavaChangeSignatureUsageSearcher method findSimpleUsagesWithoutParameters.

private PsiMethod[] findSimpleUsagesWithoutParameters(final PsiMethod method, final ArrayList<UsageInfo> result, boolean isToModifyArgs, boolean isToThrowExceptions, boolean isOriginal) {
    GlobalSearchScope projectScope = GlobalSearchScope.projectScope(method.getProject());
    PsiMethod[] overridingMethods = OverridingMethodsSearch.search(method).toArray(PsiMethod.EMPTY_ARRAY);
    for (PsiMethod overridingMethod : overridingMethods) {
        result.add(new OverriderUsageInfo(overridingMethod, method, isOriginal, isToModifyArgs, isToThrowExceptions));
    }
    boolean needToChangeCalls = !myChangeInfo.isGenerateDelegate() && (myChangeInfo.isNameChanged() || myChangeInfo.isParameterSetOrOrderChanged() || myChangeInfo.isExceptionSetOrOrderChanged() || myChangeInfo.isVisibilityChanged());
    if (needToChangeCalls) {
        int parameterCount = method.getParameterList().getParametersCount();
        PsiReference[] refs = MethodReferencesSearch.search(method, projectScope, true).toArray(PsiReference.EMPTY_ARRAY);
        for (PsiReference ref : refs) {
            PsiElement element = ref.getElement();
            boolean isToCatchExceptions = isToThrowExceptions && needToCatchExceptions(RefactoringUtil.getEnclosingMethod(element));
            if (!isToCatchExceptions) {
                if (RefactoringUtil.isMethodUsage(element)) {
                    PsiExpressionList list = RefactoringUtil.getArgumentListByMethodReference(element);
                    if (list == null || !method.isVarArgs() && list.getExpressions().length != parameterCount)
                        continue;
                }
            }
            if (RefactoringUtil.isMethodUsage(element)) {
                result.add(new MethodCallUsageInfo(element, isToModifyArgs, isToCatchExceptions));
            } else if (element instanceof PsiDocTagValue) {
                result.add(new UsageInfo(element));
            } else if (element instanceof PsiMethod && ((PsiMethod) element).isConstructor()) {
                if (JavaLanguage.INSTANCE.equals(element.getLanguage())) {
                    DefaultConstructorImplicitUsageInfo implicitUsageInfo = new DefaultConstructorImplicitUsageInfo((PsiMethod) element, ((PsiMethod) element).getContainingClass(), method);
                    result.add(implicitUsageInfo);
                }
            } else if (element instanceof PsiClass) {
                LOG.assertTrue(method.isConstructor());
                final PsiClass psiClass = (PsiClass) element;
                if (JavaLanguage.INSTANCE.equals(psiClass.getLanguage())) {
                    if (myChangeInfo instanceof JavaChangeInfoImpl) {
                        if (shouldPropagateToNonPhysicalMethod(method, result, psiClass, ((JavaChangeInfoImpl) myChangeInfo).propagateParametersMethods)) {
                            continue;
                        }
                        if (shouldPropagateToNonPhysicalMethod(method, result, psiClass, ((JavaChangeInfoImpl) myChangeInfo).propagateExceptionsMethods)) {
                            continue;
                        }
                    }
                    result.add(new NoConstructorClassUsageInfo(psiClass));
                }
            } else if (ref instanceof PsiCallReference) {
                result.add(new CallReferenceUsageInfo((PsiCallReference) ref));
            } else if (element instanceof PsiMethodReferenceExpression && MethodReferenceUsageInfo.needToExpand(myChangeInfo)) {
                result.add(new MethodReferenceUsageInfo(element, method, isToModifyArgs, isToCatchExceptions));
            } else {
                result.add(new MoveRenameUsageInfo(element, ref, method));
            }
        }
    //if (method.isConstructor() && parameterCount == 0) {
    //    RefactoringUtil.visitImplicitConstructorUsages(method.getContainingClass(),
    //                                                   new DefaultConstructorUsageCollector(result));
    //}
    } else if (myChangeInfo.isParameterTypesChanged()) {
        PsiReference[] refs = MethodReferencesSearch.search(method, projectScope, true).toArray(PsiReference.EMPTY_ARRAY);
        for (PsiReference reference : refs) {
            final PsiElement element = reference.getElement();
            if (element instanceof PsiDocTagValue) {
                result.add(new UsageInfo(reference));
            } else if (element instanceof XmlElement) {
                result.add(new MoveRenameUsageInfo(reference, method));
            } else if (element instanceof PsiMethodReferenceExpression) {
                result.add(new UsageInfo(reference));
            }
        }
    }
    // Conflicts
    detectLocalsCollisionsInMethod(method, result, isOriginal);
    for (final PsiMethod overridingMethod : overridingMethods) {
        detectLocalsCollisionsInMethod(overridingMethod, result, isOriginal);
    }
    return overridingMethods;
}
Also used : MoveRenameUsageInfo(com.intellij.refactoring.util.MoveRenameUsageInfo) PsiDocTagValue(com.intellij.psi.javadoc.PsiDocTagValue) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) NoConstructorClassUsageInfo(com.intellij.refactoring.util.usageInfo.NoConstructorClassUsageInfo) DefaultConstructorImplicitUsageInfo(com.intellij.refactoring.util.usageInfo.DefaultConstructorImplicitUsageInfo) XmlElement(com.intellij.psi.xml.XmlElement) UsageInfo(com.intellij.usageView.UsageInfo) UnresolvableCollisionUsageInfo(com.intellij.refactoring.rename.UnresolvableCollisionUsageInfo) NoConstructorClassUsageInfo(com.intellij.refactoring.util.usageInfo.NoConstructorClassUsageInfo) MoveRenameUsageInfo(com.intellij.refactoring.util.MoveRenameUsageInfo) DefaultConstructorImplicitUsageInfo(com.intellij.refactoring.util.usageInfo.DefaultConstructorImplicitUsageInfo)

Aggregations

MoveRenameUsageInfo (com.intellij.refactoring.util.MoveRenameUsageInfo)16 UsageInfo (com.intellij.usageView.UsageInfo)15 PsiElement (com.intellij.psi.PsiElement)5 IncorrectOperationException (com.intellij.util.IncorrectOperationException)4 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)3 UnresolvableCollisionUsageInfo (com.intellij.refactoring.rename.UnresolvableCollisionUsageInfo)3 NonCodeUsageInfo (com.intellij.refactoring.util.NonCodeUsageInfo)3 TextRange (com.intellij.openapi.util.TextRange)2 PsiFile (com.intellij.psi.PsiFile)2 PsiReference (com.intellij.psi.PsiReference)2 DefaultConstructorImplicitUsageInfo (com.intellij.refactoring.util.usageInfo.DefaultConstructorImplicitUsageInfo)2 NoConstructorClassUsageInfo (com.intellij.refactoring.util.usageInfo.NoConstructorClassUsageInfo)2 HashSet (com.intellij.util.containers.HashSet)2 ArrayList (java.util.ArrayList)2 NotNull (org.jetbrains.annotations.NotNull)2 ProperTextRange (com.intellij.openapi.util.ProperTextRange)1 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 LightElement (com.intellij.psi.impl.light.LightElement)1 PsiDocTagValue (com.intellij.psi.javadoc.PsiDocTagValue)1 XmlElement (com.intellij.psi.xml.XmlElement)1