Beispiel #1
0
  private static boolean isOtherTypeOrDifferent(
      @NotNull GrReferenceExpression referenceExpression, GrVariable resolved) {
    if (ControlFlowUtils.findControlFlowOwner(referenceExpression)
        != ControlFlowUtils.findControlFlowOwner(resolved)) return true;

    final PsiType currentType = referenceExpression.getType();
    return currentType != null
        && currentType != PsiType.NULL
        && !ControlFlowUtils.findAccess(resolved, referenceExpression, false, true).isEmpty();
  }
  private static boolean isTailAfterIf(
      @NotNull GrIfStatement ifStatement, @NotNull GrStatementOwner owner) {
    final GrControlFlowOwner flowOwner = ControlFlowUtils.findControlFlowOwner(ifStatement);
    if (flowOwner == null) return false;

    final Instruction[] flow = flowOwner.getControlFlow();

    final GrStatement[] statements = owner.getStatements();
    final int index = ArrayUtilRt.find(statements, ifStatement);
    if (index == statements.length - 1) return false;

    final GrStatement then = ifStatement.getThenBranch();

    for (Instruction i : flow) {
      final PsiElement element = i.getElement();
      if (element == null || !PsiTreeUtil.isAncestor(then, element, true)) continue;

      for (Instruction succ : i.allSuccessors()) {
        if (succ instanceof IfEndInstruction) {
          return false;
        }
      }
    }

    return true;
  }
  @Nullable
  public static PsiType inferReturnType(PsiElement position) {
    final GrControlFlowOwner flowOwner = ControlFlowUtils.findControlFlowOwner(position);
    if (flowOwner == null) return null;

    final PsiElement parent = flowOwner.getContext();
    if (flowOwner instanceof GrOpenBlock && parent instanceof GrMethod) {
      final GrMethod method = (GrMethod) parent;
      if (method.isConstructor()) return null;
      return method.getReturnType();
    }

    return null;
  }