Search in sources :

Example 66 with GrReferenceExpression

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression in project intellij-community by JetBrains.

the class SpockUtils method createVariableMap.

// See org.spockframework.compiler.WhereBlockRewriter
public static Map<String, SpockVariableDescriptor> createVariableMap(GrMethod method) {
    GrOpenBlock block = method.getBlock();
    if (block == null)
        return Collections.emptyMap();
    PsiElement elementUnderLabel = null;
    PsiElement elementAfterLabel = null;
    main: for (PsiElement e = block.getFirstChild(); e != null; e = e.getNextSibling()) {
        if (e instanceof GrLabeledStatement) {
            GrLabeledStatement l = (GrLabeledStatement) e;
            elementAfterLabel = l.getNextSibling();
            while (true) {
                GrStatement statement = l.getStatement();
                if ("where".equals(l.getName())) {
                    elementUnderLabel = statement;
                    break main;
                }
                if (statement instanceof GrLabeledStatement) {
                    l = (GrLabeledStatement) statement;
                    continue;
                }
                break;
            }
        }
    }
    if (elementUnderLabel == null)
        return Collections.emptyMap();
    Map<String, SpockVariableDescriptor> res = new HashMap<>();
    PsiElement e = elementUnderLabel;
    while (e != null) {
        if (e instanceof GrBinaryExpression && ((GrBinaryExpression) e).getOperationTokenType() == GroovyElementTypes.COMPOSITE_LSHIFT_SIGN) {
            GrBinaryExpression shift = (GrBinaryExpression) e;
            GrExpression leftOperand = shift.getLeftOperand();
            GrExpression rightOperand = shift.getRightOperand();
            if (leftOperand instanceof GrReferenceExpression) {
                String name = getNameByReference(leftOperand);
                if (name != null) {
                    SpockVariableDescriptor descriptor = new SpockVariableDescriptor(leftOperand, name);
                    descriptor.addExpressionOfCollection(rightOperand);
                    res.put(name, descriptor);
                }
            } else if (leftOperand instanceof GrListOrMap) {
                GrExpression[] variableDefinitions = ((GrListOrMap) leftOperand).getInitializers();
                SpockVariableDescriptor[] variables = createVariables(res, Arrays.asList(variableDefinitions));
                if (rightOperand instanceof GrListOrMap) {
                    for (GrExpression expression : ((GrListOrMap) rightOperand).getInitializers()) {
                        if (expression instanceof GrListOrMap) {
                            add(variables, Arrays.asList(((GrListOrMap) expression).getInitializers()));
                        } else {
                            for (SpockVariableDescriptor variable : variables) {
                                if (variable != null) {
                                    variable.addExpressionOfCollection(expression);
                                }
                            }
                        }
                    }
                }
            }
        } else if (e instanceof GrAssignmentExpression) {
            GrAssignmentExpression assExpr = (GrAssignmentExpression) e;
            GrExpression lValue = assExpr.getLValue();
            String name = getNameByReference(lValue);
            if (name != null) {
                res.put(name, new SpockVariableDescriptor(lValue, name).addExpression(assExpr.getRValue()));
            }
        } else if (isOrStatement(e)) {
            // See org.spockframework.compiler.WhereBlockRewriter#rewriteTableLikeParameterization()
            List<GrExpression> variableDefinitions = new ArrayList<>();
            splitOr(variableDefinitions, (GrExpression) e);
            SpockVariableDescriptor[] variables = createVariables(res, variableDefinitions);
            List<GrExpression> row = new ArrayList<>();
            PsiElement rowElement = getNext(e, elementUnderLabel, elementAfterLabel);
            while (isOrStatement(rowElement)) {
                row.clear();
                splitOr(row, (GrExpression) rowElement);
                add(variables, row);
                rowElement = getNext(rowElement, elementUnderLabel, elementAfterLabel);
            }
            e = rowElement;
            continue;
        }
        e = getNext(e, elementUnderLabel, elementAfterLabel);
    }
    return res;
}
Also used : GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GrListOrMap(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap) GrBinaryExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrBinaryExpression) GrLabeledStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrLabeledStatement) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) GrAssignmentExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrAssignmentExpression) GrOpenBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock) PsiElement(com.intellij.psi.PsiElement)

Example 67 with GrReferenceExpression

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression in project intellij-community by JetBrains.

the class SpockUtils method getNameByReference.

@Nullable
public static String getNameByReference(@Nullable PsiElement expression) {
    if (!(expression instanceof GrReferenceExpression))
        return null;
    PsiElement firstChild = expression.getFirstChild();
    if (firstChild != expression.getLastChild() || !PsiImplUtil.isLeafElementOfType(firstChild, GroovyTokenTypes.mIDENT))
        return null;
    GrReferenceExpression ref = (GrReferenceExpression) expression;
    if (ref.isQualified())
        return null;
    return ref.getReferenceName();
}
Also used : PsiElement(com.intellij.psi.PsiElement) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) Nullable(org.jetbrains.annotations.Nullable)

Example 68 with GrReferenceExpression

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression in project intellij-community by JetBrains.

the class UnusedDefInspection method isUsedInTopLevelFlowOnly.

private static boolean isUsedInTopLevelFlowOnly(PsiElement element) {
    GrVariable var = null;
    if (element instanceof GrVariable) {
        var = (GrVariable) element;
    } else if (element instanceof GrReferenceExpression) {
        final PsiElement resolved = ((GrReferenceExpression) element).resolve();
        if (resolved instanceof GrVariable)
            var = (GrVariable) resolved;
    }
    if (var != null) {
        final GroovyPsiElement scope = ControlFlowUtils.findControlFlowOwner(var);
        if (scope == null) {
            PsiFile file = var.getContainingFile();
            if (file == null) {
                LOG.error("no file??? var of type" + var.getClass().getCanonicalName());
                return false;
            } else {
                TextRange range = var.getTextRange();
                LOG.error("var: " + var.getName() + ", offset:" + (range != null ? range.getStartOffset() : -1));
                return false;
            }
        }
        return ReferencesSearch.search(var, var.getUseScope()).forEach(ref -> ControlFlowUtils.findControlFlowOwner(ref.getElement()) == scope);
    }
    return true;
}
Also used : GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) PsiFile(com.intellij.psi.PsiFile) TextRange(com.intellij.openapi.util.TextRange) 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 69 with GrReferenceExpression

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression in project intellij-community by JetBrains.

the class ControlFlowUtils method inferWriteAccessMap.

@NotNull
public static List<BitSet> inferWriteAccessMap(final Instruction[] flow, final GrVariable var) {
    final Semilattice<BitSet> sem = new Semilattice<BitSet>() {

        @NotNull
        @Override
        public BitSet join(@NotNull List<BitSet> ins) {
            BitSet result = new BitSet(flow.length);
            for (BitSet set : ins) {
                result.or(set);
            }
            return result;
        }

        @Override
        public boolean eq(@NotNull BitSet e1, @NotNull BitSet e2) {
            return e1.equals(e2);
        }
    };
    DfaInstance<BitSet> dfa = new DfaInstance<BitSet>() {

        @Override
        public void fun(@NotNull BitSet bitSet, @NotNull Instruction instruction) {
            if (!(instruction instanceof ReadWriteVariableInstruction))
                return;
            if (!((ReadWriteVariableInstruction) instruction).isWrite())
                return;
            final PsiElement element = instruction.getElement();
            if (element instanceof GrVariable && element != var)
                return;
            if (element instanceof GrReferenceExpression) {
                final GrReferenceExpression ref = (GrReferenceExpression) element;
                if (ref.isQualified() || ref.resolve() != var) {
                    return;
                }
            }
            if (!((ReadWriteVariableInstruction) instruction).getVariableName().equals(var.getName())) {
                return;
            }
            bitSet.clear();
            bitSet.set(instruction.num());
        }

        @NotNull
        @Override
        public BitSet initial() {
            return new BitSet(flow.length);
        }
    };
    return new DFAEngine<>(flow, dfa, sem).performForceDFA();
}
Also used : ReadWriteVariableInstruction(org.jetbrains.plugins.groovy.lang.psi.controlFlow.ReadWriteVariableInstruction) Instruction(org.jetbrains.plugins.groovy.lang.psi.controlFlow.Instruction) MaybeReturnInstruction(org.jetbrains.plugins.groovy.lang.psi.controlFlow.impl.MaybeReturnInstruction) AfterCallInstruction(org.jetbrains.plugins.groovy.lang.psi.controlFlow.AfterCallInstruction) ThrowingInstruction(org.jetbrains.plugins.groovy.lang.psi.controlFlow.impl.ThrowingInstruction) IfEndInstruction(org.jetbrains.plugins.groovy.lang.psi.controlFlow.impl.IfEndInstruction) ReadWriteVariableInstruction(org.jetbrains.plugins.groovy.lang.psi.controlFlow.ReadWriteVariableInstruction) NotNull(org.jetbrains.annotations.NotNull) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) DfaInstance(org.jetbrains.plugins.groovy.lang.psi.dataFlow.DfaInstance) Semilattice(org.jetbrains.plugins.groovy.lang.psi.dataFlow.Semilattice) PsiElement(com.intellij.psi.PsiElement) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 70 with GrReferenceExpression

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression in project intellij-community by JetBrains.

the class GrReferenceAdjuster method shortenReferenceInner.

private static <Qualifier extends PsiElement> boolean shortenReferenceInner(@NotNull GrQualifiedReference<Qualifier> ref, boolean addImports, boolean incomplete, boolean useFqInJavadoc, boolean useFqInCode) {
    final Qualifier qualifier = ref.getQualifier();
    if (qualifier == null || PsiUtil.isSuperReference(qualifier) || cannotShortenInContext(ref)) {
        return false;
    }
    if (ref instanceof GrReferenceExpression) {
        final GrTypeArgumentList typeArgs = ((GrReferenceExpression) ref).getTypeArgumentList();
        if (typeArgs != null && typeArgs.getTypeArgumentElements().length > 0) {
            return false;
        }
    }
    if (!shorteningIsMeaningfully(ref, useFqInJavadoc, useFqInCode))
        return false;
    final PsiElement resolved = resolveRef(ref, incomplete);
    if (resolved == null)
        return false;
    if (!checkCopyWithoutQualifier(ref, addImports, resolved))
        return false;
    ref.setQualifier(null);
    return true;
}
Also used : GrTypeArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeArgumentList) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)

Aggregations

GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)177 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)92 PsiElement (com.intellij.psi.PsiElement)56 Nullable (org.jetbrains.annotations.Nullable)28 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)27 GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)26 NotNull (org.jetbrains.annotations.NotNull)25 GroovyResolveResult (org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult)22 GrMethodCall (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrMethodCall)22 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)21 GrArgumentList (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList)17 GrParameter (org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter)16 GrField (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField)15 GroovyRecursiveElementVisitor (org.jetbrains.plugins.groovy.lang.psi.GroovyRecursiveElementVisitor)14 GrVariable (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable)14 PsiType (com.intellij.psi.PsiType)13 GrAssignmentExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrAssignmentExpression)12 Project (com.intellij.openapi.project.Project)11 ArrayList (java.util.ArrayList)11 GrMethodCallExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression)11