Search in sources :

Example 66 with GrVariable

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

the class InvalidWriteAccessSearcher method findInvalidWriteAccess.

@Nullable
public static List<ReadWriteVariableInstruction> findInvalidWriteAccess(@NotNull Instruction[] flow, @NotNull Map<String, GrVariable> variables, @NotNull Set<GrVariable> alreadyInitialized) {
    DFAEngine<MyData> engine = new DFAEngine<>(flow, new MyDFAInstance(), new MySemilattice());
    final List<MyData> dfaResult = engine.performDFAWithTimeout();
    if (dfaResult == null)
        return null;
    List<ReadWriteVariableInstruction> result = ContainerUtil.newArrayList();
    for (int i = 0; i < flow.length; i++) {
        Instruction instruction = flow[i];
        if (instruction instanceof ReadWriteVariableInstruction && ((ReadWriteVariableInstruction) instruction).isWrite()) {
            final MyData initialized = dfaResult.get(i);
            final GrVariable var = variables.get(((ReadWriteVariableInstruction) instruction).getVariableName());
            if (alreadyInitialized.contains(var)) {
                if (initialized.isInitialized(((ReadWriteVariableInstruction) instruction).getVariableName())) {
                    result.add((ReadWriteVariableInstruction) instruction);
                }
            } else {
                if (initialized.isOverInitialized(((ReadWriteVariableInstruction) instruction).getVariableName())) {
                    result.add((ReadWriteVariableInstruction) instruction);
                }
            }
        }
    }
    return result;
}
Also used : DFAEngine(org.jetbrains.plugins.groovy.lang.psi.dataFlow.DFAEngine) GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) ReadWriteVariableInstruction(org.jetbrains.plugins.groovy.lang.psi.controlFlow.ReadWriteVariableInstruction) Instruction(org.jetbrains.plugins.groovy.lang.psi.controlFlow.Instruction) ReadWriteVariableInstruction(org.jetbrains.plugins.groovy.lang.psi.controlFlow.ReadWriteVariableInstruction) Nullable(org.jetbrains.annotations.Nullable)

Example 67 with GrVariable

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

the class WritesCounterDFAInstance method fun.

@Override
public void fun(@NotNull TObjectIntHashMap<GrVariable> map, @NotNull Instruction instruction) {
    if (!(instruction instanceof ReadWriteVariableInstruction))
        return;
    final ReadWriteVariableInstruction rwInstruction = (ReadWriteVariableInstruction) instruction;
    if (!rwInstruction.isWrite())
        return;
    final GrVariable variable = getVariable(instruction.getElement());
    if (variable == null)
        return;
    int currentVal = map.get(variable);
    if (currentVal == 2)
        return;
    if (currentVal == 0 || currentVal == 1 && !(variable.getParent() instanceof GrForInClause))
        currentVal++;
    map.put(variable, currentVal);
}
Also used : GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) ReadWriteVariableInstruction(org.jetbrains.plugins.groovy.lang.psi.controlFlow.ReadWriteVariableInstruction) GrForInClause(org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrForInClause)

Example 68 with GrVariable

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

the class GroovyTypeCheckVisitor method visitForInClause.

@Override
public void visitForInClause(@NotNull GrForInClause forInClause) {
    super.visitForInClause(forInClause);
    final GrVariable variable = forInClause.getDeclaredVariable();
    final GrExpression iterated = forInClause.getIteratedExpression();
    if (variable == null || iterated == null)
        return;
    final PsiType iteratedType = ClosureParameterEnhancer.findTypeForIteration(iterated, forInClause);
    if (iteratedType == null)
        return;
    final PsiType targetType = variable.getType();
    processAssignment(targetType, iteratedType, forInClause, variable.getNameIdentifierGroovy());
}
Also used : GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable)

Example 69 with GrVariable

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

the class GrConcatenationInjector method findLanguageParams.

private static BaseInjection findLanguageParams(PsiElement place) {
    PsiElement parent = place.getParent();
    if (parent instanceof GrAssignmentExpression && ((GrAssignmentExpression) parent).getRValue() == place) {
        final GrExpression lvalue = ((GrAssignmentExpression) parent).getLValue();
        if (lvalue instanceof GrReferenceExpression) {
            final PsiElement resolved = ((GrReferenceExpression) lvalue).resolve();
            if (resolved instanceof PsiModifierListOwner) {
                return getLanguageParams((PsiModifierListOwner) resolved);
            }
        }
    } else if (parent instanceof GrVariable) {
        return getLanguageParams((PsiModifierListOwner) parent);
    } else if (parent instanceof GrArgumentList) {
        final PsiElement pparent = parent.getParent();
        if (pparent instanceof GrCall) {
            final GrCall call = (GrCall) pparent;
            final GroovyResolveResult result = call.advancedResolve();
            if (result.getElement() != null) {
                final Map<GrExpression, Pair<PsiParameter, PsiType>> map = GrClosureSignatureUtil.mapArgumentsToParameters(result, place, false, false, call.getNamedArguments(), call.getExpressionArguments(), call.getClosureArguments());
                if (map != null) {
                    final Pair<PsiParameter, PsiType> pair = map.get(place);
                    return getLanguageParams(pair.first);
                }
            }
        }
    }
    return null;
}
Also used : GrCall(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrCall) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) GrAssignmentExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrAssignmentExpression) GrArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList) Pair(com.intellij.openapi.util.Pair)

Example 70 with GrVariable

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

the class GrCastFix method findExpressionToCast.

private static GrExpression findExpressionToCast(ProblemDescriptor descriptor) {
    final PsiElement element = descriptor.getPsiElement();
    final PsiElement parent = element.getParent();
    if (parent instanceof GrVariable) {
        return ((GrVariable) parent).getInitializerGroovy();
    } else if (parent instanceof GrAssignmentExpression) {
        return ((GrAssignmentExpression) parent).getRValue();
    } else if (parent instanceof GrThrowStatement) {
        return ((GrThrowStatement) parent).getException();
    } else if (parent instanceof GrReturnStatement) {
        return ((GrReturnStatement) parent).getReturnValue();
    } else if (element instanceof GrExpression) {
        return (GrExpression) element;
    }
    PsiFile file = element.getContainingFile();
    VirtualFile virtualFile = file.getVirtualFile();
    String url = virtualFile == null ? "" : virtualFile.getPresentableUrl();
    LOG.error("can't find expression to cast at position " + element.getTextRange(), new Attachment(url, file.getText()));
    return null;
}
Also used : GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) VirtualFile(com.intellij.openapi.vfs.VirtualFile) GrAssignmentExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrAssignmentExpression) GrThrowStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrThrowStatement) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) PsiFile(com.intellij.psi.PsiFile) Attachment(com.intellij.openapi.diagnostic.Attachment) GrReturnStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrReturnStatement) PsiElement(com.intellij.psi.PsiElement)

Aggregations

GrVariable (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable)88 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)34 PsiElement (com.intellij.psi.PsiElement)24 Nullable (org.jetbrains.annotations.Nullable)20 GrVariableDeclaration (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration)19 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)16 NotNull (org.jetbrains.annotations.NotNull)14 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)14 GrField (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField)12 StringPartInfo (org.jetbrains.plugins.groovy.refactoring.introduce.StringPartInfo)11 GrStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)10 GrParameter (org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter)10 GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)8 GrMethod (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod)8 PsiType (com.intellij.psi.PsiType)7 GroovyResolveResult (org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult)7 GrAssignmentExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrAssignmentExpression)7 TextRange (com.intellij.openapi.util.TextRange)6 GroovyRecursiveElementVisitor (org.jetbrains.plugins.groovy.lang.psi.GroovyRecursiveElementVisitor)6 ASTNode (com.intellij.lang.ASTNode)5