예제 #1
0
 @Override
 @Nullable
 public Choice<Unifier> visitConditionalExpression(
     ConditionalExpressionTree conditional, Unifier unifier) {
   return getCondition()
       .unify(conditional.getCondition(), unifier.fork())
       .thenChoose(unifications(getTrueExpression(), conditional.getTrueExpression()))
       .thenChoose(unifications(getFalseExpression(), conditional.getFalseExpression()))
       .or(
           getCondition()
               .negate()
               .unify(conditional.getCondition(), unifier.fork())
               .thenChoose(unifications(getFalseExpression(), conditional.getTrueExpression()))
               .thenChoose(unifications(getTrueExpression(), conditional.getFalseExpression())));
 }
  private static List<? extends TypeMirror> computeConditionalExpression(
      Set<ElementKind> types, CompilationInfo info, TreePath parent, Tree error, int offset) {
    ConditionalExpressionTree cet = (ConditionalExpressionTree) parent.getLeaf();

    if (cet.getCondition() == error) {
      types.add(ElementKind.PARAMETER);
      types.add(ElementKind.LOCAL_VARIABLE);
      types.add(ElementKind.FIELD);

      return Collections.singletonList(info.getTypes().getPrimitiveType(TypeKind.BOOLEAN));
    }

    if (cet.getTrueExpression() == error || cet.getFalseExpression() == error) {
      types.add(ElementKind.PARAMETER);
      types.add(ElementKind.LOCAL_VARIABLE);
      types.add(ElementKind.FIELD);

      return resolveType(types, info, parent.getParentPath(), cet, offset, null, null);
    }

    return null;
  }