Search in sources :

Example 1 with MoveRenameUsageInfo

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

the class GrChageSignatureUsageSearcher 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) {
        if (GroovyLanguage.INSTANCE.equals(overridingMethod.getLanguage())) {
            result.add(new OverriderUsageInfo(overridingMethod, method, isOriginal, isToModifyArgs, isToThrowExceptions));
        }
    }
    boolean needToChangeCalls = !myChangeInfo.isGenerateDelegate() && (myChangeInfo.isNameChanged() || myChangeInfo.isParameterSetOrOrderChanged() || myChangeInfo.isExceptionSetOrOrderChanged() || myChangeInfo.isVisibilityChanged());
    if (needToChangeCalls) {
        PsiReference[] refs = MethodReferencesSearch.search(method, projectScope, true).toArray(PsiReference.EMPTY_ARRAY);
        for (PsiReference ref : refs) {
            PsiElement element = ref.getElement();
            if (!GroovyLanguage.INSTANCE.equals(element.getLanguage()))
                continue;
            boolean isToCatchExceptions = isToThrowExceptions && needToCatchExceptions(RefactoringUtil.getEnclosingMethod(element));
            if (PsiUtil.isMethodUsage(element)) {
                result.add(new GrMethodCallUsageInfo(element, isToModifyArgs, isToCatchExceptions, method));
            } else if (element instanceof GrDocTagValueToken) {
                result.add(new UsageInfo(ref.getElement()));
            } else if (element instanceof GrMethod && ((GrMethod) element).isConstructor()) {
                DefaultConstructorImplicitUsageInfo implicitUsageInfo = new DefaultConstructorImplicitUsageInfo((GrMethod) element, ((GrMethod) element).getContainingClass(), method);
                result.add(implicitUsageInfo);
            } else if (element instanceof PsiClass) {
                LOG.assertTrue(method.isConstructor());
                final PsiClass psiClass = (PsiClass) element;
                if (psiClass instanceof GrAnonymousClassDefinition) {
                    result.add(new GrMethodCallUsageInfo(element, isToModifyArgs, isToCatchExceptions, method));
                    continue;
                }
                /*if (!(myChangeInfo instanceof JavaChangeInfoImpl)) continue; todo propagate methods
          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 {
                result.add(new MoveRenameUsageInfo(element, ref, method));
            }
        }
    } 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 GrDocTagValueToken) {
                result.add(new UsageInfo(reference));
            }
        }
    }
    // Conflicts
    if (method instanceof GrMethod) {
        detectLocalsCollisionsInMethod((GrMethod) method, result, isOriginal);
    }
    for (final PsiMethod overridingMethod : overridingMethods) {
        if (overridingMethod instanceof GrMethod) {
            detectLocalsCollisionsInMethod((GrMethod) overridingMethod, result, isOriginal);
        }
    }
    return overridingMethods;
}
Also used : GrDocTagValueToken(org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocTagValueToken) GrAnonymousClassDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrAnonymousClassDefinition) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) MoveRenameUsageInfo(com.intellij.refactoring.util.MoveRenameUsageInfo) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) NoConstructorClassUsageInfo(com.intellij.refactoring.util.usageInfo.NoConstructorClassUsageInfo) DefaultConstructorImplicitUsageInfo(com.intellij.refactoring.util.usageInfo.DefaultConstructorImplicitUsageInfo) 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)

Example 2 with MoveRenameUsageInfo

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

the class RenameGrFieldProcessor method findCollisions.

@Override
public void findCollisions(PsiElement element, String newName, Map<? extends PsiElement, String> allRenames, List<UsageInfo> result) {
    List<UsageInfo> collisions = new ArrayList<>();
    for (UsageInfo info : result) {
        if (!(info instanceof MoveRenameUsageInfo))
            continue;
        final PsiElement infoElement = info.getElement();
        final PsiElement referencedElement = ((MoveRenameUsageInfo) info).getReferencedElement();
        if (!(infoElement instanceof GrReferenceExpression))
            continue;
        final GrReferenceExpression refExpr = (GrReferenceExpression) infoElement;
        if (!(referencedElement instanceof GrField || refExpr.advancedResolve().isInvokedOnProperty()))
            continue;
        if (!(refExpr.getParent() instanceof GrCall))
            continue;
        final PsiType[] argTypes = PsiUtil.getArgumentTypes(refExpr, false);
        final PsiType[] typeArguments = refExpr.getTypeArguments();
        final MethodResolverProcessor processor = new MethodResolverProcessor(newName, refExpr, false, null, argTypes, typeArguments);
        final PsiMethod resolved = ResolveUtil.resolveExistingElement(refExpr, processor, PsiMethod.class);
        if (resolved == null)
            continue;
        collisions.add(new UnresolvableCollisionUsageInfo(resolved, refExpr) {

            @Override
            public String getDescription() {
                return GroovyRefactoringBundle.message("usage.will.be.overriden.by.method", refExpr.getParent().getText(), PsiFormatUtil.formatMethod(resolved, PsiSubstitutor.EMPTY, PsiFormatUtilBase.SHOW_NAME, PsiFormatUtilBase.SHOW_TYPE));
            }
        });
    }
    result.addAll(collisions);
    super.findCollisions(element, newName, allRenames, result);
}
Also used : GrField(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField) GrCall(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrCall) ArrayList(java.util.ArrayList) MoveRenameUsageInfo(com.intellij.refactoring.util.MoveRenameUsageInfo) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) MethodResolverProcessor(org.jetbrains.plugins.groovy.lang.resolve.processors.MethodResolverProcessor) UnresolvableCollisionUsageInfo(com.intellij.refactoring.rename.UnresolvableCollisionUsageInfo) UsageInfo(com.intellij.usageView.UsageInfo) UnresolvableCollisionUsageInfo(com.intellij.refactoring.rename.UnresolvableCollisionUsageInfo) MoveRenameUsageInfo(com.intellij.refactoring.util.MoveRenameUsageInfo)

Example 3 with MoveRenameUsageInfo

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

the class BaseRefactoringProcessor method createPresentation.

@NotNull
private static UsageViewPresentation createPresentation(@NotNull UsageViewDescriptor descriptor, @NotNull Usage[] usages) {
    UsageViewPresentation presentation = new UsageViewPresentation();
    presentation.setTabText(RefactoringBundle.message("usageView.tabText"));
    presentation.setTargetsNodeText(descriptor.getProcessedElementsHeader());
    presentation.setShowReadOnlyStatusAsRed(true);
    presentation.setShowCancelButton(true);
    presentation.setUsagesString(RefactoringBundle.message("usageView.usagesText"));
    int codeUsageCount = 0;
    int nonCodeUsageCount = 0;
    int dynamicUsagesCount = 0;
    Set<PsiFile> codeFiles = new HashSet<>();
    Set<PsiFile> nonCodeFiles = new HashSet<>();
    Set<PsiFile> dynamicUsagesCodeFiles = new HashSet<>();
    for (Usage usage : usages) {
        if (usage instanceof PsiElementUsage) {
            final PsiElementUsage elementUsage = (PsiElementUsage) usage;
            final PsiElement element = elementUsage.getElement();
            if (element == null)
                continue;
            final PsiFile containingFile = element.getContainingFile();
            if (elementUsage.isNonCodeUsage()) {
                nonCodeUsageCount++;
                nonCodeFiles.add(containingFile);
            } else {
                codeUsageCount++;
                codeFiles.add(containingFile);
            }
            if (usage instanceof UsageInfo2UsageAdapter) {
                final UsageInfo usageInfo = ((UsageInfo2UsageAdapter) usage).getUsageInfo();
                if (usageInfo instanceof MoveRenameUsageInfo && usageInfo.isDynamicUsage()) {
                    dynamicUsagesCount++;
                    dynamicUsagesCodeFiles.add(containingFile);
                }
            }
        }
    }
    codeFiles.remove(null);
    nonCodeFiles.remove(null);
    dynamicUsagesCodeFiles.remove(null);
    String codeReferencesText = descriptor.getCodeReferencesText(codeUsageCount, codeFiles.size());
    presentation.setCodeUsagesString(codeReferencesText);
    final String commentReferencesText = descriptor.getCommentReferencesText(nonCodeUsageCount, nonCodeFiles.size());
    if (commentReferencesText != null) {
        presentation.setNonCodeUsagesString(commentReferencesText);
    }
    presentation.setDynamicUsagesString("Dynamic " + StringUtil.decapitalize(descriptor.getCodeReferencesText(dynamicUsagesCount, dynamicUsagesCodeFiles.size())));
    String generatedCodeString;
    if (codeReferencesText.contains("in code")) {
        generatedCodeString = StringUtil.replace(codeReferencesText, "in code", "in generated code");
    } else {
        generatedCodeString = codeReferencesText + " in generated code";
    }
    presentation.setUsagesInGeneratedCodeString(generatedCodeString);
    return presentation;
}
Also used : PsiElementUsage(com.intellij.usages.rules.PsiElementUsage) MoveRenameUsageInfo(com.intellij.refactoring.util.MoveRenameUsageInfo) PsiFile(com.intellij.psi.PsiFile) PsiElement(com.intellij.psi.PsiElement) MoveRenameUsageInfo(com.intellij.refactoring.util.MoveRenameUsageInfo) UsageInfo(com.intellij.usageView.UsageInfo) HashSet(com.intellij.util.containers.HashSet) THashSet(gnu.trove.THashSet) PsiElementUsage(com.intellij.usages.rules.PsiElementUsage) NotNull(org.jetbrains.annotations.NotNull)

Example 4 with MoveRenameUsageInfo

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

the class CommonMoveUtil method retargetUsages.

public static NonCodeUsageInfo[] retargetUsages(final UsageInfo[] usages, final Map<PsiElement, PsiElement> oldToNewElementsMapping) throws IncorrectOperationException {
    Arrays.sort(usages, (o1, o2) -> {
        final VirtualFile file1 = o1.getVirtualFile();
        final VirtualFile file2 = o2.getVirtualFile();
        if (Comparing.equal(file1, file2)) {
            final ProperTextRange rangeInElement1 = o1.getRangeInElement();
            final ProperTextRange rangeInElement2 = o2.getRangeInElement();
            if (rangeInElement1 != null && rangeInElement2 != null) {
                return rangeInElement2.getStartOffset() - rangeInElement1.getStartOffset();
            }
            return 0;
        }
        if (file1 == null)
            return -1;
        if (file2 == null)
            return 1;
        return Comparing.compare(file1.getPath(), file2.getPath());
    });
    List<NonCodeUsageInfo> nonCodeUsages = new ArrayList<>();
    for (UsageInfo usage : usages) {
        if (usage instanceof NonCodeUsageInfo) {
            nonCodeUsages.add((NonCodeUsageInfo) usage);
        } else if (usage instanceof MoveRenameUsageInfo) {
            final MoveRenameUsageInfo moveRenameUsage = (MoveRenameUsageInfo) usage;
            final PsiElement oldElement = moveRenameUsage.getReferencedElement();
            final PsiElement newElement = oldToNewElementsMapping.get(oldElement);
            LOG.assertTrue(newElement != null, oldElement);
            final PsiReference reference = moveRenameUsage.getReference();
            if (reference != null) {
                try {
                    reference.bindToElement(newElement);
                } catch (IncorrectOperationException e) {
                //
                }
            }
        }
    }
    return nonCodeUsages.toArray(new NonCodeUsageInfo[nonCodeUsages.size()]);
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) ProperTextRange(com.intellij.openapi.util.ProperTextRange) NonCodeUsageInfo(com.intellij.refactoring.util.NonCodeUsageInfo) MoveRenameUsageInfo(com.intellij.refactoring.util.MoveRenameUsageInfo) PsiReference(com.intellij.psi.PsiReference) IncorrectOperationException(com.intellij.util.IncorrectOperationException) NonCodeUsageInfo(com.intellij.refactoring.util.NonCodeUsageInfo) MoveRenameUsageInfo(com.intellij.refactoring.util.MoveRenameUsageInfo) UsageInfo(com.intellij.usageView.UsageInfo) PsiElement(com.intellij.psi.PsiElement)

Example 5 with MoveRenameUsageInfo

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

the class TurnRefsToSuperProcessorBase method performVariablesRenaming.

protected void performVariablesRenaming() {
    try {
        //forget about smart pointers
        Map<PsiElement, String> variableRenames = new HashMap<>();
        for (Map.Entry<SmartPsiElementPointer, String> entry : myVariablesRenames.entrySet()) {
            variableRenames.put(entry.getKey().getElement(), entry.getValue());
        }
        for (UsageInfo usage : myVariablesUsages) {
            if (usage instanceof MoveRenameUsageInfo) {
                final MoveRenameUsageInfo renameUsageInfo = ((MoveRenameUsageInfo) usage);
                final String newName = variableRenames.get(renameUsageInfo.getUpToDateReferencedElement());
                final PsiReference reference = renameUsageInfo.getReference();
                if (reference != null) {
                    reference.handleElementRename(newName);
                }
            }
        }
        for (Map.Entry<SmartPsiElementPointer, String> entry : myVariablesRenames.entrySet()) {
            final String newName = entry.getValue();
            if (newName != null) {
                final PsiVariable variable = (PsiVariable) entry.getKey().getElement();
                variable.setName(newName);
            }
        }
    } catch (IncorrectOperationException e) {
        LOG.error(e);
    }
}
Also used : HashMap(com.intellij.util.containers.HashMap) MoveRenameUsageInfo(com.intellij.refactoring.util.MoveRenameUsageInfo) IncorrectOperationException(com.intellij.util.IncorrectOperationException) HashMap(com.intellij.util.containers.HashMap) Map(java.util.Map) UsageInfo(com.intellij.usageView.UsageInfo) MoveRenameUsageInfo(com.intellij.refactoring.util.MoveRenameUsageInfo)

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