Search in sources :

Example 26 with Pair

use of org.checkerframework.javacutil.Pair in project bazel by bazelbuild.

the class Analysis method getReturnStatementStores.

public List<Pair<ReturnNode, TransferResult<A, S>>> getReturnStatementStores() {
    List<Pair<ReturnNode, TransferResult<A, S>>> result = new ArrayList<>();
    for (ReturnNode returnNode : cfg.getReturnNodes()) {
        TransferResult<A, S> store = storesAtReturnStatements.get(returnNode);
        result.add(Pair.of(returnNode, store));
    }
    return result;
}
Also used : ReturnNode(org.checkerframework.dataflow.cfg.node.ReturnNode) ArrayList(java.util.ArrayList) Pair(org.checkerframework.javacutil.Pair)

Example 27 with Pair

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

the class DefaultTypeHierarchy method fixUpRawTypes.

/**
 * Some times we create type arguments for types that were raw. When we do an asSuper we lose
 * these arguments. If in the converted type (i.e. the subtype as super) is missing type
 * arguments AND those type arguments should come from the original subtype's type arguments
 * then we copy the original type arguments to the converted type. e.g. We have a type W, that
 * "wasRaw" {@code ArrayList<? extends Object>} When W is converted to type A, List, using
 * asSuper it no longer has its type argument. But since the type argument to List should be the
 * same as that to ArrayList we copy over the type argument of W to A. A becomes {@code List<?
 * extends Object>}
 *
 * @param originalSubtype the subtype before being converted by asSuper
 * @param asSuperType he subtype after being converted by asSuper
 * @param supertype the supertype for which asSuperType should have the same underlying type
 * @param types the types utility
 */
private static void fixUpRawTypes(final AnnotatedTypeMirror originalSubtype, final AnnotatedTypeMirror asSuperType, final AnnotatedTypeMirror supertype, final Types types) {
    if (asSuperType != null && asSuperType.getKind() == TypeKind.DECLARED && originalSubtype.getKind() == TypeKind.DECLARED) {
        final AnnotatedDeclaredType declaredAsSuper = (AnnotatedDeclaredType) asSuperType;
        final AnnotatedDeclaredType declaredSubtype = (AnnotatedDeclaredType) originalSubtype;
        if (declaredAsSuper.wasRaw() && declaredAsSuper.getTypeArguments().isEmpty() && !declaredSubtype.getTypeArguments().isEmpty()) {
            Set<Pair<Integer, Integer>> typeArgMap = TypeArgumentMapper.mapTypeArgumentIndices((TypeElement) declaredSubtype.getUnderlyingType().asElement(), (TypeElement) declaredAsSuper.getUnderlyingType().asElement(), types);
            if (typeArgMap.size() == declaredSubtype.getTypeArguments().size()) {
                List<AnnotatedTypeMirror> newTypeArgs = new ArrayList<>();
                List<Pair<Integer, Integer>> orderedByDestination = new ArrayList<>(typeArgMap);
                Collections.sort(orderedByDestination, new Comparator<Pair<Integer, Integer>>() {

                    @Override
                    public int compare(Pair<Integer, Integer> o1, Pair<Integer, Integer> o2) {
                        return o1.second - o2.second;
                    }
                });
                final List<? extends AnnotatedTypeMirror> subTypeArgs = declaredSubtype.getTypeArguments();
                if (typeArgMap.size() == ((AnnotatedDeclaredType) supertype).getTypeArguments().size()) {
                    for (Pair<Integer, Integer> mapping : orderedByDestination) {
                        newTypeArgs.add(subTypeArgs.get(mapping.first).deepCopy());
                    }
                }
                declaredAsSuper.setTypeArguments(newTypeArgs);
            }
        }
    }
}
Also used : AnnotatedDeclaredType(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedDeclaredType) ArrayList(java.util.ArrayList) Pair(org.checkerframework.javacutil.Pair)

Example 28 with Pair

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

the class ContractsUtils method getPostconditions.

/**
 * Returns the set of postconditions on the method {@code methodElement}.
 */
public Set<Postcondition> getPostconditions(ExecutableElement methodElement) {
    Set<Postcondition> result = new LinkedHashSet<>();
    // Check for a single contract.
    AnnotationMirror ensuresAnnotation = factory.getDeclAnnotation(methodElement, EnsuresQualifier.class);
    result.addAll(getPostcondition(ensuresAnnotation));
    // Check for multiple contracts.
    AnnotationMirror ensuresAnnotations = factory.getDeclAnnotation(methodElement, EnsuresQualifiers.class);
    if (ensuresAnnotations != null) {
        List<AnnotationMirror> annotations = AnnotationUtils.getElementValueArray(ensuresAnnotations, "value", AnnotationMirror.class, false);
        for (AnnotationMirror a : annotations) {
            result.addAll(getPostcondition(a));
        }
    }
    // Check type-system specific annotations.
    Class<PostconditionAnnotation> metaAnnotation = PostconditionAnnotation.class;
    List<Pair<AnnotationMirror, AnnotationMirror>> declAnnotations = factory.getDeclAnnotationWithMetaAnnotation(methodElement, metaAnnotation);
    for (Pair<AnnotationMirror, AnnotationMirror> r : declAnnotations) {
        AnnotationMirror anno = r.first;
        AnnotationMirror metaAnno = r.second;
        List<String> expressions = AnnotationUtils.getElementValueArray(anno, "value", String.class, false);
        AnnotationMirror postcondAnno = getAnnotationMirrorOfMetaAnnotation(metaAnno, anno);
        if (postcondAnno == null) {
            continue;
        }
        for (String expr : expressions) {
            result.add(new Postcondition(expr, postcondAnno));
        }
    }
    return result;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) AnnotationMirror(javax.lang.model.element.AnnotationMirror) ConditionalPostconditionAnnotation(org.checkerframework.framework.qual.ConditionalPostconditionAnnotation) PostconditionAnnotation(org.checkerframework.framework.qual.PostconditionAnnotation) Pair(org.checkerframework.javacutil.Pair)

Example 29 with Pair

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

the class ContractsUtils method getConditionalPostconditions.

/**
 * Returns a set of triples {@code (expr, (result, annotation))} of conditional postconditions
 * on the method {@code methodElement}.
 */
public Set<ConditionalPostcondition> getConditionalPostconditions(ExecutableElement methodElement) {
    Set<ConditionalPostcondition> result = new LinkedHashSet<>();
    // Check for a single contract.
    AnnotationMirror ensuresQualifierIf = factory.getDeclAnnotation(methodElement, EnsuresQualifierIf.class);
    result.addAll(getConditionalPostcondition(ensuresQualifierIf));
    // Check for multiple contracts.
    AnnotationMirror ensuresAnnotationsIf = factory.getDeclAnnotation(methodElement, EnsuresQualifiersIf.class);
    if (ensuresAnnotationsIf != null) {
        List<AnnotationMirror> annotations = AnnotationUtils.getElementValueArray(ensuresAnnotationsIf, "value", AnnotationMirror.class, false);
        for (AnnotationMirror a : annotations) {
            result.addAll(getConditionalPostcondition(a));
        }
    }
    // Check type-system specific annotations.
    Class<ConditionalPostconditionAnnotation> metaAnnotation = ConditionalPostconditionAnnotation.class;
    List<Pair<AnnotationMirror, AnnotationMirror>> declAnnotations = factory.getDeclAnnotationWithMetaAnnotation(methodElement, metaAnnotation);
    for (Pair<AnnotationMirror, AnnotationMirror> r : declAnnotations) {
        AnnotationMirror anno = r.first;
        AnnotationMirror metaAnno = r.second;
        List<String> expressions = AnnotationUtils.getElementValueArray(anno, "expression", String.class, false);
        AnnotationMirror postcondAnno = getAnnotationMirrorOfMetaAnnotation(metaAnno, anno);
        if (postcondAnno == null) {
            continue;
        }
        boolean annoResult = AnnotationUtils.getElementValue(anno, "result", Boolean.class, false);
        for (String expr : expressions) {
            result.add(new ConditionalPostcondition(expr, annoResult, postcondAnno));
        }
    }
    return result;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) AnnotationMirror(javax.lang.model.element.AnnotationMirror) ConditionalPostconditionAnnotation(org.checkerframework.framework.qual.ConditionalPostconditionAnnotation) Pair(org.checkerframework.javacutil.Pair)

Example 30 with Pair

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

the class FlowExpressionParseUtil method parseMethod.

private static Receiver parseMethod(String s, FlowExpressionContext context, TreePath path, ProcessingEnvironment env) throws FlowExpressionParseException {
    Pair<Pair<String, String>, String> method = parseMethod(s);
    if (method == null) {
        return null;
    }
    String methodName = method.first.first;
    // parse parameter list
    String parameterList = method.first.second;
    List<Receiver> parameters = ParameterListParser.parseParameterList(parameterList, true, context.copyAndUseOuterReceiver(), path);
    // get types for parameters
    List<TypeMirror> parameterTypes = new ArrayList<>();
    for (Receiver p : parameters) {
        parameterTypes.add(p.getType());
    }
    ExecutableElement methodElement = null;
    try {
        Element element = null;
        // try to find the correct method
        Resolver resolver = new Resolver(env);
        TypeMirror receiverType = context.receiver.getType();
        if (receiverType.getKind() == TypeKind.ARRAY) {
            element = resolver.findMethod(methodName, receiverType, path, parameterTypes);
        }
        // Search for method in each enclosing class.
        while (receiverType.getKind() == TypeKind.DECLARED) {
            element = resolver.findMethod(methodName, receiverType, path, parameterTypes);
            if (element.getKind() == ElementKind.METHOD) {
                break;
            }
            receiverType = getTypeOfEnclosingClass((DeclaredType) receiverType);
        }
        if (element == null) {
            throw constructParserException(s, "element==null");
        }
        if (element.getKind() != ElementKind.METHOD) {
            throw constructParserException(s, "element.getKind()==" + element.getKind());
        }
        methodElement = (ExecutableElement) element;
        for (int i = 0; i < parameters.size(); i++) {
            VariableElement formal = methodElement.getParameters().get(i);
            TypeMirror formalType = formal.asType();
            Receiver actual = parameters.get(i);
            TypeMirror actualType = actual.getType();
            // boxing necessary
            if (TypesUtils.isBoxedPrimitive(formalType) && TypesUtils.isPrimitive(actualType)) {
                MethodSymbol valueOfMethod = TreeBuilder.getValueOfMethod(env, formalType);
                List<Receiver> p = new ArrayList<>();
                p.add(actual);
                Receiver boxedParam = new MethodCall(formalType, valueOfMethod, new ClassName(formalType), p);
                parameters.set(i, boxedParam);
            }
        }
    } catch (Throwable t) {
        throw constructParserException(s, t);
    }
    assert methodElement != null;
    /*if (!PurityUtils.isDeterministic(context.checkerContext.getAnnotationProvider(),
                methodElement)) {
            throw new FlowExpressionParseException(Result.failure(
                    "flowexpr.method.not.deterministic",
                    methodElement.getSimpleName()));
        }*/
    if (ElementUtils.isStatic(methodElement)) {
        Element classElem = methodElement.getEnclosingElement();
        Receiver staticClassReceiver = new ClassName(ElementUtils.getType(classElem));
        return new MethodCall(ElementUtils.getType(methodElement), methodElement, staticClassReceiver, parameters);
    } else {
        if (context.receiver instanceof ClassName) {
            throw constructParserException(s, "a non-static method call cannot have a class name as a receiver.");
        }
        TypeMirror methodType = TypesUtils.substituteMethodReturnType(methodElement, context.receiver.getType(), env);
        return new MethodCall(methodType, methodElement, context.receiver, parameters);
    }
}
Also used : Resolver(org.checkerframework.javacutil.Resolver) ExecutableElement(javax.lang.model.element.ExecutableElement) TypeElement(javax.lang.model.element.TypeElement) Element(javax.lang.model.element.Element) VariableElement(javax.lang.model.element.VariableElement) ExecutableElement(javax.lang.model.element.ExecutableElement) ArrayList(java.util.ArrayList) Receiver(org.checkerframework.dataflow.analysis.FlowExpressions.Receiver) VariableElement(javax.lang.model.element.VariableElement) MethodCall(org.checkerframework.dataflow.analysis.FlowExpressions.MethodCall) MethodSymbol(com.sun.tools.javac.code.Symbol.MethodSymbol) TypeMirror(javax.lang.model.type.TypeMirror) ClassName(org.checkerframework.dataflow.analysis.FlowExpressions.ClassName) Pair(org.checkerframework.javacutil.Pair) DeclaredType(javax.lang.model.type.DeclaredType)

Aggregations

Pair (org.checkerframework.javacutil.Pair)31 AnnotationMirror (javax.lang.model.element.AnnotationMirror)12 ArrayList (java.util.ArrayList)11 LinkedHashSet (java.util.LinkedHashSet)8 TypeElement (javax.lang.model.element.TypeElement)7 TypeMirror (javax.lang.model.type.TypeMirror)7 MethodTree (com.sun.source.tree.MethodTree)6 Tree (com.sun.source.tree.Tree)6 HashMap (java.util.HashMap)6 ExecutableElement (javax.lang.model.element.ExecutableElement)6 VariableElement (javax.lang.model.element.VariableElement)6 ClassTree (com.sun.source.tree.ClassTree)5 VariableTree (com.sun.source.tree.VariableTree)5 ExpressionTree (com.sun.source.tree.ExpressionTree)4 NewClassTree (com.sun.source.tree.NewClassTree)4 LinkedHashMap (java.util.LinkedHashMap)4 Map (java.util.Map)4 Element (javax.lang.model.element.Element)4 Nullable (org.checkerframework.checker.nullness.qual.Nullable)4 Receiver (org.checkerframework.dataflow.analysis.FlowExpressions.Receiver)4