Search in sources :

Example 1 with FunctionalSwitchExpressionScanner

use of org.checkerframework.javacutil.SwitchExpressionScanner.FunctionalSwitchExpressionScanner in project checker-framework by typetools.

the class BaseTypeVisitor method visitSwitchExpression17.

/**
 * This method validates the type of the switch expression. It issues an error if the type of a
 * value that the switch expression can result is not a subtype of the switch type.
 *
 * <p>If a subclass overrides this method, it must call {@code super.scan(switchExpressionTree,
 * null)} so that the blocks and statements in the cases are checked.
 *
 * @param switchExpressionTree a {@code SwitchExpressionTree}
 */
public void visitSwitchExpression17(Tree switchExpressionTree) {
    boolean valid = validateTypeOf(switchExpressionTree);
    if (valid) {
        AnnotatedTypeMirror switchType = atypeFactory.getAnnotatedType(switchExpressionTree);
        SwitchExpressionScanner<Void, Void> scanner = new FunctionalSwitchExpressionScanner<>((ExpressionTree valueTree, Void unused) -> {
            BaseTypeVisitor.this.commonAssignmentCheck(switchType, valueTree, "switch.expression");
            return null;
        }, (r1, r2) -> null);
        scanner.scanSwitchExpression(switchExpressionTree, null);
    }
    super.scan(switchExpressionTree, null);
}
Also used : FunctionalSwitchExpressionScanner(org.checkerframework.javacutil.SwitchExpressionScanner.FunctionalSwitchExpressionScanner) LambdaExpressionTree(com.sun.source.tree.LambdaExpressionTree) ConditionalExpressionTree(com.sun.source.tree.ConditionalExpressionTree) ExpressionTree(com.sun.source.tree.ExpressionTree) AnnotatedTypeMirror(org.checkerframework.framework.type.AnnotatedTypeMirror)

Example 2 with FunctionalSwitchExpressionScanner

use of org.checkerframework.javacutil.SwitchExpressionScanner.FunctionalSwitchExpressionScanner in project checker-framework by typetools.

the class TypeFromExpressionVisitor method visitSwitchExpressionTree17.

/**
 * Compute the type of the switch expression tree.
 *
 * @param switchExpressionTree a SwitchExpressionTree; typed as Tree so method signature is
 *     backward-compatible
 * @param f an AnnotatedTypeFactory
 * @return the type of the switch expression
 */
public AnnotatedTypeMirror visitSwitchExpressionTree17(Tree switchExpressionTree, AnnotatedTypeFactory f) {
    TypeMirror switchTypeMirror = TreeUtils.typeOf(switchExpressionTree);
    SwitchExpressionScanner<AnnotatedTypeMirror, Void> luber = new FunctionalSwitchExpressionScanner<>(// Function applied to each result expression of the switch expression.
    (valueTree, unused) -> f.getAnnotatedType(valueTree), // Function used to combine the types of each result expression.
    (type1, type2) -> {
        if (type1 == null) {
            return type2;
        } else if (type2 == null) {
            return type1;
        } else {
            return AnnotatedTypes.leastUpperBound(f, type1, type2, switchTypeMirror);
        }
    });
    return luber.scanSwitchExpression(switchExpressionTree, null);
}
Also used : FunctionalSwitchExpressionScanner(org.checkerframework.javacutil.SwitchExpressionScanner.FunctionalSwitchExpressionScanner) TypeMirror(javax.lang.model.type.TypeMirror)

Aggregations

FunctionalSwitchExpressionScanner (org.checkerframework.javacutil.SwitchExpressionScanner.FunctionalSwitchExpressionScanner)2 ConditionalExpressionTree (com.sun.source.tree.ConditionalExpressionTree)1 ExpressionTree (com.sun.source.tree.ExpressionTree)1 LambdaExpressionTree (com.sun.source.tree.LambdaExpressionTree)1 TypeMirror (javax.lang.model.type.TypeMirror)1 AnnotatedTypeMirror (org.checkerframework.framework.type.AnnotatedTypeMirror)1