Search in sources :

Example 96 with BugInCF

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

the class PropagationTypeAnnotator method applyAnnosFromBound.

/**
 * Take the primary annotations from typeParamBound and place them as primary annotations on
 * wildcard bound.
 */
private void applyAnnosFromBound(final AnnotatedTypeMirror wildcardBound, final AnnotatedTypeMirror typeParamBound, final Set<? extends AnnotationMirror> tops) {
    // bounds via its declaration or defaulting rules
    if (wildcardBound.getKind() == TypeKind.TYPEVAR || typeParamBound.getKind() == TypeKind.TYPEVAR) {
        return;
    }
    for (final AnnotationMirror top : tops) {
        if (wildcardBound.getAnnotationInHierarchy(top) == null) {
            final AnnotationMirror typeParamAnno = typeParamBound.getAnnotationInHierarchy(top);
            if (typeParamAnno == null) {
                throw new BugInCF(StringsPlume.joinLines("Missing annotation on type parameter", "top=" + top, "wildcardBound=" + wildcardBound, "typeParamBound=" + typeParamBound));
            }
            // else
            wildcardBound.addAnnotation(typeParamAnno);
        }
    }
}
Also used : AnnotationMirror(javax.lang.model.element.AnnotationMirror) BugInCF(org.checkerframework.javacutil.BugInCF)

Example 97 with BugInCF

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

the class TypeFromTypeTreeVisitor method getTypeVariableFromDeclaration.

/**
 * If a tree is can be found for the declaration of the type variable {@code type}, then a {@link
 * AnnotatedTypeVariable} is returned with explicit annotations from the type variables declared
 * bounds. If a tree cannot be found, then {@code type}, converted to a use, is returned.
 *
 * @param type type variable used to find declaration tree
 * @param f annotated type factory
 * @return the AnnotatedTypeVariable from the declaration of {@code type} or {@code type} if no
 *     tree is found.
 */
private AnnotatedTypeVariable getTypeVariableFromDeclaration(AnnotatedTypeVariable type, AnnotatedTypeFactory f) {
    TypeVariable typeVar = type.getUnderlyingType();
    TypeParameterElement tpe = (TypeParameterElement) typeVar.asElement();
    Element elt = tpe.getGenericElement();
    if (elt instanceof TypeElement) {
        TypeElement typeElt = (TypeElement) elt;
        int idx = typeElt.getTypeParameters().indexOf(tpe);
        ClassTree cls = (ClassTree) f.declarationFromElement(typeElt);
        if (cls == null || cls.getTypeParameters().isEmpty()) {
            // contains all necessary information and we can return that.
            return type.asUse();
        }
        // will return a declaration ATV.  So change it to a use.
        return visitTypeParameter(cls.getTypeParameters().get(idx), f).asUse();
    } else if (elt instanceof ExecutableElement) {
        ExecutableElement exElt = (ExecutableElement) elt;
        int idx = exElt.getTypeParameters().indexOf(tpe);
        MethodTree meth = (MethodTree) f.declarationFromElement(exElt);
        if (meth == null) {
            // + elt);
            return type.asUse();
        }
        // This works the same as the case above.  Even though `meth` itself is not a
        // type declaration tree, the elements of `meth.getTypeParameters()` still are.
        AnnotatedTypeVariable result = visitTypeParameter(meth.getTypeParameters().get(idx), f).shallowCopy();
        result.setDeclaration(false);
        return result;
    } else if (TypesUtils.isCapturedTypeVariable(typeVar)) {
        // not an element at all, namely Symtab.noSymbol.
        return type.asUse();
    } else {
        throw new BugInCF("TypeFromTree.forTypeVariable: not a supported element: " + elt);
    }
}
Also used : AnnotatedTypeVariable(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedTypeVariable) TypeVariable(javax.lang.model.type.TypeVariable) MethodTree(com.sun.source.tree.MethodTree) TypeElement(javax.lang.model.element.TypeElement) TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) Element(javax.lang.model.element.Element) TypeParameterElement(javax.lang.model.element.TypeParameterElement) ExecutableElement(javax.lang.model.element.ExecutableElement) ClassTree(com.sun.source.tree.ClassTree) BugInCF(org.checkerframework.javacutil.BugInCF) AnnotatedTypeVariable(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedTypeVariable) TypeParameterElement(javax.lang.model.element.TypeParameterElement)

Example 98 with BugInCF

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

the class CheckerMain method findPathTo.

/**
 * Find the jar file or directory containing the .class file from which cls was loaded.
 *
 * @param cls the class whose .class file we wish to locate; if null, CheckerMain.class
 * @param errIfFromDirectory if false, throw an exception if the file was loaded from a directory
 */
public static String findPathTo(Class<?> cls, boolean errIfFromDirectory) throws IllegalStateException {
    if (cls == null) {
        cls = CheckerMain.class;
    }
    String name = cls.getName();
    String classFileName;
    /* name is something like pakkage.name.ContainingClass$ClassName. We need to turn this into ContainingClass$ClassName.class. */
    {
        int idx = name.lastIndexOf('.');
        classFileName = (idx == -1 ? name : name.substring(idx + 1)) + ".class";
    }
    String uri = cls.getResource(classFileName).toString();
    if (uri.startsWith("file:")) {
        if (errIfFromDirectory) {
            return uri;
        } else {
            throw new IllegalStateException("This class has been loaded from a directory and not from a jar file.");
        }
    }
    if (!uri.startsWith("jar:file:")) {
        int idx = uri.indexOf(':');
        String protocol = idx == -1 ? "(unknown)" : uri.substring(0, idx);
        throw new IllegalStateException("This class has been loaded remotely via the " + protocol + " protocol. Only loading from a jar on the local file system is supported.");
    }
    int idx = uri.indexOf('!');
    // Sanity check
    if (idx == -1) {
        throw new IllegalStateException("You appear to have loaded this class from a local jar file, but URI has no \"!\": " + uri);
    }
    try {
        String fileName = URLDecoder.decode(uri.substring("jar:file:".length(), idx), Charset.defaultCharset().name());
        return new File(fileName).getAbsolutePath();
    } catch (UnsupportedEncodingException e) {
        throw new BugInCF("Default charset doesn't exist. Your VM is borked.");
    }
}
Also used : UnsupportedEncodingException(java.io.UnsupportedEncodingException) BugInCF(org.checkerframework.javacutil.BugInCF) File(java.io.File)

Example 99 with BugInCF

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

the class JavaExpression method fromNode.

/**
 * We ignore operations such as widening and narrowing when computing the internal representation.
 *
 * @param receiverNode a node to convert to a JavaExpression
 * @return the internal representation of the given node. Might contain {@link Unknown}.
 */
public static JavaExpression fromNode(Node receiverNode) {
    JavaExpression result = null;
    if (receiverNode instanceof FieldAccessNode) {
        result = fromNodeFieldAccess((FieldAccessNode) receiverNode);
    } else if (receiverNode instanceof ExplicitThisNode) {
        result = new ThisReference(receiverNode.getType());
    } else if (receiverNode instanceof ThisNode) {
        result = new ThisReference(receiverNode.getType());
    } else if (receiverNode instanceof SuperNode) {
        result = new ThisReference(receiverNode.getType());
    } else if (receiverNode instanceof LocalVariableNode) {
        LocalVariableNode lv = (LocalVariableNode) receiverNode;
        result = new LocalVariable(lv);
    } else if (receiverNode instanceof ArrayAccessNode) {
        ArrayAccessNode a = (ArrayAccessNode) receiverNode;
        result = fromArrayAccess(a);
    } else if (receiverNode instanceof StringConversionNode) {
        // ignore string conversion
        return fromNode(((StringConversionNode) receiverNode).getOperand());
    } else if (receiverNode instanceof WideningConversionNode) {
        // ignore widening
        return fromNode(((WideningConversionNode) receiverNode).getOperand());
    } else if (receiverNode instanceof NarrowingConversionNode) {
        // ignore narrowing
        return fromNode(((NarrowingConversionNode) receiverNode).getOperand());
    } else if (receiverNode instanceof UnaryOperationNode) {
        UnaryOperationNode uopn = (UnaryOperationNode) receiverNode;
        return new UnaryOperation(uopn, fromNode(uopn.getOperand()));
    } else if (receiverNode instanceof BinaryOperationNode) {
        BinaryOperationNode bopn = (BinaryOperationNode) receiverNode;
        return new BinaryOperation(bopn, fromNode(bopn.getLeftOperand()), fromNode(bopn.getRightOperand()));
    } else if (receiverNode instanceof ClassNameNode) {
        ClassNameNode cn = (ClassNameNode) receiverNode;
        result = new ClassName(cn.getType());
    } else if (receiverNode instanceof ValueLiteralNode) {
        ValueLiteralNode vn = (ValueLiteralNode) receiverNode;
        result = new ValueLiteral(vn.getType(), vn);
    } else if (receiverNode instanceof ArrayCreationNode) {
        ArrayCreationNode an = (ArrayCreationNode) receiverNode;
        List<@Nullable JavaExpression> dimensions = CollectionsPlume.mapList(JavaExpression::fromNode, an.getDimensions());
        List<JavaExpression> initializers = CollectionsPlume.mapList(JavaExpression::fromNode, an.getInitializers());
        result = new ArrayCreation(an.getType(), dimensions, initializers);
    } else if (receiverNode instanceof MethodInvocationNode) {
        MethodInvocationNode mn = (MethodInvocationNode) receiverNode;
        MethodInvocationTree t = mn.getTree();
        if (t == null) {
            throw new BugInCF("Unexpected null tree for node: " + mn);
        }
        assert TreeUtils.isUseOfElement(t) : "@AssumeAssertion(nullness): tree kind";
        ExecutableElement invokedMethod = TreeUtils.elementFromUse(t);
        // Note that the method might be nondeterministic.
        List<JavaExpression> parameters = CollectionsPlume.mapList(JavaExpression::fromNode, mn.getArguments());
        JavaExpression methodReceiver;
        if (ElementUtils.isStatic(invokedMethod)) {
            methodReceiver = new ClassName(mn.getTarget().getReceiver().getType());
        } else {
            methodReceiver = fromNode(mn.getTarget().getReceiver());
        }
        result = new MethodCall(mn.getType(), invokedMethod, methodReceiver, parameters);
    }
    if (result == null) {
        result = new Unknown(receiverNode);
    }
    return result;
}
Also used : ExecutableElement(javax.lang.model.element.ExecutableElement) ClassNameNode(org.checkerframework.dataflow.cfg.node.ClassNameNode) StringConversionNode(org.checkerframework.dataflow.cfg.node.StringConversionNode) ExplicitThisNode(org.checkerframework.dataflow.cfg.node.ExplicitThisNode) ArrayCreationNode(org.checkerframework.dataflow.cfg.node.ArrayCreationNode) WideningConversionNode(org.checkerframework.dataflow.cfg.node.WideningConversionNode) BinaryOperationNode(org.checkerframework.dataflow.cfg.node.BinaryOperationNode) NarrowingConversionNode(org.checkerframework.dataflow.cfg.node.NarrowingConversionNode) MethodInvocationTree(com.sun.source.tree.MethodInvocationTree) List(java.util.List) ArrayList(java.util.ArrayList) MethodInvocationNode(org.checkerframework.dataflow.cfg.node.MethodInvocationNode) SuperNode(org.checkerframework.dataflow.cfg.node.SuperNode) ArrayAccessNode(org.checkerframework.dataflow.cfg.node.ArrayAccessNode) BugInCF(org.checkerframework.javacutil.BugInCF) FieldAccessNode(org.checkerframework.dataflow.cfg.node.FieldAccessNode) LocalVariableNode(org.checkerframework.dataflow.cfg.node.LocalVariableNode) UnaryOperationNode(org.checkerframework.dataflow.cfg.node.UnaryOperationNode) ValueLiteralNode(org.checkerframework.dataflow.cfg.node.ValueLiteralNode) ThisNode(org.checkerframework.dataflow.cfg.node.ThisNode) ExplicitThisNode(org.checkerframework.dataflow.cfg.node.ExplicitThisNode) Nullable(org.checkerframework.checker.nullness.qual.Nullable)

Example 100 with BugInCF

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

the class JavaExpression method getImplicitReceiver.

/**
 * Returns the implicit receiver of ele.
 *
 * <p>Returns either a new ClassName or a new ThisReference depending on whether ele is static or
 * not. The passed element must be a field, method, or class.
 *
 * @param ele a field, method, or class
 * @return either a new ClassName or a new ThisReference depending on whether ele is static or not
 */
public static JavaExpression getImplicitReceiver(Element ele) {
    TypeElement enclosingTypeElement = ElementUtils.enclosingTypeElement(ele);
    if (enclosingTypeElement == null) {
        throw new BugInCF("getImplicitReceiver's arg has no enclosing type: " + ele);
    }
    TypeMirror enclosingType = enclosingTypeElement.asType();
    if (ElementUtils.isStatic(ele)) {
        return new ClassName(enclosingType);
    } else {
        return new ThisReference(enclosingType);
    }
}
Also used : TypeMirror(javax.lang.model.type.TypeMirror) TypeElement(javax.lang.model.element.TypeElement) BugInCF(org.checkerframework.javacutil.BugInCF)

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