Search in sources :

Example 1 with ExternalUsageInfo

use of com.intellij.refactoring.introduceParameter.ExternalUsageInfo in project intellij-community by JetBrains.

the class ExtractClosureFromClosureProcessor method findUsages.

@NotNull
@Override
protected UsageInfo[] findUsages() {
    final GrVariable var = (GrVariable) myHelper.getToSearchFor();
    if (var != null) {
        final List<UsageInfo> result = new ArrayList<>();
        for (PsiReference ref : ReferencesSearch.search(var)) {
            final PsiElement element = ref.getElement();
            if (element.getLanguage() != GroovyLanguage.INSTANCE) {
                result.add(new OtherLanguageUsageInfo(ref));
                continue;
            }
            final GrCall call = GroovyRefactoringUtil.getCallExpressionByMethodReference(element);
            if (call == null)
                continue;
            result.add(new ExternalUsageInfo(element));
        }
        return result.toArray(new UsageInfo[result.size()]);
    }
    return UsageInfo.EMPTY_ARRAY;
}
Also used : GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) GrCall(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrCall) ArrayList(java.util.ArrayList) PsiReference(com.intellij.psi.PsiReference) ExternalUsageInfo(com.intellij.refactoring.introduceParameter.ExternalUsageInfo) ExternalUsageInfo(com.intellij.refactoring.introduceParameter.ExternalUsageInfo) UsageInfo(com.intellij.usageView.UsageInfo) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with ExternalUsageInfo

use of com.intellij.refactoring.introduceParameter.ExternalUsageInfo in project intellij-community by JetBrains.

the class GrIntroduceClosureParameterProcessor method findUsages.

@NotNull
@Override
protected UsageInfo[] findUsages() {
    ArrayList<UsageInfo> result = new ArrayList<>();
    if (!mySettings.generateDelegate() && toSearchFor != null) {
        Collection<PsiReference> refs;
        if (toSearchFor instanceof GrField) {
            refs = ReferencesSearch.search(toSearchFor).findAll();
            final GrAccessorMethod[] getters = ((GrField) toSearchFor).getGetters();
            for (GrAccessorMethod getter : getters) {
                refs.addAll(MethodReferencesSearch.search(getter, getter.getResolveScope(), true).findAll());
            }
        } else if (toSearchFor instanceof GrVariable) {
            refs = findUsagesForLocal(toReplaceIn, ((GrVariable) toSearchFor));
        } else {
            refs = ReferencesSearch.search(toSearchFor).findAll();
        }
        for (PsiReference ref1 : refs) {
            PsiElement ref = ref1.getElement();
            if (!PsiTreeUtil.isAncestor(toReplaceIn, ref, false)) {
                result.add(new ExternalUsageInfo(ref));
            } else {
                result.add(new ChangedMethodCallInfo(ref));
            }
        }
        if (toSearchFor instanceof GrVariable && !((GrVariable) toSearchFor).hasModifierProperty(PsiModifier.FINAL)) {
            setPreviewUsages(true);
        }
    }
    if (mySettings.replaceAllOccurrences()) {
        PsiElement[] exprs = GroovyIntroduceParameterUtil.getOccurrences(mySettings);
        for (PsiElement expr : exprs) {
            result.add(new InternalUsageInfo(expr));
        }
    } else {
        if (mySettings.getExpression() != null) {
            result.add(new InternalUsageInfo(mySettings.getExpression()));
        }
    }
    final UsageInfo[] usageInfos = result.toArray(new UsageInfo[result.size()]);
    return UsageViewUtil.removeDuplicatedUsages(usageInfos);
}
Also used : ArrayList(java.util.ArrayList) ChangedMethodCallInfo(com.intellij.refactoring.introduceParameter.ChangedMethodCallInfo) GrAccessorMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrAccessorMethod) ExternalUsageInfo(com.intellij.refactoring.introduceParameter.ExternalUsageInfo) ExternalUsageInfo(com.intellij.refactoring.introduceParameter.ExternalUsageInfo) UsageInfo(com.intellij.usageView.UsageInfo) InternalUsageInfo(com.intellij.refactoring.introduceParameter.InternalUsageInfo) InternalUsageInfo(com.intellij.refactoring.introduceParameter.InternalUsageInfo) NotNull(org.jetbrains.annotations.NotNull)

Example 3 with ExternalUsageInfo

use of com.intellij.refactoring.introduceParameter.ExternalUsageInfo in project intellij-community by JetBrains.

the class GroovyIntroduceParameterUtil method detectAccessibilityConflicts.

public static void detectAccessibilityConflicts(@Nullable GroovyPsiElement elementToProcess, final UsageInfo[] usages, MultiMap<PsiElement, String> conflicts, boolean replaceFieldsWithGetters, Project project) {
    if (elementToProcess == null)
        return;
    final ReferencedElementsCollector collector = new ReferencedElementsCollector();
    elementToProcess.accept(collector);
    final List<PsiElement> result = collector.getResult();
    if (result.isEmpty())
        return;
    for (final UsageInfo usageInfo : usages) {
        if (!(usageInfo instanceof ExternalUsageInfo) || !IntroduceParameterUtil.isMethodUsage(usageInfo))
            continue;
        final PsiElement place = usageInfo.getElement();
        for (PsiElement element : result) {
            if (element instanceof PsiField && replaceFieldsWithGetters) {
                //check getter access instead
                final PsiClass psiClass = ((PsiField) element).getContainingClass();
                LOG.assertTrue(psiClass != null);
                final PsiMethod method = GroovyPropertyUtils.findGetterForField((PsiField) element);
                if (method != null) {
                    element = method;
                }
            }
            if (element instanceof PsiMember && !JavaPsiFacade.getInstance(project).getResolveHelper().isAccessible((PsiMember) element, place, null)) {
                String message = RefactoringBundle.message("0.is.not.accessible.from.1.value.for.introduced.parameter.in.that.method.call.will.be.incorrect", RefactoringUIUtil.getDescription(element, true), RefactoringUIUtil.getDescription(ConflictsUtil.getContainer(place), true));
                conflicts.putValue(element, message);
            }
        }
    }
}
Also used : ExternalUsageInfo(com.intellij.refactoring.introduceParameter.ExternalUsageInfo) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) ExternalUsageInfo(com.intellij.refactoring.introduceParameter.ExternalUsageInfo) UsageInfo(com.intellij.usageView.UsageInfo)

Aggregations

ExternalUsageInfo (com.intellij.refactoring.introduceParameter.ExternalUsageInfo)3 UsageInfo (com.intellij.usageView.UsageInfo)3 ArrayList (java.util.ArrayList)2 NotNull (org.jetbrains.annotations.NotNull)2 PsiElement (com.intellij.psi.PsiElement)1 PsiReference (com.intellij.psi.PsiReference)1 ChangedMethodCallInfo (com.intellij.refactoring.introduceParameter.ChangedMethodCallInfo)1 InternalUsageInfo (com.intellij.refactoring.introduceParameter.InternalUsageInfo)1 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)1 GrVariable (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable)1 GrCall (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrCall)1 GrAccessorMethod (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrAccessorMethod)1