Search in sources :

Example 81 with BugInCF

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

the class PerDirectorySuite method getParametersMethod.

/**
 * Returns method annotated @Parameters, typically the getTestDirs or getTestFiles method.
 */
private FrameworkMethod getParametersMethod(TestClass testClass) {
    final List<FrameworkMethod> parameterMethods = testClass.getAnnotatedMethods(Parameters.class);
    if (parameterMethods.size() != 1) {
        // Construct error message
        String methods;
        if (parameterMethods.isEmpty()) {
            methods = "[No methods specified]";
        } else {
            StringJoiner sj = new StringJoiner(", ");
            for (FrameworkMethod method : parameterMethods) {
                sj.add(method.getName());
            }
            methods = sj.toString();
        }
        throw new BugInCF("Exactly one of the following methods should be declared:%n%s%n" + "testClass=%s%n" + "parameterMethods=%s", requiredFormsMessage, testClass.getName(), methods);
    }
    FrameworkMethod method = parameterMethods.get(0);
    Class<?> returnType = method.getReturnType();
    String methodName = method.getName();
    switch(methodName) {
        case "getTestDirs":
            if (!(returnType.isArray() && returnType.getComponentType() == String.class)) {
                throw new RuntimeException("getTestDirs should return String[], found " + returnType);
            }
            break;
        default:
            throw new RuntimeException(requiredFormsMessage + "%n" + "testClass=" + testClass.getName() + "%n" + "parameterMethods=" + method);
    }
    int modifiers = method.getMethod().getModifiers();
    if (!Modifier.isStatic(modifiers) || !Modifier.isPublic(modifiers)) {
        throw new RuntimeException("Parameter method (" + method.getName() + ") must be public and static");
    }
    return method;
}
Also used : BugInCF(org.checkerframework.javacutil.BugInCF) FrameworkMethod(org.junit.runners.model.FrameworkMethod) StringJoiner(java.util.StringJoiner)

Example 82 with BugInCF

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

the class TypeArgInferenceUtil method leastUpperBound.

/**
 * Successively calls least upper bound on the elements of types. Unlike leastUpperBound, this
 * method will box primitives if necessary
 */
public static AnnotatedTypeMirror leastUpperBound(final AnnotatedTypeFactory typeFactory, final Iterable<AnnotatedTypeMirror> types) {
    final Iterator<AnnotatedTypeMirror> typesIter = types.iterator();
    if (!typesIter.hasNext()) {
        throw new BugInCF("Calling LUB on empty list");
    }
    AnnotatedTypeMirror lubType = typesIter.next();
    AnnotatedTypeMirror nextType = null;
    while (typesIter.hasNext()) {
        nextType = typesIter.next();
        if (lubType.getKind().isPrimitive()) {
            if (!nextType.getKind().isPrimitive()) {
                lubType = typeFactory.getBoxedType((AnnotatedPrimitiveType) lubType);
            }
        } else if (nextType.getKind().isPrimitive()) {
            if (!lubType.getKind().isPrimitive()) {
                nextType = typeFactory.getBoxedType((AnnotatedPrimitiveType) nextType);
            }
        }
        lubType = AnnotatedTypes.leastUpperBound(typeFactory, lubType, nextType);
    }
    return lubType;
}
Also used : BugInCF(org.checkerframework.javacutil.BugInCF) AnnotatedTypeMirror(org.checkerframework.framework.type.AnnotatedTypeMirror) AnnotatedPrimitiveType(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedPrimitiveType)

Example 83 with BugInCF

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

the class TypeArgInferenceUtil method assignedTo.

/**
 * Returns the annotated type that the leaf of path is assigned to, if it is within an assignment
 * context. Returns the annotated type that the method invocation at the leaf is assigned to. If
 * the result is a primitive, return the boxed version.
 *
 * @param atypeFactory the type factory, for looking up types
 * @param path the path whole leaf to look up a type for
 * @return the type of path's leaf
 */
// AST node comparisons
@SuppressWarnings("interning:not.interned")
public static AnnotatedTypeMirror assignedTo(AnnotatedTypeFactory atypeFactory, TreePath path) {
    Tree assignmentContext = TreePathUtil.getAssignmentContext(path);
    AnnotatedTypeMirror res;
    if (assignmentContext == null) {
        res = null;
    } else if (assignmentContext instanceof AssignmentTree) {
        ExpressionTree variable = ((AssignmentTree) assignmentContext).getVariable();
        res = atypeFactory.getAnnotatedType(variable);
    } else if (assignmentContext instanceof CompoundAssignmentTree) {
        ExpressionTree variable = ((CompoundAssignmentTree) assignmentContext).getVariable();
        res = atypeFactory.getAnnotatedType(variable);
    } else if (assignmentContext instanceof MethodInvocationTree) {
        MethodInvocationTree methodInvocation = (MethodInvocationTree) assignmentContext;
        // TODO move to getAssignmentContext
        if (methodInvocation.getMethodSelect() instanceof MemberSelectTree && ((MemberSelectTree) methodInvocation.getMethodSelect()).getExpression() == path.getLeaf()) {
            return null;
        }
        ExecutableElement methodElt = TreeUtils.elementFromUse(methodInvocation);
        AnnotatedTypeMirror receiver = atypeFactory.getReceiverType(methodInvocation);
        if (TreeUtils.isSuperConstructorCall(methodInvocation)) {
            receiver = atypeFactory.getSelfType(methodInvocation);
        }
        res = assignedToExecutable(atypeFactory, path, methodElt, receiver, methodInvocation.getArguments());
    } else if (assignmentContext instanceof NewArrayTree) {
        // TODO: I left the previous implementation below, it definitely caused infinite loops
        // TODO: if you called it from places like the TreeAnnotator.
        res = null;
    // TODO: This may cause infinite loop
    // AnnotatedTypeMirror type =
    // atypeFactory.getAnnotatedType((NewArrayTree)assignmentContext);
    // type = AnnotatedTypes.innerMostType(type);
    // return type;
    } else if (assignmentContext instanceof NewClassTree) {
        // This need to be basically like MethodTree
        NewClassTree newClassTree = (NewClassTree) assignmentContext;
        if (newClassTree.getEnclosingExpression() instanceof NewClassTree && (newClassTree.getEnclosingExpression() == path.getLeaf())) {
            return null;
        }
        ExecutableElement constructorElt = TreeUtils.constructor(newClassTree);
        AnnotatedTypeMirror receiver = atypeFactory.fromNewClass(newClassTree);
        res = assignedToExecutable(atypeFactory, path, constructorElt, receiver, newClassTree.getArguments());
    } else if (assignmentContext instanceof ReturnTree) {
        HashSet<Tree.Kind> kinds = new HashSet<>(Arrays.asList(Tree.Kind.LAMBDA_EXPRESSION, Tree.Kind.METHOD));
        Tree enclosing = TreePathUtil.enclosingOfKind(path, kinds);
        if (enclosing.getKind() == Tree.Kind.METHOD) {
            res = atypeFactory.getAnnotatedType((MethodTree) enclosing).getReturnType();
        } else {
            AnnotatedExecutableType fninf = atypeFactory.getFunctionTypeFromTree((LambdaExpressionTree) enclosing);
            res = fninf.getReturnType();
        }
    } else if (assignmentContext instanceof VariableTree) {
        res = assignedToVariable(atypeFactory, assignmentContext);
    } else {
        throw new BugInCF("AnnotatedTypes.assignedTo: shouldn't be here");
    }
    if (res != null && TypesUtils.isPrimitive(res.getUnderlyingType())) {
        return atypeFactory.getBoxedType((AnnotatedPrimitiveType) res);
    } else {
        return res;
    }
}
Also used : MemberSelectTree(com.sun.source.tree.MemberSelectTree) ExecutableElement(javax.lang.model.element.ExecutableElement) VariableTree(com.sun.source.tree.VariableTree) NewClassTree(com.sun.source.tree.NewClassTree) ReturnTree(com.sun.source.tree.ReturnTree) BugInCF(org.checkerframework.javacutil.BugInCF) AnnotatedTypeMirror(org.checkerframework.framework.type.AnnotatedTypeMirror) CompoundAssignmentTree(com.sun.source.tree.CompoundAssignmentTree) AnnotatedExecutableType(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedExecutableType) NewArrayTree(com.sun.source.tree.NewArrayTree) MethodInvocationTree(com.sun.source.tree.MethodInvocationTree) TypeKind(javax.lang.model.type.TypeKind) CompoundAssignmentTree(com.sun.source.tree.CompoundAssignmentTree) MethodInvocationTree(com.sun.source.tree.MethodInvocationTree) AssignmentTree(com.sun.source.tree.AssignmentTree) LambdaExpressionTree(com.sun.source.tree.LambdaExpressionTree) NewArrayTree(com.sun.source.tree.NewArrayTree) ConditionalExpressionTree(com.sun.source.tree.ConditionalExpressionTree) MemberSelectTree(com.sun.source.tree.MemberSelectTree) ReturnTree(com.sun.source.tree.ReturnTree) MethodTree(com.sun.source.tree.MethodTree) VariableTree(com.sun.source.tree.VariableTree) NewClassTree(com.sun.source.tree.NewClassTree) Tree(com.sun.source.tree.Tree) ExpressionTree(com.sun.source.tree.ExpressionTree) LambdaExpressionTree(com.sun.source.tree.LambdaExpressionTree) ConditionalExpressionTree(com.sun.source.tree.ConditionalExpressionTree) ExpressionTree(com.sun.source.tree.ExpressionTree) CompoundAssignmentTree(com.sun.source.tree.CompoundAssignmentTree) AssignmentTree(com.sun.source.tree.AssignmentTree) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 84 with BugInCF

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

the class QualifierDefaults method isElementAnnotatedForThisChecker.

private boolean isElementAnnotatedForThisChecker(final Element elt) {
    boolean elementAnnotatedForThisChecker = false;
    if (elt == null) {
        throw new BugInCF("Call of QualifierDefaults.isElementAnnotatedForThisChecker with null");
    }
    if (elementAnnotatedFors.containsKey(elt)) {
        return elementAnnotatedFors.get(elt);
    }
    final AnnotationMirror annotatedFor = atypeFactory.getDeclAnnotation(elt, AnnotatedFor.class);
    if (annotatedFor != null) {
        elementAnnotatedForThisChecker = atypeFactory.doesAnnotatedForApplyToThisChecker(annotatedFor);
    }
    if (!elementAnnotatedForThisChecker) {
        Element parent;
        if (elt.getKind() == ElementKind.PACKAGE) {
            // elt.getEnclosingElement() on a package is null; therefore,
            // use the dedicated method.
            parent = ElementUtils.parentPackage((PackageElement) elt, elements);
        } else {
            parent = elt.getEnclosingElement();
        }
        if (parent != null && isElementAnnotatedForThisChecker(parent)) {
            elementAnnotatedForThisChecker = true;
        }
    }
    elementAnnotatedFors.put(elt, elementAnnotatedForThisChecker);
    return elementAnnotatedForThisChecker;
}
Also used : AnnotationMirror(javax.lang.model.element.AnnotationMirror) Element(javax.lang.model.element.Element) PackageElement(javax.lang.model.element.PackageElement) ExecutableElement(javax.lang.model.element.ExecutableElement) TypeParameterElement(javax.lang.model.element.TypeParameterElement) PackageElement(javax.lang.model.element.PackageElement) BugInCF(org.checkerframework.javacutil.BugInCF)

Example 85 with BugInCF

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

the class DependentTypesHelper method atLocalVariable.

/**
 * Standardize the Java expressions in annotations in a type. Converts the parameter syntax, e.g.
 * "#2", to the parameter name.
 *
 * @param type the type to standardize; is side-effected by this method
 * @param elt the element whose type is {@code type}
 */
public void atLocalVariable(AnnotatedTypeMirror type, Element elt) {
    if (!hasDependentType(type)) {
        return;
    }
    switch(elt.getKind()) {
        case PARAMETER:
        case LOCAL_VARIABLE:
        case RESOURCE_VARIABLE:
        case EXCEPTION_PARAMETER:
            Tree declarationTree = factory.declarationFromElement(elt);
            if (declarationTree == null) {
                if (elt.getKind() == ElementKind.PARAMETER) {
                    // assignment context for a pseudo assignment of an argument to a method parameter.
                    return;
                }
                throw new BugInCF(this.getClass() + ": tree not found");
            } else if (TreeUtils.typeOf(declarationTree) == null) {
                // assignment context for a pseudo assignment of an argument to a method parameter.
                return;
            }
            atVariableDeclaration(type, declarationTree, (VariableElement) elt);
            return;
        default:
            // for example), so there is nothing to do.
            break;
    }
}
Also used : MethodInvocationTree(com.sun.source.tree.MethodInvocationTree) LambdaExpressionTree(com.sun.source.tree.LambdaExpressionTree) MemberSelectTree(com.sun.source.tree.MemberSelectTree) ModifiersTree(com.sun.source.tree.ModifiersTree) AnnotationTree(com.sun.source.tree.AnnotationTree) MethodTree(com.sun.source.tree.MethodTree) VariableTree(com.sun.source.tree.VariableTree) NewClassTree(com.sun.source.tree.NewClassTree) Tree(com.sun.source.tree.Tree) ClassTree(com.sun.source.tree.ClassTree) ExpressionTree(com.sun.source.tree.ExpressionTree) JCTree(com.sun.tools.javac.tree.JCTree) 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