Search in sources :

Example 26 with AnnotatedExecutableType

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

the class GenericAnnotatedTypeFactory method constructorFromUse.

@Override
public Pair<AnnotatedExecutableType, List<AnnotatedTypeMirror>> constructorFromUse(NewClassTree tree) {
    Pair<AnnotatedExecutableType, List<AnnotatedTypeMirror>> mfuPair = super.constructorFromUse(tree);
    AnnotatedExecutableType method = mfuPair.first;
    if (dependentTypesHelper != null) {
        dependentTypesHelper.viewpointAdaptConstructor(tree, method);
    }
    poly.annotate(tree, method);
    return mfuPair;
}
Also used : AnnotatedExecutableType(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedExecutableType) ArrayList(java.util.ArrayList) List(java.util.List)

Example 27 with AnnotatedExecutableType

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

the class TypeFromMemberVisitor method inferLambdaParamAnnotations.

/**
 * @return the type of the lambda parameter or null if paramElement is not a lambda parameter
 */
private static AnnotatedTypeMirror inferLambdaParamAnnotations(AnnotatedTypeFactory f, AnnotatedTypeMirror lambdaParam, Element paramElement) {
    if (paramElement.getKind() != ElementKind.PARAMETER || f.declarationFromElement(paramElement) == null || f.getPath(f.declarationFromElement(paramElement)) == null || f.getPath(f.declarationFromElement(paramElement)).getParentPath() == null) {
        return null;
    }
    Tree declaredInTree = f.getPath(f.declarationFromElement(paramElement)).getParentPath().getLeaf();
    if (declaredInTree.getKind() == Kind.LAMBDA_EXPRESSION) {
        LambdaExpressionTree lambdaDecl = (LambdaExpressionTree) declaredInTree;
        int index = lambdaDecl.getParameters().indexOf(f.declarationFromElement(paramElement));
        Pair<AnnotatedDeclaredType, AnnotatedExecutableType> res = f.getFnInterfaceFromTree(lambdaDecl);
        AnnotatedExecutableType functionType = res.second;
        AnnotatedTypeMirror funcTypeParam = functionType.getParameterTypes().get(index);
        if (TreeUtils.isImplicitlyTypedLambda(declaredInTree)) {
            if (f.types.isSubtype(funcTypeParam.actualType, lambdaParam.actualType)) {
                // (#979) isn't implement, check first.
                return AnnotatedTypes.asSuper(f, funcTypeParam, lambdaParam);
            }
            lambdaParam.addMissingAnnotations(funcTypeParam.getAnnotations());
            return lambdaParam;
        } else {
            // The lambda expression is explicitly typed, so the parameters have declared types:
            // (String s) -> ...
            // The declared type may or may not have explicit annotations.
            // If it does not have an annotation for a hierarchy, then copy the annotation from
            // the function type rather than use usual defaulting rules.
            // Note lambdaParam is a super type of funcTypeParam, so only primary annotations
            // can be copied.
            lambdaParam.addMissingAnnotations(funcTypeParam.getAnnotations());
            return lambdaParam;
        }
    }
    return null;
}
Also used : LambdaExpressionTree(com.sun.source.tree.LambdaExpressionTree) AnnotatedExecutableType(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedExecutableType) AnnotatedDeclaredType(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedDeclaredType) MethodTree(com.sun.source.tree.MethodTree) VariableTree(com.sun.source.tree.VariableTree) LambdaExpressionTree(com.sun.source.tree.LambdaExpressionTree) Tree(com.sun.source.tree.Tree)

Example 28 with AnnotatedExecutableType

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

the class TypeFromMemberVisitor method visitMethod.

@Override
public AnnotatedTypeMirror visitMethod(MethodTree node, AnnotatedTypeFactory f) {
    ExecutableElement elt = TreeUtils.elementFromDeclaration(node);
    AnnotatedExecutableType result = (AnnotatedExecutableType) f.toAnnotatedType(elt.asType(), false);
    result.setElement(elt);
    ElementAnnotationApplier.apply(result, elt, f);
    return result;
}
Also used : AnnotatedExecutableType(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedExecutableType) ExecutableElement(javax.lang.model.element.ExecutableElement)

Example 29 with AnnotatedExecutableType

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

the class TypesIntoElements method storeMethod.

private static void storeMethod(ProcessingEnvironment processingEnv, Types types, AnnotatedTypeFactory atypeFactory, MethodTree meth) {
    AnnotatedExecutableType mtype = atypeFactory.getAnnotatedType(meth);
    MethodSymbol sym = (MethodSymbol) TreeUtils.elementFromDeclaration(meth);
    TypeAnnotationPosition tapos;
    List<Attribute.TypeCompound> tcs = List.nil();
    storeTypeParameters(processingEnv, types, atypeFactory, meth.getTypeParameters(), sym);
    {
        // return type
        JCTree ret = ((JCTree.JCMethodDecl) meth).getReturnType();
        if (ret != null) {
            tapos = TypeAnnotationUtils.methodReturnTAPosition(ret.pos);
            tcs = tcs.appendList(generateTypeCompounds(processingEnv, mtype.getReturnType(), tapos));
        }
    }
    {
        // receiver
        JCTree recv = ((JCTree.JCMethodDecl) meth).getReceiverParameter();
        if (recv != null) {
            tapos = TypeAnnotationUtils.methodReceiverTAPosition(recv.pos);
            tcs = tcs.appendList(generateTypeCompounds(processingEnv, mtype.getReceiverType(), tapos));
        }
    }
    {
        // parameters
        int pidx = 0;
        java.util.List<AnnotatedTypeMirror> ptypes = mtype.getParameterTypes();
        for (JCTree param : ((JCTree.JCMethodDecl) meth).getParameters()) {
            tapos = TypeAnnotationUtils.methodParameterTAPosition(pidx, param.pos);
            tcs = tcs.appendList(generateTypeCompounds(processingEnv, ptypes.get(pidx), tapos));
            ++pidx;
        }
    }
    {
        // throws clauses
        int tidx = 0;
        java.util.List<AnnotatedTypeMirror> ttypes = mtype.getThrownTypes();
        for (JCTree thr : ((JCTree.JCMethodDecl) meth).getThrows()) {
            tapos = TypeAnnotationUtils.methodThrowsTAPosition(tidx, thr.pos);
            tcs = tcs.appendList(generateTypeCompounds(processingEnv, ttypes.get(tidx), tapos));
            ++tidx;
        }
    }
    addUniqueTypeCompounds(types, sym, tcs);
}
Also used : TypeCompound(com.sun.tools.javac.code.Attribute.TypeCompound) AnnotatedExecutableType(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedExecutableType) MethodSymbol(com.sun.tools.javac.code.Symbol.MethodSymbol) JCTree(com.sun.tools.javac.tree.JCTree) List(com.sun.tools.javac.util.List) TypeAnnotationPosition(com.sun.tools.javac.code.TypeAnnotationPosition)

Example 30 with AnnotatedExecutableType

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

the class LockAnnotatedTypeFactory method methodFromUse.

@Override
public Pair<AnnotatedExecutableType, List<AnnotatedTypeMirror>> methodFromUse(ExpressionTree tree, ExecutableElement methodElt, AnnotatedTypeMirror receiverType) {
    Pair<AnnotatedExecutableType, List<AnnotatedTypeMirror>> mfuPair = super.methodFromUse(tree, methodElt, receiverType);
    if (tree.getKind() != Kind.METHOD_INVOCATION) {
        return mfuPair;
    }
    // If a method's formal return type is annotated with @GuardSatisfied(index), look for the
    // first instance of @GuardSatisfied(index) in the method definition's receiver type or
    // formal parameters, retrieve the corresponding type of the actual parameter / receiver at
    // the call site (e.g. @GuardedBy("someLock") and replace the return type at the call site
    // with this type.
    AnnotatedExecutableType invokedMethod = mfuPair.first;
    if (invokedMethod.getElement().getKind() == ElementKind.CONSTRUCTOR) {
        return mfuPair;
    }
    AnnotatedTypeMirror methodDefinitionReturn = invokedMethod.getReturnType();
    if (methodDefinitionReturn == null || !methodDefinitionReturn.hasAnnotation(GuardSatisfied.class)) {
        return mfuPair;
    }
    int returnGuardSatisfiedIndex = getGuardSatisfiedIndex(methodDefinitionReturn);
    if (returnGuardSatisfiedIndex == -1) {
        return mfuPair;
    }
    if (!ElementUtils.isStatic(invokedMethod.getElement()) && replaceAnnotationInGuardedByHierarchyIfGuardSatisfiedIndexMatches(methodDefinitionReturn, invokedMethod.getReceiverType(), /* the method definition receiver*/
    returnGuardSatisfiedIndex, receiverType.getAnnotationInHierarchy(GUARDEDBYUNKNOWN))) {
        return mfuPair;
    }
    List<? extends ExpressionTree> methodInvocationTreeArguments = ((MethodInvocationTree) tree).getArguments();
    List<AnnotatedTypeMirror> requiredArgs = AnnotatedTypes.expandVarArgs(this, invokedMethod, methodInvocationTreeArguments);
    for (int i = 0; i < requiredArgs.size(); i++) {
        if (replaceAnnotationInGuardedByHierarchyIfGuardSatisfiedIndexMatches(methodDefinitionReturn, requiredArgs.get(i), returnGuardSatisfiedIndex, getAnnotatedType(methodInvocationTreeArguments.get(i)).getEffectiveAnnotationInHierarchy(GUARDEDBYUNKNOWN))) {
            return mfuPair;
        }
    }
    return mfuPair;
}
Also used : AnnotatedExecutableType(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedExecutableType) MethodInvocationTree(com.sun.source.tree.MethodInvocationTree) 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