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;
}
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);
}
}
};
}
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);
}
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);
}
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;
}
Aggregations