Search in sources :

Example 41 with UsageInfo

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

the class InheritanceToDelegationProcessor method analyzeConflicts.

private void analyzeConflicts(UsageInfo[] usage, MultiMap<PsiElement, String> conflicts) {
    HashMap<PsiElement, HashSet<PsiElement>> reportedNonDelegatedUsages = new HashMap<>();
    HashMap<PsiClass, HashSet<PsiElement>> reportedUpcasts = new HashMap<>();
    //    HashSet reportedObjectUpcasts = new HashSet();
    //    final String nameJavaLangObject = ConflictsUtil.htmlEmphasize("java.lang.Object");
    final String classDescription = RefactoringUIUtil.getDescription(myClass, false);
    for (UsageInfo aUsage : usage) {
        final PsiElement element = aUsage.getElement();
        if (aUsage instanceof InheritanceToDelegationUsageInfo) {
            InheritanceToDelegationUsageInfo usageInfo = (InheritanceToDelegationUsageInfo) aUsage;
            /*if (usageInfo instanceof ObjectUpcastedUsageInfo) {
         PsiElement container = ConflictsUtil.getContainer(usageInfo.element);
         if (!reportedObjectUpcasts.contains(container)) {
           String message = "An instance of " + classDescription + " is upcasted to "
                   + nameJavaLangObject + " in " + ConflictsUtil.getDescription(container, true) + ".";
           conflicts.add(message);
           reportedObjectUpcasts.add(container);
         }
       } else*/
            if (!myIsDelegateOtherMembers && !usageInfo.getDelegateFieldAccessible().isAccessible()) {
                if (usageInfo instanceof NonDelegatedMemberUsageInfo) {
                    final PsiElement nonDelegatedMember = ((NonDelegatedMemberUsageInfo) usageInfo).nonDelegatedMember;
                    HashSet<PsiElement> reportedContainers = reportedNonDelegatedUsages.get(nonDelegatedMember);
                    if (reportedContainers == null) {
                        reportedContainers = new HashSet<>();
                        reportedNonDelegatedUsages.put(nonDelegatedMember, reportedContainers);
                    }
                    final PsiElement container = ConflictsUtil.getContainer(element);
                    if (!reportedContainers.contains(container)) {
                        String message = RefactoringBundle.message("0.uses.1.of.an.instance.of.a.2", RefactoringUIUtil.getDescription(container, true), RefactoringUIUtil.getDescription(nonDelegatedMember, true), classDescription);
                        conflicts.putValue(container, CommonRefactoringUtil.capitalize(message));
                        reportedContainers.add(container);
                    }
                } else if (usageInfo instanceof UpcastedUsageInfo) {
                    final PsiClass upcastedTo = ((UpcastedUsageInfo) usageInfo).upcastedTo;
                    HashSet<PsiElement> reportedContainers = reportedUpcasts.get(upcastedTo);
                    if (reportedContainers == null) {
                        reportedContainers = new HashSet<>();
                        reportedUpcasts.put(upcastedTo, reportedContainers);
                    }
                    final PsiElement container = ConflictsUtil.getContainer(element);
                    if (!reportedContainers.contains(container)) {
                        String message = RefactoringBundle.message("0.upcasts.an.instance.of.1.to.2", RefactoringUIUtil.getDescription(container, true), classDescription, RefactoringUIUtil.getDescription(upcastedTo, false));
                        conflicts.putValue(container, CommonRefactoringUtil.capitalize(message));
                        reportedContainers.add(container);
                    }
                }
            }
        } else if (aUsage instanceof NoLongerOverridingSubClassMethodUsageInfo) {
            NoLongerOverridingSubClassMethodUsageInfo info = (NoLongerOverridingSubClassMethodUsageInfo) aUsage;
            String message = RefactoringBundle.message("0.will.no.longer.override.1", RefactoringUIUtil.getDescription(info.getSubClassMethod(), true), RefactoringUIUtil.getDescription(info.getOverridenMethod(), true));
            conflicts.putValue(info.getSubClassMethod(), message);
        }
    }
}
Also used : HashMap(com.intellij.util.containers.HashMap) UsageInfo(com.intellij.usageView.UsageInfo)

Example 42 with UsageInfo

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

the class InlineConstantFieldProcessor method preprocessUsages.

@Override
protected boolean preprocessUsages(@NotNull Ref<UsageInfo[]> refUsages) {
    UsageInfo[] usagesIn = refUsages.get();
    MultiMap<PsiElement, String> conflicts = new MultiMap<>();
    ReferencedElementsCollector collector = new ReferencedElementsCollector();
    PsiExpression initializer = InlineConstantFieldHandler.getInitializer(myField);
    LOG.assertTrue(initializer != null);
    initializer.accept(collector);
    HashSet<PsiMember> referencedWithVisibility = collector.myReferencedMembers;
    PsiResolveHelper resolveHelper = JavaPsiFacade.getInstance(myField.getProject()).getResolveHelper();
    for (UsageInfo info : usagesIn) {
        PsiElement element = info.getElement();
        if (element instanceof PsiExpression && (!myField.hasModifierProperty(PsiModifier.FINAL) || myInlineThisOnly) && isAccessedForWriting((PsiExpression) element)) {
            String message = RefactoringBundle.message("0.is.used.for.writing.in.1", RefactoringUIUtil.getDescription(myField, true), RefactoringUIUtil.getDescription(ConflictsUtil.getContainer(element), true));
            conflicts.putValue(element, message);
        }
        for (PsiMember member : referencedWithVisibility) {
            if (!resolveHelper.isAccessible(member, element, null)) {
                String message = RefactoringBundle.message("0.will.not.be.accessible.from.1.after.inlining", RefactoringUIUtil.getDescription(member, true), RefactoringUIUtil.getDescription(ConflictsUtil.getContainer(element), true));
                conflicts.putValue(member, message);
            }
        }
    }
    if (!myInlineThisOnly) {
        for (UsageInfo info : usagesIn) {
            if (info instanceof UsageFromJavaDoc) {
                final PsiElement element = info.getElement();
                if (element instanceof PsiDocMethodOrFieldRef && !PsiTreeUtil.isAncestor(myField, element, false)) {
                    conflicts.putValue(element, "Inlined method is used in javadoc");
                }
            }
        }
    }
    return showConflicts(conflicts, usagesIn);
}
Also used : MultiMap(com.intellij.util.containers.MultiMap) PsiDocMethodOrFieldRef(com.intellij.psi.impl.source.javadoc.PsiDocMethodOrFieldRef) UsageInfo(com.intellij.usageView.UsageInfo)

Example 43 with UsageInfo

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

the class InlineConstantFieldProcessor method performRefactoring.

@Override
protected void performRefactoring(@NotNull UsageInfo[] usages) {
    PsiExpression initializer = InlineConstantFieldHandler.getInitializer(myField);
    LOG.assertTrue(initializer != null);
    initializer = normalize(initializer);
    final Set<PsiAssignmentExpression> assignments = new HashSet<>();
    for (UsageInfo info : usages) {
        if (info instanceof UsageFromJavaDoc)
            continue;
        if (info instanceof NonCodeUsageInfo)
            continue;
        final PsiElement element = info.getElement();
        if (element == null)
            continue;
        try {
            if (element instanceof PsiExpression) {
                inlineExpressionUsage((PsiExpression) element, initializer, assignments);
            } else {
                PsiImportStaticStatement importStaticStatement = PsiTreeUtil.getParentOfType(element, PsiImportStaticStatement.class);
                LOG.assertTrue(importStaticStatement != null, element.getText());
                importStaticStatement.delete();
            }
        } catch (IncorrectOperationException e) {
            LOG.error(e);
        }
    }
    for (PsiAssignmentExpression assignment : assignments) {
        assignment.delete();
    }
    if (!myInlineThisOnly && myDeleteDeclaration) {
        try {
            myField.delete();
        } catch (IncorrectOperationException e) {
            LOG.error(e);
        }
    }
}
Also used : IncorrectOperationException(com.intellij.util.IncorrectOperationException) UsageInfo(com.intellij.usageView.UsageInfo)

Example 44 with UsageInfo

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

the class InlineParameterExpressionProcessor method findUsages.

@NotNull
@Override
protected UsageInfo[] findUsages() {
    int parameterIndex = myMethod.getParameterList().getParameterIndex(myParameter);
    final Map<PsiVariable, PsiElement> localToParamRef = new HashMap<>();
    final PsiExpression[] arguments = myMethodCall.getArgumentList().getExpressions();
    for (int i = 0; i < arguments.length; i++) {
        if (i != parameterIndex && arguments[i] instanceof PsiReferenceExpression) {
            final PsiReferenceExpression referenceExpression = (PsiReferenceExpression) arguments[i];
            final PsiElement element = referenceExpression.resolve();
            if (element instanceof PsiLocalVariable || element instanceof PsiParameter) {
                final PsiParameter param = myMethod.getParameterList().getParameters()[i];
                final PsiExpression paramRef = JavaPsiFacade.getInstance(myMethod.getProject()).getElementFactory().createExpressionFromText(param.getName(), myMethod);
                localToParamRef.put((PsiVariable) element, paramRef);
            }
        }
    }
    final List<UsageInfo> result = new ArrayList<>();
    myInitializer.accept(new JavaRecursiveElementVisitor() {

        @Override
        public void visitReferenceExpression(final PsiReferenceExpression expression) {
            super.visitReferenceExpression(expression);
            final PsiElement element = expression.resolve();
            if (element instanceof PsiLocalVariable) {
                final PsiLocalVariable localVariable = (PsiLocalVariable) element;
                final PsiElement[] elements = DefUseUtil.getDefs(myCallingBlock, localVariable, expression);
                if (elements.length == 1) {
                    PsiExpression localInitializer = null;
                    if (elements[0] instanceof PsiLocalVariable) {
                        localInitializer = ((PsiLocalVariable) elements[0]).getInitializer();
                    } else if (elements[0] instanceof PsiAssignmentExpression) {
                        localInitializer = ((PsiAssignmentExpression) elements[0]).getRExpression();
                    } else if (elements[0] instanceof PsiReferenceExpression) {
                        final PsiReferenceExpression refElement = (PsiReferenceExpression) elements[0];
                        final PsiElement parent = refElement.getParent();
                        if (parent instanceof PsiAssignmentExpression && ((PsiAssignmentExpression) parent).getLExpression() == refElement) {
                            localInitializer = ((PsiAssignmentExpression) parent).getRExpression();
                        }
                    }
                    if (localInitializer != null) {
                        final PsiElement replacement;
                        if (localToParamRef.containsKey(localVariable)) {
                            replacement = localToParamRef.get(localVariable);
                        } else {
                            replacement = replaceArgs(localToParamRef, localInitializer.copy());
                        }
                        result.add(new LocalReplacementUsageInfo(expression, replacement));
                    }
                }
            }
        }
    });
    if (!myCreateLocal) {
        for (PsiReference ref : ReferencesSearch.search(myParameter).findAll()) {
            result.add(new UsageInfo(ref));
        }
    }
    final PsiParameter[] parameters = myMethod.getParameterList().getParameters();
    final List<ParameterInfoImpl> psiParameters = new ArrayList<>();
    int paramIdx = 0;
    final String paramName = myParameter.getName();
    for (PsiParameter param : parameters) {
        if (!Comparing.strEqual(paramName, param.getName())) {
            psiParameters.add(new ParameterInfoImpl(paramIdx, param.getName(), param.getType()));
        }
        paramIdx++;
    }
    PsiType returnType = myMethod.getReturnType();
    myChangeInfo = new JavaChangeInfoImpl(VisibilityUtil.getVisibilityModifier(myMethod.getModifierList()), myMethod, myMethod.getName(), returnType != null ? CanonicalTypes.createTypeWrapper(returnType) : null, psiParameters.toArray(new ParameterInfoImpl[psiParameters.size()]), null, false, Collections.emptySet(), Collections.emptySet());
    myChangeSignatureUsages = ChangeSignatureProcessorBase.findUsages(myChangeInfo);
    final UsageInfo[] usageInfos = result.toArray(new UsageInfo[result.size()]);
    return UsageViewUtil.removeDuplicatedUsages(usageInfos);
}
Also used : UsageInfo(com.intellij.usageView.UsageInfo) JavaChangeInfoImpl(com.intellij.refactoring.changeSignature.JavaChangeInfoImpl) ParameterInfoImpl(com.intellij.refactoring.changeSignature.ParameterInfoImpl) NotNull(org.jetbrains.annotations.NotNull)

Example 45 with UsageInfo

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

the class InlineParameterExpressionProcessor method preprocessUsages.

@Override
protected boolean preprocessUsages(@NotNull Ref<UsageInfo[]> refUsages) {
    final MultiMap<PsiElement, String> conflicts = new MultiMap<>();
    JavaSafeDeleteProcessor.collectMethodConflicts(conflicts, myMethod, myParameter);
    final UsageInfo[] usages = refUsages.get();
    final InaccessibleExpressionsDetector detector = new InaccessibleExpressionsDetector(conflicts);
    myInitializer.accept(detector);
    for (UsageInfo usage : usages) {
        if (usage instanceof LocalReplacementUsageInfo) {
            final PsiElement replacement = ((LocalReplacementUsageInfo) usage).getReplacement();
            if (replacement != null) {
                replacement.accept(detector);
            }
        }
    }
    final Set<PsiVariable> vars = new HashSet<>();
    for (UsageInfo usageInfo : usages) {
        if (usageInfo instanceof LocalReplacementUsageInfo) {
            final PsiVariable var = ((LocalReplacementUsageInfo) usageInfo).getVariable();
            if (var != null) {
                vars.add(var);
            }
        }
    }
    for (PsiVariable var : vars) {
        for (PsiReference ref : ReferencesSearch.search(var)) {
            final PsiElement element = ref.getElement();
            if (element instanceof PsiExpression && isAccessedForWriting((PsiExpression) element)) {
                conflicts.putValue(element, "Parameter initializer depends on value which is not available inside method and cannot be inlined");
                break;
            }
        }
    }
    return showConflicts(conflicts, usages);
}
Also used : MultiMap(com.intellij.util.containers.MultiMap) 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