Search in sources :

Example 11 with GrAccessorMethod

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrAccessorMethod in project intellij-community by JetBrains.

the class ResolveUtil method isAccessible.

public static boolean isAccessible(@NotNull PsiElement place, @NotNull PsiNamedElement namedElement) {
    if (namedElement instanceof GrField) {
        final GrField field = (GrField) namedElement;
        if (org.jetbrains.plugins.groovy.lang.psi.util.PsiUtil.isAccessible(place, field)) {
            return true;
        }
        for (GrAccessorMethod method : field.getGetters()) {
            if (org.jetbrains.plugins.groovy.lang.psi.util.PsiUtil.isAccessible(place, method)) {
                return true;
            }
        }
        final GrAccessorMethod setter = field.getSetter();
        if (setter != null && org.jetbrains.plugins.groovy.lang.psi.util.PsiUtil.isAccessible(place, setter)) {
            return true;
        }
        return false;
    }
    return !(namedElement instanceof PsiMember) || org.jetbrains.plugins.groovy.lang.psi.util.PsiUtil.isAccessible(place, ((PsiMember) namedElement));
}
Also used : GrAccessorMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrAccessorMethod)

Example 12 with GrAccessorMethod

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrAccessorMethod in project intellij-community by JetBrains.

the class GroovyFieldFindUsagesHandlerFactory method createFindUsagesHandler.

@Override
public FindUsagesHandler createFindUsagesHandler(@NotNull PsiElement element, boolean forHighlightUsages) {
    return new JavaFindUsagesHandler(element, this) {

        @NotNull
        @Override
        public PsiElement[] getSecondaryElements() {
            PsiElement element = getPsiElement();
            final PsiField field = (PsiField) element;
            PsiClass containingClass = field.getContainingClass();
            if (containingClass != null) {
                PsiMethod[] getters = GroovyPropertyUtils.getAllGettersByField(field);
                PsiMethod[] setters = GroovyPropertyUtils.getAllSettersByField(field);
                if (getters.length + setters.length > 0) {
                    final boolean doSearch;
                    if (arePhysical(getters) || arePhysical(setters)) {
                        if (ApplicationManager.getApplication().isUnitTestMode())
                            return PsiElement.EMPTY_ARRAY;
                        doSearch = Messages.showYesNoDialog(FindBundle.message("find.field.accessors.prompt", field.getName()), FindBundle.message("find.field.accessors.title"), Messages.getQuestionIcon()) == Messages.YES;
                    } else {
                        doSearch = true;
                    }
                    if (doSearch) {
                        final List<PsiElement> elements = new ArrayList<>();
                        for (PsiMethod getter : getters) {
                            ContainerUtil.addAll(elements, SuperMethodWarningUtil.checkSuperMethods(getter, ACTION_STRING));
                        }
                        for (PsiMethod setter : setters) {
                            ContainerUtil.addAll(elements, SuperMethodWarningUtil.checkSuperMethods(setter, ACTION_STRING));
                        }
                        for (Iterator<PsiElement> iterator = elements.iterator(); iterator.hasNext(); ) {
                            if (iterator.next() instanceof GrAccessorMethod)
                                iterator.remove();
                        }
                        return PsiUtilCore.toPsiElementArray(elements);
                    } else {
                        return PsiElement.EMPTY_ARRAY;
                    }
                }
            }
            return super.getSecondaryElements();
        }
    };
}
Also used : GrAccessorMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrAccessorMethod) PsiMethod(com.intellij.psi.PsiMethod) PsiField(com.intellij.psi.PsiField) PsiClass(com.intellij.psi.PsiClass) ArrayList(java.util.ArrayList) JavaFindUsagesHandler(com.intellij.find.findUsages.JavaFindUsagesHandler) PsiElement(com.intellij.psi.PsiElement)

Example 13 with GrAccessorMethod

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrAccessorMethod 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 14 with GrAccessorMethod

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrAccessorMethod in project intellij-community by JetBrains.

the class GrIntroduceClosureParameterProcessor method processExternalUsage.

private static void processExternalUsage(UsageInfo usage, GrIntroduceParameterSettings settings, PsiElement expression) {
    final PsiElement element = usage.getElement();
    GrCall callExpression = GroovyRefactoringUtil.getCallExpressionByMethodReference(element);
    if (callExpression == null) {
        final PsiElement parent = element.getParent();
        if (parent instanceof GrReferenceExpression && element == ((GrReferenceExpression) parent).getQualifier() && "call".equals(((GrReferenceExpression) parent).getReferenceName())) {
            callExpression = GroovyRefactoringUtil.getCallExpressionByMethodReference(parent);
        }
    }
    if (callExpression == null)
        return;
    //check for x.getFoo()(args)
    if (callExpression instanceof GrMethodCall) {
        final GrExpression invoked = ((GrMethodCall) callExpression).getInvokedExpression();
        if (invoked instanceof GrReferenceExpression) {
            final GroovyResolveResult result = ((GrReferenceExpression) invoked).advancedResolve();
            final PsiElement resolved = result.getElement();
            if (resolved instanceof GrAccessorMethod && !result.isInvokedOnProperty()) {
                PsiElement actualCallExpression = callExpression.getParent();
                if (actualCallExpression instanceof GrCall) {
                    callExpression = (GrCall) actualCallExpression;
                }
            }
        }
    }
    GrArgumentList argList = callExpression.getArgumentList();
    LOG.assertTrue(argList != null);
    GrExpression[] oldArgs = argList.getExpressionArguments();
    GrClosableBlock toReplaceIn = (GrClosableBlock) settings.getToReplaceIn();
    GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(settings.getProject());
    final GrExpression anchor = getAnchorForArgument(oldArgs, toReplaceIn.isVarArgs(), toReplaceIn.getParameterList());
    GrClosureSignature signature = GrClosureSignatureUtil.createSignature(callExpression);
    if (signature == null)
        signature = GrClosureSignatureUtil.createSignature(toReplaceIn);
    final GrClosureSignatureUtil.ArgInfo<PsiElement>[] actualArgs = GrClosureSignatureUtil.mapParametersToArguments(signature, callExpression.getNamedArguments(), callExpression.getExpressionArguments(), callExpression.getClosureArguments(), callExpression, true, true);
    if (PsiTreeUtil.isAncestor(toReplaceIn, callExpression, false)) {
        argList.addAfter(factory.createExpressionFromText(settings.getName()), anchor);
    } else {
        PsiElement initializer = ExpressionConverter.getExpression(expression, GroovyLanguage.INSTANCE, settings.getProject());
        LOG.assertTrue(initializer instanceof GrExpression);
        GrExpression newArg = GroovyIntroduceParameterUtil.addClosureToCall(initializer, argList);
        if (newArg == null) {
            final PsiElement dummy = argList.addAfter(factory.createExpressionFromText("1"), anchor);
            newArg = ((GrExpression) dummy).replaceWithExpression((GrExpression) initializer, true);
        }
        new OldReferencesResolver(callExpression, newArg, toReplaceIn, settings.replaceFieldsWithGetters(), initializer, signature, actualArgs, toReplaceIn.getParameters()).resolve();
        ChangeContextUtil.clearContextInfo(initializer);
        //newarg can be replaced by OldReferenceResolve
        if (newArg.isValid()) {
            JavaCodeStyleManager.getInstance(newArg.getProject()).shortenClassReferences(newArg);
            CodeStyleManager.getInstance(settings.getProject()).reformat(newArg);
        }
    }
    if (actualArgs == null) {
        GroovyIntroduceParameterUtil.removeParamsFromUnresolvedCall(callExpression, toReplaceIn.getParameters(), settings.parametersToRemove());
    } else {
        GroovyIntroduceParameterUtil.removeParametersFromCall(actualArgs, settings.parametersToRemove());
    }
    if (argList.getAllArguments().length == 0 && PsiImplUtil.hasClosureArguments(callExpression)) {
        final GrArgumentList emptyArgList = ((GrMethodCallExpression) factory.createExpressionFromText("foo{}")).getArgumentList();
        argList.replace(emptyArgList);
    }
}
Also used : GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) GrAccessorMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrAccessorMethod) GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) GrArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList) GrMethodCallExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression) GrClosureSignature(org.jetbrains.plugins.groovy.lang.psi.api.signatures.GrClosureSignature) OldReferencesResolver(org.jetbrains.plugins.groovy.refactoring.introduce.parameter.java2groovy.OldReferencesResolver)

Example 15 with GrAccessorMethod

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrAccessorMethod in project intellij-community by JetBrains.

the class GrVariableInliner method getConflicts.

@Override
@Nullable
public MultiMap<PsiElement, String> getConflicts(@NotNull PsiReference reference, @NotNull PsiElement referenced) {
    MultiMap<PsiElement, String> conflicts = new MultiMap<>();
    GrExpression expr = (GrExpression) reference.getElement();
    if (expr.getParent() instanceof GrAssignmentExpression) {
        GrAssignmentExpression parent = (GrAssignmentExpression) expr.getParent();
        if (expr.equals(parent.getLValue())) {
            conflicts.putValue(expr, GroovyRefactoringBundle.message("local.varaible.is.lvalue"));
        }
    }
    if ((referenced instanceof GrAccessorMethod || referenced instanceof GrField) && expr instanceof GrReferenceExpression) {
        final GroovyResolveResult resolveResult = ((GrReferenceExpression) expr).advancedResolve();
        if (resolveResult.getElement() instanceof GrAccessorMethod && !resolveResult.isInvokedOnProperty()) {
            final PsiElement parent = expr.getParent();
            if (!(parent instanceof GrCall && parent instanceof GrExpression)) {
                conflicts.putValue(expr, GroovyRefactoringBundle.message("reference.to.accessor.0.is.used", CommonRefactoringUtil.htmlEmphasize(PsiFormatUtil.formatMethod((GrAccessorMethod) resolveResult.getElement(), PsiSubstitutor.EMPTY, PsiFormatUtilBase.SHOW_NAME | PsiFormatUtilBase.SHOW_PARAMETERS, PsiFormatUtilBase.SHOW_TYPE))));
            }
        }
    }
    return conflicts;
}
Also used : MultiMap(com.intellij.util.containers.MultiMap) GrAccessorMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrAccessorMethod) GrField(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField) GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) GrAssignmentExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrAssignmentExpression) GrCall(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrCall) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) PsiElement(com.intellij.psi.PsiElement) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

GrAccessorMethod (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrAccessorMethod)25 GrField (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField)13 GroovyResolveResult (org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult)7 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)6 PsiElement (com.intellij.psi.PsiElement)5 ArrayList (java.util.ArrayList)5 NotNull (org.jetbrains.annotations.NotNull)5 Nullable (org.jetbrains.annotations.Nullable)5 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)5 GrMethod (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod)4 MultiMap (com.intellij.util.containers.MultiMap)3 GrClosureSignature (org.jetbrains.plugins.groovy.lang.psi.api.signatures.GrClosureSignature)3 GrCall (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrCall)3 Editor (com.intellij.openapi.editor.Editor)2 Project (com.intellij.openapi.project.Project)2 PsiMethod (com.intellij.psi.PsiMethod)2 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)2 UsageInfo (com.intellij.usageView.UsageInfo)2 GroovyFileBase (org.jetbrains.plugins.groovy.lang.psi.GroovyFileBase)2 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)2