Search in sources :

Example 36 with GrParameter

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

the class GroovyTypeCheckVisitor method visitVariable.

@Override
public void visitVariable(@NotNull GrVariable variable) {
    super.visitVariable(variable);
    final PsiType varType = variable.getType();
    final PsiElement parent = variable.getParent();
    if (variable instanceof GrParameter && ((GrParameter) variable).getDeclarationScope() instanceof GrMethod || parent instanceof GrForInClause) {
        return;
    } else if (parent instanceof GrVariableDeclaration && ((GrVariableDeclaration) parent).isTuple()) {
        //check tuple assignment:  def (int x, int y) = foo()
        final GrVariableDeclaration tuple = (GrVariableDeclaration) parent;
        final GrExpression initializer = tuple.getTupleInitializer();
        if (initializer == null)
            return;
        if (!(initializer instanceof GrListOrMap)) {
            PsiType type = initializer.getType();
            if (type == null)
                return;
            PsiType valueType = extractIterableTypeParameter(type, false);
            processAssignment(varType, valueType, tuple, variable.getNameIdentifierGroovy());
            return;
        }
    }
    GrExpression initializer = variable.getInitializerGroovy();
    if (initializer == null)
        return;
    processAssignment(varType, initializer, variable.getNameIdentifierGroovy(), "cannot.assign", variable, ApplicableTo.ASSIGNMENT);
}
Also used : GrVariableDeclaration(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration) GrForInClause(org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrForInClause) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) GrListOrMap(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap) GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)

Example 37 with GrParameter

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

the class GroovyFileImpl method getSyntheticArgsParameter.

private GrParameter getSyntheticArgsParameter() {
    GrParameter parameter = mySyntheticArgsParameter;
    if (parameter == null) {
        final PsiType psiType = JavaPsiFacade.getElementFactory(getProject()).createTypeFromText("java.lang.String[]", this);
        parameter = new GrLightParameter(SYNTHETIC_PARAMETER_NAME, psiType, this);
        mySyntheticArgsParameter = parameter;
    }
    return parameter;
}
Also used : GrLightParameter(org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.GrLightParameter) GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter)

Example 38 with GrParameter

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

the class ControlFlowBuilder method visitOpenBlock.

@Override
public void visitOpenBlock(@NotNull GrOpenBlock block) {
    final PsiElement parent = block.getParent();
    final PsiElement lbrace = block.getLBrace();
    if (lbrace != null && parent instanceof GrMethod) {
        for (GrParameter parameter : ((GrMethod) parent).getParameters()) {
            if (myPolicy.isVariableInitialized(parameter)) {
                addNode(new ReadWriteVariableInstruction(parameter.getName(), parameter, ReadWriteVariableInstruction.WRITE));
            }
        }
    }
    super.visitOpenBlock(block);
    if (!(block.getParent() instanceof GrBlockStatement && block.getParent().getParent() instanceof GrLoopStatement)) {
        final GrStatement[] statements = block.getStatements();
        if (statements.length > 0) {
            handlePossibleReturn(statements[statements.length - 1]);
        }
    }
}
Also used : GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)

Example 39 with GrParameter

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

the class ControlFlowBuilder method visitTryStatement.

@Override
public void visitTryStatement(@NotNull GrTryCatchStatement tryCatchStatement) {
    final GrOpenBlock tryBlock = tryCatchStatement.getTryBlock();
    final GrCatchClause[] catchClauses = tryCatchStatement.getCatchClauses();
    final GrFinallyClause finallyClause = tryCatchStatement.getFinallyClause();
    for (int i = catchClauses.length - 1; i >= 0; i--) {
        myCaughtExceptionInfos.push(new ExceptionInfo(catchClauses[i]));
    }
    if (finallyClause != null)
        myFinallyCount++;
    List<Pair<InstructionImpl, GroovyPsiElement>> oldPending = null;
    if (finallyClause != null) {
        oldPending = myPending;
        myPending = new ArrayList<>();
    }
    InstructionImpl tryBegin = startNode(tryBlock);
    tryBlock.accept(this);
    InstructionImpl tryEnd = myHead;
    finishNode(tryBegin);
    Set<Pair<InstructionImpl, GroovyPsiElement>> pendingAfterTry = new LinkedHashSet<>(myPending);
    @SuppressWarnings("unchecked") List<InstructionImpl>[] throwers = new List[catchClauses.length];
    for (int i = 0; i < catchClauses.length; i++) {
        throwers[i] = myCaughtExceptionInfos.pop().myThrowers;
    }
    InstructionImpl[] catches = new InstructionImpl[catchClauses.length];
    for (int i = 0; i < catchClauses.length; i++) {
        interruptFlow();
        final InstructionImpl catchBeg = startNode(catchClauses[i]);
        for (InstructionImpl thrower : throwers[i]) {
            addEdge(thrower, catchBeg);
        }
        final GrParameter parameter = catchClauses[i].getParameter();
        if (parameter != null && myPolicy.isVariableInitialized(parameter)) {
            addNode(new ReadWriteVariableInstruction(parameter.getName(), parameter, ReadWriteVariableInstruction.WRITE));
        }
        catchClauses[i].accept(this);
        catches[i] = myHead;
        finishNode(catchBeg);
    }
    pendingAfterTry.addAll(myPending);
    myPending = new ArrayList<>(pendingAfterTry);
    if (finallyClause != null) {
        myFinallyCount--;
        interruptFlow();
        final InstructionImpl finallyInstruction = startNode(finallyClause, false);
        Set<AfterCallInstruction> postCalls = new LinkedHashSet<>();
        final List<Pair<InstructionImpl, GroovyPsiElement>> copy = myPending;
        myPending = new ArrayList<>();
        for (Pair<InstructionImpl, GroovyPsiElement> pair : copy) {
            postCalls.add(addCallNode(finallyInstruction, pair.getSecond(), pair.getFirst()));
        }
        if (tryEnd != null) {
            postCalls.add(addCallNode(finallyInstruction, tryCatchStatement, tryEnd));
        }
        for (InstructionImpl catchEnd : catches) {
            if (catchEnd != null) {
                postCalls.add(addCallNode(finallyInstruction, tryCatchStatement, catchEnd));
            }
        }
        //save added postcalls into separate list because we don't want returnInstruction grabbed their pending edges
        List<Pair<InstructionImpl, GroovyPsiElement>> pendingPostCalls = myPending;
        myPending = new ArrayList<>();
        myHead = finallyInstruction;
        finallyClause.accept(this);
        final ReturnInstruction returnInstruction = new ReturnInstruction(finallyClause);
        for (AfterCallInstruction postCall : postCalls) {
            postCall.setReturnInstruction(returnInstruction);
            addEdge(returnInstruction, postCall);
        }
        addNodeAndCheckPending(returnInstruction);
        interruptFlow();
        finishNode(finallyInstruction);
        if (oldPending == null) {
            error();
        }
        oldPending.addAll(pendingPostCalls);
        myPending = oldPending;
    } else {
        if (tryEnd != null) {
            addPendingEdge(tryCatchStatement, tryEnd);
        }
        for (InstructionImpl catchEnd : catches) {
            addPendingEdge(tryBlock, catchEnd);
        }
    }
}
Also used : GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter) GrArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList) FList(com.intellij.util.containers.FList) GrOpenBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock) Pair(com.intellij.openapi.util.Pair)

Example 40 with GrParameter

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

the class GrModifierListImpl method addAnnotation.

@Override
@NotNull
public GrAnnotation addAnnotation(@NotNull @NonNls String qualifiedName) {
    final PsiClass psiClass = JavaPsiFacade.getInstance(getProject()).findClass(qualifiedName, getResolveScope());
    final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(getProject());
    GrAnnotation annotation;
    if (psiClass != null && psiClass.isAnnotationType()) {
        annotation = (GrAnnotation) addAfter(factory.createModifierFromText("@xxx"), null);
        annotation.getClassReference().bindToElement(psiClass);
    } else {
        annotation = (GrAnnotation) addAfter(factory.createModifierFromText("@" + qualifiedName), null);
    }
    final PsiElement parent = getParent();
    if (!(parent instanceof GrParameter)) {
        final ASTNode node = annotation.getNode();
        final ASTNode treeNext = node.getTreeNext();
        if (treeNext != null) {
            getNode().addLeaf(TokenType.WHITE_SPACE, "\n", treeNext);
        } else {
            parent.getNode().addLeaf(TokenType.WHITE_SPACE, "\n", getNode().getTreeNext());
        }
    }
    return annotation;
}
Also used : GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) GrAnnotation(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotation) ASTNode(com.intellij.lang.ASTNode) GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

GrParameter (org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter)99 GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)22 NotNull (org.jetbrains.annotations.NotNull)20 PsiElement (com.intellij.psi.PsiElement)19 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)19 GrMethod (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod)18 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)16 GrParameterList (org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameterList)14 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)13 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)12 ArrayList (java.util.ArrayList)11 GrVariable (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable)10 TextRange (com.intellij.openapi.util.TextRange)9 GrOpenBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock)9 Nullable (org.jetbrains.annotations.Nullable)8 GroovyResolveResult (org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult)7 Project (com.intellij.openapi.project.Project)6 IncorrectOperationException (com.intellij.util.IncorrectOperationException)6 GroovyFile (org.jetbrains.plugins.groovy.lang.psi.GroovyFile)6 GrField (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField)6