Search in sources :

Example 41 with BugInCF

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

the class GenericAnnotatedTypeFactory method getAnnotatedTypeVarargsArray.

/**
 * Returns the type of a varargs array of a method invocation or a constructor invocation. Returns
 * null only if private field {@code useFlow} is false.
 *
 * @param tree a method invocation or a constructor invocation
 * @return AnnotatedTypeMirror of varargs array for a method or constructor invocation {@code
 *     tree}; returns null if private field {@code useFlow} is false
 */
@Nullable
public AnnotatedTypeMirror getAnnotatedTypeVarargsArray(Tree tree) {
    if (!useFlow) {
        return null;
    }
    // Get the synthetic NewArray tree that dataflow creates as the last argument of a call to a
    // vararg method. Do this by getting the MethodInvocationNode to which "tree" maps. The last
    // argument node of the MethodInvocationNode stores the synthetic NewArray tree.
    List<Node> args;
    switch(tree.getKind()) {
        case METHOD_INVOCATION:
            args = getFirstNodeOfKindForTree(tree, MethodInvocationNode.class).getArguments();
            break;
        case NEW_CLASS:
            args = getFirstNodeOfKindForTree(tree, ObjectCreationNode.class).getArguments();
            break;
        default:
            throw new BugInCF("Unexpected kind of tree: " + tree);
    }
    assert !args.isEmpty() : "Arguments are empty";
    Node varargsArray = args.get(args.size() - 1);
    AnnotatedTypeMirror varargtype = getAnnotatedType(varargsArray.getTree());
    return varargtype;
}
Also used : AssignmentNode(org.checkerframework.dataflow.cfg.node.AssignmentNode) ObjectCreationNode(org.checkerframework.dataflow.cfg.node.ObjectCreationNode) ReturnNode(org.checkerframework.dataflow.cfg.node.ReturnNode) MethodInvocationNode(org.checkerframework.dataflow.cfg.node.MethodInvocationNode) Node(org.checkerframework.dataflow.cfg.node.Node) BugInCF(org.checkerframework.javacutil.BugInCF) Nullable(org.checkerframework.checker.nullness.qual.Nullable)

Example 42 with BugInCF

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

the class GenericAnnotatedTypeFactory method getPostconditionAnnotations.

/**
 * Return the postcondition annotations for the given AMethod. Does not modify the AMethod. This
 * method must only be called when using WholeProgramInferenceScenes.
 *
 * @param m AFU representation of a method
 * @param preconds the precondition annotations for the method; used to suppress redundant
 *     postconditions
 * @return postcondition annotations for the method
 */
public List<AnnotationMirror> getPostconditionAnnotations(AMethod m, List<AnnotationMirror> preconds) {
    List<AnnotationMirror> result = new ArrayList<>(m.getPostconditions().size());
    for (Map.Entry<String, AField> entry : m.getPostconditions().entrySet()) {
        WholeProgramInferenceImplementation<?> wholeProgramInference = (WholeProgramInferenceImplementation<?>) getWholeProgramInference();
        WholeProgramInferenceScenesStorage storage = (WholeProgramInferenceScenesStorage) wholeProgramInference.getStorage();
        TypeMirror typeMirror = entry.getValue().getTypeMirror();
        if (typeMirror == null) {
            throw new BugInCF("null TypeMirror in AField inferred by WPI postcondition inference. AField: " + entry.getValue().toString());
        }
        AnnotatedTypeMirror declaredType = storage.getPostconditionDeclaredType(m, entry.getKey());
        AnnotatedTypeMirror inferredType = storage.atmFromStorageLocation(typeMirror, entry.getValue().type);
        result.addAll(getPostconditionAnnotations(entry.getKey(), inferredType, declaredType, preconds));
    }
    Collections.sort(result, Ordering.usingToString());
    return result;
}
Also used : WholeProgramInferenceImplementation(org.checkerframework.common.wholeprograminference.WholeProgramInferenceImplementation) ArrayList(java.util.ArrayList) WholeProgramInferenceScenesStorage(org.checkerframework.common.wholeprograminference.WholeProgramInferenceScenesStorage) BugInCF(org.checkerframework.javacutil.BugInCF) AField(scenelib.annotations.el.AField) AnnotationMirror(javax.lang.model.element.AnnotationMirror) TypeMirror(javax.lang.model.type.TypeMirror) Map(java.util.Map) IdentityHashMap(java.util.IdentityHashMap) HashMap(java.util.HashMap)

Example 43 with BugInCF

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

the class QualifierHierarchy method greatestLowerBounds.

/**
 * Returns the greatest lower bound of the two sets of qualifiers. The result is the lub of the
 * qualifier for the same hierarchy in each set.
 *
 * @param qualifiers1 set of qualifiers; exactly one per hierarchy
 * @param qualifiers2 set of qualifiers; exactly one per hierarchy
 * @return the greatest lower bound of the two sets of qualifiers
 */
default Set<? extends AnnotationMirror> greatestLowerBounds(Collection<? extends AnnotationMirror> qualifiers1, Collection<? extends AnnotationMirror> qualifiers2) {
    assertSameSize(qualifiers1, qualifiers2);
    if (qualifiers1.isEmpty()) {
        throw new BugInCF("QualifierHierarchy.greatestLowerBounds: tried to determine GLB with empty sets");
    }
    Set<AnnotationMirror> result = AnnotationUtils.createAnnotationSet();
    for (AnnotationMirror a1 : qualifiers1) {
        for (AnnotationMirror a2 : qualifiers2) {
            AnnotationMirror glb = greatestLowerBound(a1, a2);
            if (glb != null) {
                result.add(glb);
            }
        }
    }
    assertSameSize(qualifiers1, qualifiers2, result);
    return result;
}
Also used : AnnotationMirror(javax.lang.model.element.AnnotationMirror) BugInCF(org.checkerframework.javacutil.BugInCF)

Example 44 with BugInCF

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

the class AnnotationClassLoader method loadBundledAnnotationClasses.

/**
 * Loads the set of annotation classes in the qual directory of a checker shipped with the Checker
 * Framework.
 */
private void loadBundledAnnotationClasses() {
    // retrieve the fully qualified class names of the annotations
    Set<@BinaryName String> annotationNames;
    // see whether the resource URL has a protocol of jar or file
    if (resourceURL != null && resourceURL.getProtocol().contentEquals("jar")) {
        // if the checker class file is contained within a jar, then the resource URL for the qual
        // directory will have the protocol "jar". This means the whole checker is loaded as a jar
        // file.
        JarURLConnection connection;
        // create a connection to the jar file
        try {
            connection = (JarURLConnection) resourceURL.openConnection();
            // disable caching / connection sharing of the low level URLConnection to the Jar file
            connection.setDefaultUseCaches(false);
            connection.setUseCaches(false);
            // connect to the Jar file
            connection.connect();
        } catch (IOException e) {
            throw new BugInCF("AnnotationClassLoader: cannot open a connection to the Jar file " + resourceURL.getFile());
        }
        // open up that jar file and extract annotation class names
        try (JarFile jarFile = connection.getJarFile()) {
            // get class names inside the jar file within the particular package
            annotationNames = getBundledAnnotationNamesFromJar(jarFile);
        } catch (IOException e) {
            throw new BugInCF("AnnotationClassLoader: cannot open the Jar file " + resourceURL.getFile());
        }
    } else if (resourceURL != null && resourceURL.getProtocol().contentEquals("file")) {
        // If the checker class file is found within the file system itself within some directory
        // (usually development build directories), then process the package as a file directory in
        // the file system and load the annotations contained in the qual directory.
        // open up the directory
        File packageDir = new File(resourceURL.getFile());
        annotationNames = getAnnotationNamesFromDirectory(packageName, packageDir, packageDir);
    } else {
        // We do not support a resource URL with any other protocols, so create an empty set.
        annotationNames = Collections.emptySet();
    }
    if (annotationNames.isEmpty()) {
        PackageElement pkgEle = checker.getElementUtils().getPackageElement(packageName);
        if (pkgEle != null) {
            for (Element e : pkgEle.getEnclosedElements()) {
                if (e.getKind() == ElementKind.ANNOTATION_TYPE) {
                    // Elements needs to be annotated.
                    @SuppressWarnings("signature:assignment") @BinaryName String annoBinName = checker.getElementUtils().getBinaryName((TypeElement) e).toString();
                    annotationNames.add(annoBinName);
                }
            }
        }
    }
    supportedBundledAnnotationClasses.addAll(loadAnnotationClasses(annotationNames));
}
Also used : JarURLConnection(java.net.JarURLConnection) TypeElement(javax.lang.model.element.TypeElement) PackageElement(javax.lang.model.element.PackageElement) TypeElement(javax.lang.model.element.TypeElement) Element(javax.lang.model.element.Element) BinaryName(org.checkerframework.checker.signature.qual.BinaryName) IOException(java.io.IOException) PackageElement(javax.lang.model.element.PackageElement) BugInCF(org.checkerframework.javacutil.BugInCF) JarFile(java.util.jar.JarFile) JarFile(java.util.jar.JarFile) File(java.io.File)

Example 45 with BugInCF

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

the class DefaultInferredTypesApplier method removePrimaryAnnotationTypeVar.

private void removePrimaryAnnotationTypeVar(AnnotatedTypeVariable annotatedTypeVariable, TypeMirror inferredTypeMirror, AnnotationMirror top, AnnotationMirror previousAnnotation) {
    if (inferredTypeMirror.getKind() != TypeKind.TYPEVAR) {
        throw new BugInCF("Missing annos");
    }
    TypeVariable typeVar = (TypeVariable) inferredTypeMirror;
    AnnotatedTypeVariable typeVariableDecl = (AnnotatedTypeVariable) factory.getAnnotatedType(typeVar.asElement());
    AnnotationMirror upperBound = typeVariableDecl.getEffectiveAnnotationInHierarchy(top);
    if (omitSubtypingCheck || hierarchy.isSubtype(upperBound, previousAnnotation)) {
        annotatedTypeVariable.removeAnnotationInHierarchy(top);
        AnnotationMirror ub = typeVariableDecl.getUpperBound().getAnnotationInHierarchy(top);
        apply(annotatedTypeVariable.getUpperBound(), ub, typeVar.getUpperBound(), top);
        AnnotationMirror lb = typeVariableDecl.getLowerBound().getAnnotationInHierarchy(top);
        apply(annotatedTypeVariable.getLowerBound(), lb, typeVar.getLowerBound(), top);
    }
}
Also used : AnnotationMirror(javax.lang.model.element.AnnotationMirror) AnnotatedTypeVariable(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedTypeVariable) TypeVariable(javax.lang.model.type.TypeVariable) BugInCF(org.checkerframework.javacutil.BugInCF) AnnotatedTypeVariable(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedTypeVariable)

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