Search in sources :

Example 41 with AnnotatedExecutableType

use of org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedExecutableType in project checker-framework by typetools.

the class TypeArgInferenceUtil method assignedToExecutable.

private static AnnotatedTypeMirror assignedToExecutable(AnnotatedTypeFactory atypeFactory, TreePath path, ExecutableElement methodElt, AnnotatedTypeMirror receiver, List<? extends ExpressionTree> arguments) {
    AnnotatedExecutableType method = AnnotatedTypes.asMemberOf(atypeFactory.getContext().getTypeUtils(), atypeFactory, receiver, methodElt);
    int treeIndex = -1;
    for (int i = 0; i < arguments.size(); ++i) {
        ExpressionTree argumentTree = arguments.get(i);
        if (isArgument(path, argumentTree)) {
            treeIndex = i;
            break;
        }
    }
    final AnnotatedTypeMirror paramType;
    if (treeIndex == -1) {
        // The tree wasn't found as an argument, so it has to be the receiver.
        // This can happen for inner class constructors that take an outer class argument.
        paramType = method.getReceiverType();
    } else if (treeIndex >= method.getParameterTypes().size() && methodElt.isVarArgs()) {
        paramType = method.getParameterTypes().get(method.getParameterTypes().size() - 1);
    } else {
        paramType = method.getParameterTypes().get(treeIndex);
    }
    // would require solving the constraints for both type argument inferences simultaneously
    if (paramType == null || containsUninferredTypeParameter(paramType, method)) {
        return null;
    }
    return paramType;
}
Also used : AnnotatedExecutableType(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedExecutableType) LambdaExpressionTree(com.sun.source.tree.LambdaExpressionTree) ConditionalExpressionTree(com.sun.source.tree.ConditionalExpressionTree) ExpressionTree(com.sun.source.tree.ExpressionTree) AnnotatedTypeMirror(org.checkerframework.framework.type.AnnotatedTypeMirror)

Example 42 with AnnotatedExecutableType

use of org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedExecutableType in project checker-framework by typetools.

the class DefaultReflectionResolver method resolveConstructorCall.

/**
 * Resolves a call to {@link Constructor#newInstance(Object...)}.
 *
 * @param factory the {@link AnnotatedTypeFactory} of the underlying type system
 * @param tree the method invocation tree (representing a constructor call) that has to be
 *     resolved
 * @param origResult the original result from {@code factory.methodFromUse}.
 */
private Pair<AnnotatedExecutableType, List<AnnotatedTypeMirror>> resolveConstructorCall(AnnotatedTypeFactory factory, MethodInvocationTree tree, Pair<AnnotatedExecutableType, List<AnnotatedTypeMirror>> origResult) {
    debugReflection("Try to resolve reflective constructor call: " + tree);
    List<JCNewClass> possibleConstructors = resolveReflectiveConstructor(tree, factory);
    // Reflective constructor could not be resolved
    if (possibleConstructors.size() == 0) {
        return origResult;
    }
    Set<? extends AnnotationMirror> returnLub = null;
    Set<? extends AnnotationMirror> paramsGlb = null;
    // parameter types
    for (JCNewClass resolvedTree : possibleConstructors) {
        debugReflection("Resolved constructor invocation: " + resolvedTree);
        if (!checkNewClassArguments(resolvedTree)) {
            debugReflection("Spoofed tree's arguments did not match declaration" + resolvedTree.toString());
            // in QualifierPolymorphism.PolyCollector.visitArray(...)
            continue;
        }
        Pair<AnnotatedExecutableType, List<AnnotatedTypeMirror>> resolvedResult = factory.constructorFromUse(resolvedTree);
        // Lub return types
        returnLub = lub(returnLub, resolvedResult.first.getReturnType().getAnnotations(), factory);
        // Glb parameter types
        for (AnnotatedTypeMirror mirror : resolvedResult.first.getParameterTypes()) {
            paramsGlb = glb(paramsGlb, mirror.getAnnotations(), factory);
        }
    }
    if (returnLub == null) {
        // None of the spoofed tree's arguments matched the declared method
        return origResult;
    }
    /*
         * Clear all original (return, parameter type) annotations and set
         * lub/glb annotations from resolved constructors.
         */
    // return value
    origResult.first.getReturnType().clearAnnotations();
    origResult.first.getReturnType().addAnnotations(returnLub);
    // parameter types
    if (paramsGlb != null) {
        AnnotatedArrayType origArrayType = (AnnotatedArrayType) origResult.first.getParameterTypes().get(0);
        origArrayType.getComponentType().clearAnnotations();
        origArrayType.getComponentType().addAnnotations(paramsGlb);
    }
    debugReflection("Resolved annotations: " + origResult.first);
    return origResult;
}
Also used : AnnotatedExecutableType(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedExecutableType) AnnotatedArrayType(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedArrayType) JCNewClass(com.sun.tools.javac.tree.JCTree.JCNewClass) List(java.util.List) ArrayList(java.util.ArrayList) AnnotatedTypeMirror(org.checkerframework.framework.type.AnnotatedTypeMirror)

Aggregations

AnnotatedExecutableType (org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedExecutableType)42 AnnotatedTypeMirror (org.checkerframework.framework.type.AnnotatedTypeMirror)19 ExecutableElement (javax.lang.model.element.ExecutableElement)17 ArrayList (java.util.ArrayList)15 AnnotatedDeclaredType (org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedDeclaredType)15 ExpressionTree (com.sun.source.tree.ExpressionTree)13 MethodTree (com.sun.source.tree.MethodTree)13 List (java.util.List)13 LambdaExpressionTree (com.sun.source.tree.LambdaExpressionTree)12 MethodInvocationTree (com.sun.source.tree.MethodInvocationTree)12 Tree (com.sun.source.tree.Tree)12 VariableTree (com.sun.source.tree.VariableTree)12 ConditionalExpressionTree (com.sun.source.tree.ConditionalExpressionTree)9 AssignmentTree (com.sun.source.tree.AssignmentTree)7 ClassTree (com.sun.source.tree.ClassTree)7 NewArrayTree (com.sun.source.tree.NewArrayTree)7 ReturnTree (com.sun.source.tree.ReturnTree)7 NewClassTree (com.sun.source.tree.NewClassTree)6 VariableElement (javax.lang.model.element.VariableElement)6 AnnotationTree (com.sun.source.tree.AnnotationTree)5