Search in sources :

Example 66 with GroovyPsiElement

use of org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement 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)

Example 67 with GroovyPsiElement

use of org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement in project intellij-community by JetBrains.

the class GrUnresolvableLocalCollisionDetector method visitUpstreamCollisions.

private static void visitUpstreamCollisions(PsiElement element, String newName, GroovyPsiElement place, CollidingVariableVisitor visitor) {
    final GrReferenceExpression refExpr = GroovyPsiElementFactory.getInstance(place.getProject()).createReferenceExpressionFromText(newName, place);
    final GroovyResolveResult[] results = refExpr.multiResolve(false);
    for (GroovyResolveResult result : results) {
        final PsiElement resolved = result.getElement();
        if (resolved instanceof GrParameter || (resolved instanceof GrVariable && !(resolved instanceof GrField))) {
            final PsiElement parent = PsiTreeUtil.findCommonParent(resolved, element);
            if (parent != null) {
                PsiElement current = element;
                while (current != null && current != parent) {
                    if (current instanceof PsiMethod || current instanceof PsiClass || current instanceof GrClosableBlock) {
                        return;
                    }
                    current = current.getParent();
                }
            }
            if (!place.getManager().areElementsEquivalent(element, resolved)) {
                visitor.visitCollidingVariable((PsiVariable) resolved);
            }
        }
    }
}
Also used : GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) GrField(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField) GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) PsiMethod(com.intellij.psi.PsiMethod) PsiClass(com.intellij.psi.PsiClass) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter) PsiElement(com.intellij.psi.PsiElement) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)

Example 68 with GroovyPsiElement

use of org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement in project intellij-community by JetBrains.

the class ArgumentListGenerator method generateMultiArg.

private boolean generateMultiArg(GrClosureSignatureUtil.ArgInfo<PsiElement> arg, GrClosureParameter param, PsiSubstitutor substitutor, Project project, GroovyPsiElement context) {
    final PsiType type = param.getType();
    if (type instanceof PsiEllipsisType) {
        for (PsiElement element : arg.args) {
            LOG.assertTrue(element instanceof GrExpression);
            ((GrExpression) element).accept(myExpressionGenerator);
            myBuilder.append(", ");
        }
        if (!arg.args.isEmpty()) {
            myBuilder.delete(myBuilder.length() - 2, myBuilder.length());
            return true;
        } else {
            return false;
        }
    } else if (type instanceof PsiArrayType) {
        myBuilder.append("new ");
        if (arg.args.isEmpty()) {
            TypeWriter.writeType(myBuilder, ((PsiArrayType) type).getComponentType(), context);
            myBuilder.append("[0]");
        } else {
            TypeWriter.writeTypeForNew(myBuilder, type, context);
            myBuilder.append("{");
            for (PsiElement element : arg.args) {
                LOG.assertTrue(element instanceof GrExpression);
                ((GrExpression) element).accept(myExpressionGenerator);
                myBuilder.append(", ");
            }
            if (!arg.args.isEmpty())
                myBuilder.delete(myBuilder.length() - 2, myBuilder.length());
            //if (arg.args.size() > 0) myBuilder.removeFromTheEnd(2);
            myBuilder.append('}');
        }
    } else {
        final GrExpression listOrMap = GroovyRefactoringUtil.generateArgFromMultiArg(substitutor, arg.args, type, project);
        LOG.assertTrue(listOrMap instanceof GrListOrMap);
        listOrMap.accept(myExpressionGenerator);
    }
    return true;
}
Also used : GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GrListOrMap(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)

Example 69 with GroovyPsiElement

use of org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement in project intellij-community by JetBrains.

the class GroovyVariableCanBeFinalInspection method checkVariableAssignedInsideClosureOrAnonymous.

private static boolean checkVariableAssignedInsideClosureOrAnonymous(@NotNull GrControlFlowOwner owner, @NotNull GrVariable variable) {
    final Collection<PsiReference> references = ReferencesSearch.search(variable, variable.getUseScope()).findAll();
    for (final PsiReference reference : references) {
        final PsiElement element = reference.getElement();
        if (!(element instanceof GroovyPsiElement))
            continue;
        final GroovyPsiElement groovyElement = (GroovyPsiElement) element;
        final GroovyPsiElement closure = PsiTreeUtil.getParentOfType(groovyElement, GrClosableBlock.class, GrAnonymousClassDefinition.class);
        if (closure == null || !PsiTreeUtil.isAncestor(owner, closure, false))
            continue;
        if (PsiUtil.isLValue(groovyElement))
            return true;
    }
    return false;
}
Also used : GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)

Example 70 with GroovyPsiElement

use of org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement in project intellij-community by JetBrains.

the class GroovyTypeCheckVisitor method checkNamedArgumentsType.

private void checkNamedArgumentsType(@NotNull CallInfo<?> info) {
    GroovyPsiElement rawCall = info.getCall();
    if (!(rawCall instanceof GrCall))
        return;
    GrCall call = (GrCall) rawCall;
    GrNamedArgument[] namedArguments = PsiUtil.getFirstMapNamedArguments(call);
    if (namedArguments.length == 0)
        return;
    Map<String, NamedArgumentDescriptor> map = GroovyNamedArgumentProvider.getNamedArgumentsFromAllProviders(call, null, false);
    if (map == null)
        return;
    checkNamedArguments(call, namedArguments, map);
}
Also used : NamedArgumentDescriptor(org.jetbrains.plugins.groovy.extensions.NamedArgumentDescriptor) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) GrNamedArgument(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrNamedArgument) GrString(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrString)

Aggregations

GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)98 PsiElement (com.intellij.psi.PsiElement)34 Nullable (org.jetbrains.annotations.Nullable)17 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)17 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)16 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)13 GrArgumentList (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList)12 GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)11 GrMethod (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod)9 GrListOrMap (org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap)8 GrNamedArgument (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrNamedArgument)8 GrApplicationStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrApplicationStatement)8 GrMethodCallExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression)8 GrTypeDefinition (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition)8 Project (com.intellij.openapi.project.Project)7 TextRange (com.intellij.openapi.util.TextRange)7 NotNull (org.jetbrains.annotations.NotNull)7 GroovyRecursiveElementVisitor (org.jetbrains.plugins.groovy.lang.psi.GroovyRecursiveElementVisitor)7 GrVariable (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable)7 GroovyResolveResult (org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult)6