Search in sources :

Example 91 with BugInCF

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

the class AFReducingVisitor method visitDeclared_Declared.

// From the JLS Spec:
// If F has the form G<..., Yk-1,U, Yk+1, ...>, where U involves Tj
@Override
public Void visitDeclared_Declared(AnnotatedDeclaredType subtype, AnnotatedDeclaredType supertype, Set<AFConstraint> constraints) {
    if (subtype.isUnderlyingTypeRaw() || supertype.isUnderlyingTypeRaw()) {
        // inference will be aborted, but type-checking will continue.
        throw new BugInCF("Can't infer type arguments when raw types are involved.");
    }
    if (!TypesUtils.isErasedSubtype(subtype.getUnderlyingType(), supertype.getUnderlyingType(), typeFactory.getChecker().getTypeUtils())) {
        return null;
    }
    AnnotatedDeclaredType subAsSuper = AnnotatedTypes.castedAsSuper(typeFactory, subtype, supertype);
    final List<AnnotatedTypeMirror> subTypeArgs = subAsSuper.getTypeArguments();
    final List<AnnotatedTypeMirror> superTypeArgs = supertype.getTypeArguments();
    for (int i = 0; i < subTypeArgs.size(); i++) {
        final AnnotatedTypeMirror subTypeArg = subTypeArgs.get(i);
        final AnnotatedTypeMirror superTypeArg = superTypeArgs.get(i);
        // Since we always have both bounds in the checker framework we always compare both
        if (superTypeArg.getKind() == TypeKind.WILDCARD) {
            final AnnotatedWildcardType superWc = (AnnotatedWildcardType) superTypeArg;
            if (subTypeArg.getKind() == TypeKind.WILDCARD) {
                final AnnotatedWildcardType subWc = (AnnotatedWildcardType) subTypeArg;
                TypeArgInferenceUtil.checkForUninferredTypes(subWc);
                addConstraint(subWc.getExtendsBound(), superWc.getExtendsBound(), constraints);
                addInverseConstraint(superWc.getSuperBound(), subWc.getSuperBound(), constraints);
            } else {
                addConstraint(subTypeArg, superWc.getExtendsBound(), constraints);
                addInverseConstraint(superWc.getSuperBound(), subTypeArg, constraints);
            }
        } else {
            // if F has the form G<..., Yk-1, U, Yk+1, ...>, where U is a type expression that involves
            // Tj
            addEqualityConstraint(subTypeArg, superTypeArg, constraints);
        }
    }
    return null;
}
Also used : AnnotatedDeclaredType(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedDeclaredType) AnnotatedWildcardType(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedWildcardType) BugInCF(org.checkerframework.javacutil.BugInCF) AnnotatedTypeMirror(org.checkerframework.framework.type.AnnotatedTypeMirror)

Example 92 with BugInCF

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

the class MustCallInferenceLogic method getNormalSuccessors.

/**
 * Returns the non-exceptional successors of the current block.
 *
 * @param cur the current block
 * @return the successors of this current block
 */
private List<Block> getNormalSuccessors(Block cur) {
    List<Block> successorBlock = new ArrayList<>();
    if (cur.getType() == Block.BlockType.CONDITIONAL_BLOCK) {
        ConditionalBlock ccur = (ConditionalBlock) cur;
        successorBlock.add(ccur.getThenSuccessor());
        successorBlock.add(ccur.getElseSuccessor());
    } else {
        if (!(cur instanceof SingleSuccessorBlock)) {
            throw new BugInCF("BlockImpl is neither a conditional block nor a SingleSuccessorBlock");
        }
        Block b = ((SingleSuccessorBlock) cur).getSuccessor();
        if (b != null) {
            successorBlock.add(b);
        }
    }
    return successorBlock;
}
Also used : ConditionalBlock(org.checkerframework.dataflow.cfg.block.ConditionalBlock) SingleSuccessorBlock(org.checkerframework.dataflow.cfg.block.SingleSuccessorBlock) ArrayList(java.util.ArrayList) SingleSuccessorBlock(org.checkerframework.dataflow.cfg.block.SingleSuccessorBlock) Block(org.checkerframework.dataflow.cfg.block.Block) ConditionalBlock(org.checkerframework.dataflow.cfg.block.ConditionalBlock) BugInCF(org.checkerframework.javacutil.BugInCF)

Example 93 with BugInCF

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

the class FactoryTestChecker method initChecker.

@Override
public void initChecker() {
    super.initChecker();
    // Find factory constructor
    String checkerClassName = getOption("checker");
    try {
        if (checkerClassName != null) {
            Class<?> checkerClass = Class.forName(checkerClassName);
            Constructor<?> constructor = checkerClass.getConstructor();
            Object o = constructor.newInstance();
            if (o instanceof SourceChecker) {
                checker = (SourceChecker) o;
            }
        }
    } catch (Exception e) {
        throw new BugInCF("Couldn't load " + checkerClassName + " class.");
    }
}
Also used : JavaFileObject(javax.tools.JavaFileObject) SourceChecker(org.checkerframework.framework.source.SourceChecker) BugInCF(org.checkerframework.javacutil.BugInCF) IOException(java.io.IOException)

Example 94 with BugInCF

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

the class AnnotatedTypes method findEffectiveLowerBoundAnnotations.

/**
 * This method returns the effective annotation on the lower bound of a type, or on the type
 * itself if the type has no lower bound (it is not a type variable, wildcard, or intersection).
 *
 * @return the set of effective annotation mirrors in all hierarchies
 */
public static Set<AnnotationMirror> findEffectiveLowerBoundAnnotations(final QualifierHierarchy qualifierHierarchy, final AnnotatedTypeMirror toSearch) {
    AnnotatedTypeMirror source = toSearch;
    TypeKind kind = source.getKind();
    while (kind == TypeKind.TYPEVAR || kind == TypeKind.WILDCARD || kind == TypeKind.INTERSECTION) {
        switch(source.getKind()) {
            case TYPEVAR:
                source = ((AnnotatedTypeVariable) source).getLowerBound();
                break;
            case WILDCARD:
                source = ((AnnotatedWildcardType) source).getSuperBound();
                break;
            case INTERSECTION:
                // if there are multiple conflicting annotations, choose the lowest
                final Set<AnnotationMirror> glb = glbOfBounds((AnnotatedIntersectionType) source, qualifierHierarchy);
                return glb;
            default:
                throw new BugInCF("Unexpected AnnotatedTypeMirror with no primary annotation;" + " toSearch=" + toSearch + " source=" + source);
        }
        kind = source.getKind();
    }
    return source.getAnnotations();
}
Also used : AnnotationMirror(javax.lang.model.element.AnnotationMirror) TypeKind(javax.lang.model.type.TypeKind) BugInCF(org.checkerframework.javacutil.BugInCF) AnnotatedTypeMirror(org.checkerframework.framework.type.AnnotatedTypeMirror)

Example 95 with BugInCF

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

the class AnnotatedTypes method glbSubtype.

/**
 * Returns the annotated greatest lower bound of {@code subtype} and {@code supertype}, where the
 * underlying Java types are in a subtyping relationship.
 *
 * <p>This handles cases 1, 2, and 3 mentioned in the Javadoc of {@link
 * #annotatedGLB(AnnotatedTypeFactory, AnnotatedTypeMirror, AnnotatedTypeMirror)}.
 *
 * @param qualifierHierarchy QualifierHierarchy
 * @param subtype annotated type whose underlying type is a subtype of {@code supertype}
 * @param supertype annotated type whose underlying type is a supertype of {@code subtype}
 * @return the annotated greatest lower bound of {@code subtype} and {@code supertype}
 */
private static AnnotatedTypeMirror glbSubtype(QualifierHierarchy qualifierHierarchy, AnnotatedTypeMirror subtype, AnnotatedTypeMirror supertype) {
    AnnotatedTypeMirror glb = subtype.deepCopy();
    glb.clearPrimaryAnnotations();
    for (AnnotationMirror top : qualifierHierarchy.getTopAnnotations()) {
        AnnotationMirror subAnno = subtype.getAnnotationInHierarchy(top);
        AnnotationMirror superAnno = supertype.getAnnotationInHierarchy(top);
        if (subAnno != null && superAnno != null) {
            glb.addAnnotation(qualifierHierarchy.greatestLowerBound(subAnno, superAnno));
        } else if (subAnno == null && superAnno == null) {
            if (subtype.getKind() != TypeKind.TYPEVAR || supertype.getKind() != TypeKind.TYPEVAR) {
                throw new BugInCF("Missing primary annotations: subtype: %s, supertype: %s", subtype, supertype);
            }
        } else if (subAnno == null) {
            if (subtype.getKind() != TypeKind.TYPEVAR) {
                throw new BugInCF("Missing primary annotations: subtype: %s", subtype);
            }
            Set<AnnotationMirror> lb = findEffectiveLowerBoundAnnotations(qualifierHierarchy, subtype);
            AnnotationMirror lbAnno = qualifierHierarchy.findAnnotationInHierarchy(lb, top);
            if (lbAnno != null && !qualifierHierarchy.isSubtype(lbAnno, superAnno)) {
                // The superAnno is lower than the lower bound annotation, so add it.
                glb.addAnnotation(superAnno);
            }
        // else don't add any annotation.
        } else {
            throw new BugInCF("GLB: subtype: %s, supertype: %s", subtype, supertype);
        }
    }
    return glb;
}
Also used : AnnotationMirror(javax.lang.model.element.AnnotationMirror) BugInCF(org.checkerframework.javacutil.BugInCF) AnnotatedTypeMirror(org.checkerframework.framework.type.AnnotatedTypeMirror)

Aggregations

BugInCF (org.checkerframework.javacutil.BugInCF)127 AnnotatedTypeMirror (org.checkerframework.framework.type.AnnotatedTypeMirror)29 ArrayList (java.util.ArrayList)28 AnnotationMirror (javax.lang.model.element.AnnotationMirror)26 TypeElement (javax.lang.model.element.TypeElement)26 TypeMirror (javax.lang.model.type.TypeMirror)25 ExecutableElement (javax.lang.model.element.ExecutableElement)24 MethodTree (com.sun.source.tree.MethodTree)20 ExpressionTree (com.sun.source.tree.ExpressionTree)18 VariableTree (com.sun.source.tree.VariableTree)18 Element (javax.lang.model.element.Element)18 ClassTree (com.sun.source.tree.ClassTree)17 MethodInvocationTree (com.sun.source.tree.MethodInvocationTree)17 NewClassTree (com.sun.source.tree.NewClassTree)17 LambdaExpressionTree (com.sun.source.tree.LambdaExpressionTree)16 IOException (java.io.IOException)16 Tree (com.sun.source.tree.Tree)15 Map (java.util.Map)15 List (java.util.List)14 VariableElement (javax.lang.model.element.VariableElement)14