Search in sources :

Example 6 with GrControlFlowOwner

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

the class GroovyCompletionData method hasReturnValue.

private static boolean hasReturnValue(PsiElement context) {
    GrControlFlowOwner flowOwner = ControlFlowUtils.findControlFlowOwner(context);
    if (flowOwner instanceof GrClosableBlock)
        return true;
    if (flowOwner instanceof GroovyFile)
        return true;
    if (flowOwner == null)
        return true;
    PsiElement parent = flowOwner.getParent();
    if (parent instanceof GrMethod) {
        return !PsiType.VOID.equals(((GrMethod) parent).getReturnType());
    } else if (parent instanceof GrClassInitializer) {
        return false;
    }
    return true;
}
Also used : GrControlFlowOwner(org.jetbrains.plugins.groovy.lang.psi.GrControlFlowOwner) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) GroovyFile(org.jetbrains.plugins.groovy.lang.psi.GroovyFile)

Example 7 with GrControlFlowOwner

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

the class GrReassignedInClosureLocalVarInspection method buildVisitor.

@NotNull
@Override
protected BaseInspectionVisitor buildVisitor() {
    return new BaseInspectionVisitor() {

        @Override
        public void visitReferenceExpression(@NotNull GrReferenceExpression referenceExpression) {
            super.visitReferenceExpression(referenceExpression);
            if (!PsiUtil.isLValue(referenceExpression))
                return;
            final PsiElement resolved = referenceExpression.resolve();
            if (!PsiUtil.isLocalVariable(resolved))
                return;
            final PsiType checked = GrReassignedLocalVarsChecker.getReassignedVarType(referenceExpression, false);
            if (checked == null)
                return;
            final GrControlFlowOwner varFlowOwner = ControlFlowUtils.findControlFlowOwner(resolved);
            final GrControlFlowOwner refFlorOwner = ControlFlowUtils.findControlFlowOwner(referenceExpression);
            if (isOtherScopeAndType(referenceExpression, checked, varFlowOwner, refFlorOwner)) {
                String flowDescription = getFlowDescription(refFlorOwner);
                final String message = GroovyInspectionBundle.message("local.var.0.is.reassigned", ((GrNamedElement) resolved).getName(), flowDescription);
                registerError(referenceExpression, message, LocalQuickFix.EMPTY_ARRAY, ProblemHighlightType.GENERIC_ERROR_OR_WARNING);
            }
        }
    };
}
Also used : GrControlFlowOwner(org.jetbrains.plugins.groovy.lang.psi.GrControlFlowOwner) BaseInspectionVisitor(org.jetbrains.plugins.groovy.codeInspection.BaseInspectionVisitor) NotNull(org.jetbrains.annotations.NotNull) PsiElement(com.intellij.psi.PsiElement) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) PsiType(com.intellij.psi.PsiType) NotNull(org.jetbrains.annotations.NotNull)

Example 8 with GrControlFlowOwner

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

the class ControlFlowUtils method findAccess.

/**
   * searches for next or previous write access to local variable
   * @param local variable to analyze
   * @param place place to start searching
   * @param ahead if true search for next write. if false searches for previous write
   * @return all write instructions leading to (or preceding) the place
   */
public static List<ReadWriteVariableInstruction> findAccess(GrVariable local, final PsiElement place, boolean ahead, boolean writeAccessOnly) {
    LOG.assertTrue(!(local instanceof GrField), local.getClass());
    final GrControlFlowOwner owner = findControlFlowOwner(place);
    assert owner != null;
    final Instruction cur = findInstruction(place, owner.getControlFlow());
    if (cur == null) {
        throw new IllegalArgumentException("place is not in the flow");
    }
    return findAccess(local, ahead, writeAccessOnly, cur);
}
Also used : GrControlFlowOwner(org.jetbrains.plugins.groovy.lang.psi.GrControlFlowOwner) 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)

Example 9 with GrControlFlowOwner

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

the class TypeInferenceHelper method getInferredType.

@Nullable
public static PsiType getInferredType(@NotNull final GrReferenceExpression refExpr) {
    final GrControlFlowOwner scope = ControlFlowUtils.findControlFlowOwner(refExpr);
    if (scope == null)
        return null;
    final String referenceName = refExpr.getReferenceName();
    if (referenceName == null)
        return null;
    final ReadWriteVariableInstruction rwInstruction = ControlFlowUtils.findRWInstruction(refExpr, scope.getControlFlow());
    if (rwInstruction == null)
        return null;
    return getInferenceCache(scope).getInferredType(referenceName, rwInstruction);
}
Also used : GrControlFlowOwner(org.jetbrains.plugins.groovy.lang.psi.GrControlFlowOwner) ReadWriteVariableInstruction(org.jetbrains.plugins.groovy.lang.psi.controlFlow.ReadWriteVariableInstruction) Nullable(org.jetbrains.annotations.Nullable)

Example 10 with GrControlFlowOwner

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

the class GroovyLanguageInjectionSupport method doInject.

private static boolean doInject(@NotNull String languageId, @NotNull PsiElement psiElement, @NotNull PsiLanguageInjectionHost host) {
    final PsiElement target = getTopLevelInjectionTarget(psiElement);
    final PsiElement parent = target.getParent();
    final Project project = psiElement.getProject();
    if (parent instanceof GrReturnStatement) {
        final GrControlFlowOwner owner = ControlFlowUtils.findControlFlowOwner(parent);
        if (owner instanceof GrOpenBlock && owner.getParent() instanceof GrMethod) {
            return JavaLanguageInjectionSupport.doInjectInJavaMethod(project, (PsiMethod) owner.getParent(), -1, host, languageId);
        }
    } else if (parent instanceof GrMethod) {
        return JavaLanguageInjectionSupport.doInjectInJavaMethod(project, (GrMethod) parent, -1, host, languageId);
    } else if (parent instanceof GrAnnotationNameValuePair) {
        final PsiReference ref = parent.getReference();
        if (ref != null) {
            final PsiElement resolved = ref.resolve();
            if (resolved instanceof PsiMethod) {
                return JavaLanguageInjectionSupport.doInjectInJavaMethod(project, (PsiMethod) resolved, -1, host, languageId);
            }
        }
    } else if (parent instanceof GrArgumentList && parent.getParent() instanceof GrMethodCall) {
        final PsiMethod method = ((GrMethodCall) parent.getParent()).resolveMethod();
        if (method != null) {
            final int index = GrInjectionUtil.findParameterIndex(target, ((GrMethodCall) parent.getParent()));
            if (index >= 0) {
                return JavaLanguageInjectionSupport.doInjectInJavaMethod(project, method, index, host, languageId);
            }
        }
    } else if (parent instanceof GrAssignmentExpression) {
        final GrExpression expr = ((GrAssignmentExpression) parent).getLValue();
        if (expr instanceof GrReferenceExpression) {
            final PsiElement element = ((GrReferenceExpression) expr).resolve();
            if (element != null) {
                return doInject(languageId, element, host);
            }
        }
    } else {
        if (parent instanceof PsiVariable) {
            Processor<PsiLanguageInjectionHost> fixer = getAnnotationFixer(project, languageId);
            if (JavaLanguageInjectionSupport.doAddLanguageAnnotation(project, (PsiModifierListOwner) parent, host, languageId, fixer))
                return true;
        } else if (target instanceof PsiVariable && !(target instanceof LightElement)) {
            Processor<PsiLanguageInjectionHost> fixer = getAnnotationFixer(project, languageId);
            if (JavaLanguageInjectionSupport.doAddLanguageAnnotation(project, (PsiModifierListOwner) target, host, languageId, fixer))
                return true;
        }
    }
    return false;
}
Also used : Processor(com.intellij.util.Processor) GrAnnotationNameValuePair(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotationNameValuePair) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) GrReturnStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrReturnStatement) LightElement(com.intellij.psi.impl.light.LightElement) Project(com.intellij.openapi.project.Project) GrControlFlowOwner(org.jetbrains.plugins.groovy.lang.psi.GrControlFlowOwner) GrArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList) GrOpenBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)

Aggregations

GrControlFlowOwner (org.jetbrains.plugins.groovy.lang.psi.GrControlFlowOwner)15 PsiElement (com.intellij.psi.PsiElement)7 Instruction (org.jetbrains.plugins.groovy.lang.psi.controlFlow.Instruction)7 NotNull (org.jetbrains.annotations.NotNull)3 Nullable (org.jetbrains.annotations.Nullable)3 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)3 GrStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)3 GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)3 GrReturnStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrReturnStatement)3 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)3 GrMethod (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod)3 ReadWriteVariableInstruction (org.jetbrains.plugins.groovy.lang.psi.controlFlow.ReadWriteVariableInstruction)3 IfEndInstruction (org.jetbrains.plugins.groovy.lang.psi.controlFlow.impl.IfEndInstruction)3 Editor (com.intellij.openapi.editor.Editor)2 Project (com.intellij.openapi.project.Project)2 ArrayList (java.util.ArrayList)2 ControlFlowUtils (org.jetbrains.plugins.groovy.codeInspection.utils.ControlFlowUtils)2 GroovyFile (org.jetbrains.plugins.groovy.lang.psi.GroovyFile)2 GrOpenBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock)2 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)2