Search in sources :

Example 46 with UsageInfo

use of com.intellij.usageView.UsageInfo in project intellij-community by JetBrains.

the class InlineToAnonymousClassProcessor method findUsages.

@NotNull
protected UsageInfo[] findUsages() {
    if (myInlineThisOnly) {
        return new UsageInfo[] { new UsageInfo(myCallToInline) };
    }
    Set<UsageInfo> usages = new HashSet<>();
    final GlobalSearchScope searchScope = GlobalSearchScope.projectScope(myProject);
    for (PsiReference reference : ReferencesSearch.search(myClass, searchScope)) {
        usages.add(new UsageInfo(reference.getElement()));
    }
    final String qName = myClass.getQualifiedName();
    if (qName != null) {
        List<UsageInfo> nonCodeUsages = new ArrayList<>();
        if (mySearchInComments) {
            TextOccurrencesUtil.addUsagesInStringsAndComments(myClass, qName, nonCodeUsages, new NonCodeUsageInfoFactory(myClass, qName));
        }
        if (mySearchInNonJavaFiles) {
            TextOccurrencesUtil.addTextOccurences(myClass, qName, searchScope, nonCodeUsages, new NonCodeUsageInfoFactory(myClass, qName));
        }
        usages.addAll(nonCodeUsages);
    }
    return usages.toArray(new UsageInfo[usages.size()]);
}
Also used : NonCodeUsageInfoFactory(com.intellij.refactoring.rename.NonCodeUsageInfoFactory) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) UsageInfo(com.intellij.usageView.UsageInfo) NotNull(org.jetbrains.annotations.NotNull)

Example 47 with UsageInfo

use of com.intellij.usageView.UsageInfo in project intellij-community by JetBrains.

the class InlineToAnonymousClassProcessor method performRefactoring.

protected void performRefactoring(@NotNull UsageInfo[] usages) {
    final PsiClassType superType = getSuperType(myClass);
    LOG.assertTrue(superType != null);
    List<PsiElement> elementsToDelete = new ArrayList<>();
    List<PsiNewExpression> newExpressions = new ArrayList<>();
    for (UsageInfo info : usages) {
        final PsiElement element = info.getElement();
        if (element instanceof PsiNewExpression) {
            newExpressions.add((PsiNewExpression) element);
        } else if (element.getParent() instanceof PsiNewExpression) {
            newExpressions.add((PsiNewExpression) element.getParent());
        } else {
            PsiImportStatement statement = PsiTreeUtil.getParentOfType(element, PsiImportStatement.class);
            if (statement != null && !myInlineThisOnly) {
                elementsToDelete.add(statement);
            } else {
                PsiTypeElement typeElement = PsiTreeUtil.getParentOfType(element, PsiTypeElement.class);
                if (typeElement != null) {
                    replaceWithSuperType(typeElement, superType);
                }
            }
        }
    }
    Collections.sort(newExpressions, PsiUtil.BY_POSITION);
    for (PsiNewExpression newExpression : newExpressions) {
        replaceNewOrType(newExpression, superType);
    }
    for (PsiElement element : elementsToDelete) {
        try {
            if (element.isValid()) {
                element.delete();
            }
        } catch (IncorrectOperationException e) {
            LOG.error(e);
        }
    }
    if (!myInlineThisOnly && myClass.getOriginalElement().isWritable()) {
        try {
            myClass.delete();
        } catch (IncorrectOperationException e) {
            LOG.error(e);
        }
    }
}
Also used : IncorrectOperationException(com.intellij.util.IncorrectOperationException) UsageInfo(com.intellij.usageView.UsageInfo)

Example 48 with UsageInfo

use of com.intellij.usageView.UsageInfo in project intellij-community by JetBrains.

the class InlineSuperCallUsageInfo method getConflictMessage.

@Override
public String getConflictMessage() {
    final MultiMap<PsiElement, String> conflicts = new MultiMap<>();
    final PsiElement element = getElement();
    if (element instanceof PsiMethodCallExpression) {
        PsiMethodCallExpression methodCallExpression = (PsiMethodCallExpression) element;
        final PsiMethod superConstructor = methodCallExpression.resolveMethod();
        if (superConstructor != null) {
            InlineMethodProcessor.addInaccessibleMemberConflicts(superConstructor, new UsageInfo[] { new UsageInfo(methodCallExpression.getMethodExpression()) }, new ReferencedElementsCollector() {

                @Override
                protected void checkAddMember(@NotNull PsiMember member) {
                    if (!PsiTreeUtil.isAncestor(superConstructor.getContainingClass(), member, false)) {
                        super.checkAddMember(member);
                    }
                }
            }, conflicts);
            if (InlineMethodProcessor.checkBadReturns(superConstructor) && !InlineUtil.allUsagesAreTailCalls(superConstructor)) {
                conflicts.putValue(superConstructor, CommonRefactoringUtil.capitalize(RefactoringBundle.message("refactoring.is.not.supported.when.return.statement.interrupts.the.execution.flow", "") + " of super constructor"));
            }
        }
    }
    //todo
    return conflicts.isEmpty() ? null : conflicts.values().iterator().next();
}
Also used : MultiMap(com.intellij.util.containers.MultiMap) ReferencedElementsCollector(com.intellij.refactoring.inline.ReferencedElementsCollector) UsageInfo(com.intellij.usageView.UsageInfo) FixableUsageInfo(com.intellij.refactoring.util.FixableUsageInfo)

Example 49 with UsageInfo

use of com.intellij.usageView.UsageInfo in project intellij-community by JetBrains.

the class InlineMethodProcessor method getInaccessible.

/**
   * Given a set of referencedElements, returns a map from containers (in a sense of ConflictsUtil.getContainer)
   * to subsets of referencedElemens that are not accessible from that container
   *
   * @param referencedElements
   * @param usages
   * @param elementToInline
   */
private static Map<PsiMember, Set<PsiMember>> getInaccessible(HashSet<PsiMember> referencedElements, UsageInfo[] usages, PsiElement elementToInline) {
    final Map<PsiMember, Set<PsiMember>> result = new HashMap<>();
    final PsiResolveHelper resolveHelper = JavaPsiFacade.getInstance(elementToInline.getProject()).getResolveHelper();
    for (UsageInfo usage : usages) {
        final PsiElement usageElement = usage.getElement();
        if (usageElement == null)
            continue;
        final PsiElement container = ConflictsUtil.getContainer(usageElement);
        // usage in import statement
        if (!(container instanceof PsiMember))
            continue;
        PsiMember memberContainer = (PsiMember) container;
        Set<PsiMember> inaccessibleReferenced = result.get(memberContainer);
        if (inaccessibleReferenced == null) {
            inaccessibleReferenced = new HashSet<>();
            result.put(memberContainer, inaccessibleReferenced);
            for (PsiMember member : referencedElements) {
                if (PsiTreeUtil.isAncestor(elementToInline, member, false))
                    continue;
                if (elementToInline instanceof PsiClass && InheritanceUtil.isInheritorOrSelf((PsiClass) elementToInline, member.getContainingClass(), true))
                    continue;
                PsiElement resolveScope = usageElement instanceof PsiReferenceExpression ? ((PsiReferenceExpression) usageElement).advancedResolve(false).getCurrentFileResolveScope() : null;
                if (!resolveHelper.isAccessible(member, member.getModifierList(), usageElement, null, resolveScope)) {
                    inaccessibleReferenced.add(member);
                }
            }
        }
    }
    return result;
}
Also used : HashMap(com.intellij.util.containers.HashMap) UsageInfo(com.intellij.usageView.UsageInfo)

Example 50 with UsageInfo

use of com.intellij.usageView.UsageInfo in project intellij-community by JetBrains.

the class InlineParameterExpressionProcessor method performRefactoring.

@Override
protected void performRefactoring(@NotNull UsageInfo[] usages) {
    final List<PsiClassType> thrownExceptions = ExceptionUtil.getThrownCheckedExceptions(myInitializer);
    final Set<PsiVariable> varsUsedInInitializer = new HashSet<>();
    final Set<PsiJavaCodeReferenceElement> paramRefsToInline = new HashSet<>();
    final Map<PsiElement, PsiElement> replacements = new HashMap<>();
    for (UsageInfo usage : usages) {
        if (usage instanceof LocalReplacementUsageInfo) {
            final LocalReplacementUsageInfo replacementUsageInfo = (LocalReplacementUsageInfo) usage;
            final PsiElement element = replacementUsageInfo.getElement();
            final PsiElement replacement = replacementUsageInfo.getReplacement();
            if (element != null && replacement != null) {
                replacements.put(element, replacement);
            }
            varsUsedInInitializer.add(replacementUsageInfo.getVariable());
        } else {
            LOG.assertTrue(!myCreateLocal);
            paramRefsToInline.add((PsiJavaCodeReferenceElement) usage.getElement());
        }
    }
    myInitializer = (PsiExpression) RefactoringUtil.replaceElementsWithMap(myInitializer, replacements);
    if (myCreateLocal) {
        final PsiElementFactory factory = JavaPsiFacade.getInstance(myMethod.getProject()).getElementFactory();
        PsiDeclarationStatement localDeclaration = factory.createVariableDeclarationStatement(myParameter.getName(), myParameter.getType(), myInitializer);
        final PsiLocalVariable declaredVar = (PsiLocalVariable) localDeclaration.getDeclaredElements()[0];
        PsiUtil.setModifierProperty(declaredVar, PsiModifier.FINAL, myParameter.hasModifierProperty(PsiModifier.FINAL));
        final PsiExpression localVarInitializer = InlineUtil.inlineVariable(myParameter, myInitializer, (PsiReferenceExpression) factory.createExpressionFromText(myParameter.getName(), myMethod));
        final PsiExpression initializer = declaredVar.getInitializer();
        LOG.assertTrue(initializer != null);
        initializer.replace(localVarInitializer);
        final PsiCodeBlock body = myMethod.getBody();
        if (body != null) {
            PsiElement anchor = findAnchorForLocalVariableDeclaration(body);
            body.addAfter(localDeclaration, anchor);
        }
    } else {
        for (PsiJavaCodeReferenceElement paramRef : paramRefsToInline) {
            InlineUtil.inlineVariable(myParameter, myInitializer, paramRef);
        }
    }
    //delete var if it becomes unused
    for (PsiVariable variable : varsUsedInInitializer) {
        if (variable != null && variable.isValid()) {
            if (ReferencesSearch.search(variable).findFirst() == null) {
                variable.delete();
            }
        }
    }
    ChangeSignatureProcessorBase.doChangeSignature(myChangeInfo, myChangeSignatureUsages);
    if (!thrownExceptions.isEmpty()) {
        for (PsiClassType exception : thrownExceptions) {
            PsiClass exceptionClass = exception.resolve();
            if (exceptionClass != null) {
                PsiUtil.addException(myMethod, exceptionClass);
            }
        }
    }
}
Also used : UsageInfo(com.intellij.usageView.UsageInfo)

Aggregations

UsageInfo (com.intellij.usageView.UsageInfo)283 PsiElement (com.intellij.psi.PsiElement)70 NotNull (org.jetbrains.annotations.NotNull)57 ArrayList (java.util.ArrayList)41 MoveRenameUsageInfo (com.intellij.refactoring.util.MoveRenameUsageInfo)38 IncorrectOperationException (com.intellij.util.IncorrectOperationException)34 PsiFile (com.intellij.psi.PsiFile)33 MultiMap (com.intellij.util.containers.MultiMap)30 VirtualFile (com.intellij.openapi.vfs.VirtualFile)29 Nullable (org.jetbrains.annotations.Nullable)20 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)19 DefaultConstructorImplicitUsageInfo (com.intellij.refactoring.util.usageInfo.DefaultConstructorImplicitUsageInfo)17 NoConstructorClassUsageInfo (com.intellij.refactoring.util.usageInfo.NoConstructorClassUsageInfo)17 Project (com.intellij.openapi.project.Project)16 HashSet (com.intellij.util.containers.HashSet)15 TextRange (com.intellij.openapi.util.TextRange)12 PsiReference (com.intellij.psi.PsiReference)12 ProgressIndicator (com.intellij.openapi.progress.ProgressIndicator)11 PsiClass (com.intellij.psi.PsiClass)11 LocalSearchScope (com.intellij.psi.search.LocalSearchScope)11