Search in sources :

Example 6 with ParameterizedExecutableType

use of org.checkerframework.framework.type.AnnotatedTypeFactory.ParameterizedExecutableType 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}
 * @return the resolved type of the call
 */
private ParameterizedExecutableType resolveConstructorCall(AnnotatedTypeFactory factory, MethodInvocationTree tree, ParameterizedExecutableType origResult) {
    debugReflection("Try to resolve reflective constructor call: " + tree);
    List<JCNewClass> possibleConstructors = resolveReflectiveConstructor(tree, factory);
    // Reflective constructor could not be resolved
    if (possibleConstructors.isEmpty()) {
        return origResult;
    }
    Set<? extends AnnotationMirror> returnLub = null;
    Set<? extends AnnotationMirror> paramsGlb = null;
    // Iterate over all possible constructors: lub return types and glb parameter types
    for (JCNewClass resolvedTree : possibleConstructors) {
        debugReflection("Resolved constructor invocation: " + resolvedTree);
        if (!checkNewClassArguments(resolvedTree)) {
            debugReflection("Spoofed tree's arguments did not match declaration" + resolvedTree);
            // QualifierPolymorphism.PolyCollector.visitArray(...)
            continue;
        }
        ParameterizedExecutableType resolvedResult = factory.constructorFromUse(resolvedTree);
        // Lub return types
        returnLub = lub(returnLub, resolvedResult.executableType.getReturnType().getAnnotations(), factory);
        // Glb parameter types
        for (AnnotatedTypeMirror mirror : resolvedResult.executableType.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.executableType.getReturnType().clearPrimaryAnnotations();
    origResult.executableType.getReturnType().addAnnotations(returnLub);
    // parameter types
    if (paramsGlb != null) {
        AnnotatedArrayType origArrayType = (AnnotatedArrayType) origResult.executableType.getParameterTypes().get(0);
        origArrayType.getComponentType().clearPrimaryAnnotations();
        origArrayType.getComponentType().addAnnotations(paramsGlb);
    }
    debugReflection("Resolved annotations: " + origResult.executableType);
    return origResult;
}
Also used : AnnotatedArrayType(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedArrayType) JCNewClass(com.sun.tools.javac.tree.JCTree.JCNewClass) ParameterizedExecutableType(org.checkerframework.framework.type.AnnotatedTypeFactory.ParameterizedExecutableType) AnnotatedTypeMirror(org.checkerframework.framework.type.AnnotatedTypeMirror)

Aggregations

ParameterizedExecutableType (org.checkerframework.framework.type.AnnotatedTypeFactory.ParameterizedExecutableType)6 AnnotatedTypeMirror (org.checkerframework.framework.type.AnnotatedTypeMirror)6 ExecutableElement (javax.lang.model.element.ExecutableElement)4 AnnotatedExecutableType (org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedExecutableType)4 ExpressionTree (com.sun.source.tree.ExpressionTree)2 MethodInvocationTree (com.sun.source.tree.MethodInvocationTree)2 MethodTree (com.sun.source.tree.MethodTree)2 AnnotatedArrayType (org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedArrayType)2 AnnotatedTypeVariable (org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedTypeVariable)2 AnnotatedTypeParameterBounds (org.checkerframework.framework.type.AnnotatedTypeParameterBounds)2 AssignmentTree (com.sun.source.tree.AssignmentTree)1 ClassTree (com.sun.source.tree.ClassTree)1 LambdaExpressionTree (com.sun.source.tree.LambdaExpressionTree)1 NewClassTree (com.sun.source.tree.NewClassTree)1 ReturnTree (com.sun.source.tree.ReturnTree)1 Tree (com.sun.source.tree.Tree)1 VariableTree (com.sun.source.tree.VariableTree)1 JCNewClass (com.sun.tools.javac.tree.JCTree.JCNewClass)1 ArrayList (java.util.ArrayList)1 AnnotationMirror (javax.lang.model.element.AnnotationMirror)1