Search in sources :

Example 1 with AMethod

use of scenelib.annotations.el.AMethod in project checker-framework by typetools.

the class WholeProgramInferenceScenes method updateInferredConstructorParameterTypes.

/**
 * Updates the parameter types of the constructor created by objectCreationNode based on
 * arguments to the constructor.
 *
 * <p>For each parameter in constructorElt:
 *
 * <ul>
 *   <li>If the Scene does not contain an annotated type for that parameter, then the type of
 *       the respective value passed as argument in the object creation call objectCreationNode
 *       will be added to the parameter in the Scene.
 *   <li>If the Scene previously contained an annotated type for that parameter, then its new
 *       type will be the LUB between the previous type and the type of the respective value
 *       passed as argument in the object creation call.
 * </ul>
 *
 * <p>
 *
 * @param objectCreationNode the new Object() node
 * @param atf the annotated type factory of a given type system, whose type hierarchy will be
 *     used to update the constructor's parameters' types
 */
@Override
public void updateInferredConstructorParameterTypes(ObjectCreationNode objectCreationNode, ExecutableElement constructorElt, AnnotatedTypeFactory atf) {
    ClassSymbol classSymbol = getEnclosingClassSymbol(objectCreationNode.getTree());
    if (classSymbol == null) {
        // https://github.com/typetools/checker-framework/issues/682
        return;
    }
    String className = classSymbol.flatname.toString();
    String jaifPath = helper.getJaifPath(className);
    AClass clazz = helper.getAClass(className, jaifPath);
    String methodName = JVMNames.getJVMMethodName(constructorElt);
    AMethod method = clazz.methods.vivify(methodName);
    List<Node> arguments = objectCreationNode.getArguments();
    updateInferredExecutableParameterTypes(constructorElt, atf, jaifPath, method, arguments);
}
Also used : ClassSymbol(com.sun.tools.javac.code.Symbol.ClassSymbol) FieldAccessNode(org.checkerframework.dataflow.cfg.node.FieldAccessNode) ObjectCreationNode(org.checkerframework.dataflow.cfg.node.ObjectCreationNode) ReturnNode(org.checkerframework.dataflow.cfg.node.ReturnNode) MethodInvocationNode(org.checkerframework.dataflow.cfg.node.MethodInvocationNode) ImplicitThisLiteralNode(org.checkerframework.dataflow.cfg.node.ImplicitThisLiteralNode) LocalVariableNode(org.checkerframework.dataflow.cfg.node.LocalVariableNode) Node(org.checkerframework.dataflow.cfg.node.Node) AClass(scenelib.annotations.el.AClass) AMethod(scenelib.annotations.el.AMethod)

Example 2 with AMethod

use of scenelib.annotations.el.AMethod in project checker-framework by typetools.

the class WholeProgramInferenceScenes method updateInferredMethodReceiverType.

/**
 * Updates the receiver type of the method {@code methodElt} in the Scene of the method's
 * enclosing class based on the overridden method {@code overriddenMethod} receiver type.
 *
 * <p>For the receiver in methodElt:
 *
 * <ul>
 *   <li>If the Scene does not contain an annotated type for the receiver, then the type of the
 *       receiver on the overridden method will be added to the receiver in the Scene.
 *   <li>If the Scene previously contained an annotated type for the receiver, then its new type
 *       will be the LUB between the previous type and the type of the receiver on the
 *       overridden method.
 * </ul>
 *
 * <p>
 *
 * @param methodTree the tree of the method that contains the receiver
 * @param methodElt the element of the method
 * @param overriddenMethod the overridden method
 * @param atf the annotated type factory of a given type system, whose type hierarchy will be
 *     used to update the receiver type
 */
@Override
public void updateInferredMethodReceiverType(MethodTree methodTree, ExecutableElement methodElt, AnnotatedExecutableType overriddenMethod, AnnotatedTypeFactory atf) {
    ClassSymbol classSymbol = getEnclosingClassSymbol(methodTree);
    String className = classSymbol.flatname.toString();
    String jaifPath = helper.getJaifPath(className);
    AClass clazz = helper.getAClass(className, jaifPath);
    String methodName = JVMNames.getJVMMethodName(methodElt);
    AMethod method = clazz.methods.vivify(methodName);
    AnnotatedDeclaredType argADT = overriddenMethod.getReceiverType();
    if (argADT != null) {
        AnnotatedTypeMirror paramATM = atf.getAnnotatedType(methodTree).getReceiverType();
        if (paramATM != null) {
            AField receiver = method.receiver;
            helper.updateAnnotationSetInScene(receiver.type, atf, jaifPath, argADT, paramATM, TypeUseLocation.RECEIVER);
        }
    }
}
Also used : ClassSymbol(com.sun.tools.javac.code.Symbol.ClassSymbol) AnnotatedDeclaredType(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedDeclaredType) AClass(scenelib.annotations.el.AClass) AMethod(scenelib.annotations.el.AMethod) AnnotatedTypeMirror(org.checkerframework.framework.type.AnnotatedTypeMirror) AField(scenelib.annotations.el.AField)

Example 3 with AMethod

use of scenelib.annotations.el.AMethod in project checker-framework by typetools.

the class WholeProgramInferenceScenes method updateInferredParameterType.

/**
 * Updates the parameter type represented by lhs of the method methodTree in the Scene of the
 * receiverTree's enclosing class based on assignments to the parameter inside the method body.
 *
 * <ul>
 *   <li>If the Scene does not contain an annotated type for that parameter, then the type of
 *       the respective value passed as argument in the method call methodInvNode will be added
 *       to the parameter in the Scene.
 *   <li>If the Scene previously contained an annotated type for that parameter, then its new
 *       type will be the LUB between the previous type and the type of the respective value
 *       passed as argument in the method call.
 * </ul>
 *
 * <p>
 *
 * @param lhs the node representing the parameter
 * @param rhs the node being assigned to the parameter
 * @param classTree the tree of the class that contains the parameter
 * @param methodTree the tree of the method that contains the parameter
 * @param atf the annotated type factory of a given type system, whose type hierarchy will be
 *     used to update the parameter type
 */
@Override
public void updateInferredParameterType(LocalVariableNode lhs, Node rhs, ClassTree classTree, MethodTree methodTree, AnnotatedTypeFactory atf) {
    ClassSymbol classSymbol = getEnclosingClassSymbol(classTree, lhs);
    // https://github.com/typetools/checker-framework/issues/682
    if (classSymbol == null)
        return;
    String className = classSymbol.flatname.toString();
    String jaifPath = helper.getJaifPath(className);
    AClass clazz = helper.getAClass(className, jaifPath);
    String methodName = JVMNames.getJVMMethodName(methodTree);
    AMethod method = clazz.methods.vivify(methodName);
    List<? extends VariableTree> params = methodTree.getParameters();
    // Look-up parameter by name:
    for (int i = 0; i < params.size(); i++) {
        VariableTree vt = params.get(i);
        if (vt.getName().contentEquals(lhs.getName())) {
            Tree treeNode = rhs.getTree();
            if (treeNode == null) {
                // https://github.com/typetools/checker-framework/issues/682
                continue;
            }
            AnnotatedTypeMirror paramATM = atf.getAnnotatedType(vt);
            AnnotatedTypeMirror argATM = atf.getAnnotatedType(treeNode);
            AField param = method.parameters.vivify(i);
            helper.updateAnnotationSetInScene(param.type, atf, jaifPath, argATM, paramATM, TypeUseLocation.PARAMETER);
            break;
        }
    }
}
Also used : ClassSymbol(com.sun.tools.javac.code.Symbol.ClassSymbol) VariableTree(com.sun.source.tree.VariableTree) MethodTree(com.sun.source.tree.MethodTree) VariableTree(com.sun.source.tree.VariableTree) Tree(com.sun.source.tree.Tree) ClassTree(com.sun.source.tree.ClassTree) AClass(scenelib.annotations.el.AClass) AMethod(scenelib.annotations.el.AMethod) AnnotatedTypeMirror(org.checkerframework.framework.type.AnnotatedTypeMirror) AField(scenelib.annotations.el.AField)

Example 4 with AMethod

use of scenelib.annotations.el.AMethod in project checker-framework by typetools.

the class WholeProgramInferenceScenes method updateInferredMethodReturnType.

/**
 * Updates the return type of the method methodTree in the Scene of the class with symbol
 * classSymbol.
 *
 * <p>If the Scene does not contain an annotated return type for the method methodTree, then the
 * type of the value passed to the return expression will be added to the return type of that
 * method in the Scene. If the Scene previously contained an annotated return type for the
 * method methodTree, its new type will be the LUB between the previous type and the type of the
 * value passed to the return expression.
 *
 * <p>
 *
 * @param retNode the node that contains the expression returned
 * @param classSymbol the symbol of the class that contains the method
 * @param methodTree the tree of the method whose return type may be updated
 * @param atf the annotated type factory of a given type system, whose type hierarchy will be
 *     used to update the method's return type
 */
@Override
public void updateInferredMethodReturnType(ReturnNode retNode, ClassSymbol classSymbol, MethodTree methodTree, AnnotatedTypeFactory atf) {
    // TODO: Handle anonymous classes.
    if (classSymbol == null)
        return;
    String className = classSymbol.flatname.toString();
    String jaifPath = helper.getJaifPath(className);
    AClass clazz = helper.getAClass(className, jaifPath);
    AMethod method = clazz.methods.vivify(JVMNames.getJVMMethodName(methodTree));
    // Method return type
    AnnotatedTypeMirror lhsATM = atf.getAnnotatedType(methodTree).getReturnType();
    // Type of the expression returned
    AnnotatedTypeMirror rhsATM = atf.getAnnotatedType(retNode.getTree().getExpression());
    helper.updateAnnotationSetInScene(method.returnType, atf, jaifPath, rhsATM, lhsATM, TypeUseLocation.RETURN);
}
Also used : AClass(scenelib.annotations.el.AClass) AMethod(scenelib.annotations.el.AMethod) AnnotatedTypeMirror(org.checkerframework.framework.type.AnnotatedTypeMirror)

Example 5 with AMethod

use of scenelib.annotations.el.AMethod in project j2objc by google.

the class ExternalAnnotationInjector method visit.

@Override
public boolean visit(MethodDeclaration node) {
    if (!annotatedElementStack.peekLast().isPresent()) {
        return true;
    }
    AClass annotatedParent = (AClass) annotatedElementStack.peekLast().get();
    ExecutableElement executable = node.getExecutableElement();
    String prefix = elementUtil.getBinaryName(ElementUtil.getDeclaringClass(executable)) + ".";
    String methodName = Mappings.getMethodKey(executable, typeUtil).substring(prefix.length());
    AMethod annotatedMethod = annotatedParent.methods.get(methodName);
    if (annotatedMethod != null) {
        Set<Annotation> annotations = new LinkedHashSet<>();
        annotations.addAll(annotatedMethod.tlAnnotationsHere);
        annotations.addAll(annotatedMethod.returnType.tlAnnotationsHere);
        recordAnnotations(node.getExecutableElement(), annotations);
        injectAnnotationsToNode(node, annotations);
    }
    return true;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) GeneratedExecutableElement(com.google.devtools.j2objc.types.GeneratedExecutableElement) ExecutableElement(javax.lang.model.element.ExecutableElement) AClass(scenelib.annotations.el.AClass) AMethod(scenelib.annotations.el.AMethod) NormalAnnotation(com.google.devtools.j2objc.ast.NormalAnnotation) Annotation(scenelib.annotations.Annotation)

Aggregations

AMethod (scenelib.annotations.el.AMethod)19 AClass (scenelib.annotations.el.AClass)13 AField (scenelib.annotations.el.AField)9 Annotation (scenelib.annotations.Annotation)6 ClassSymbol (com.sun.tools.javac.code.Symbol.ClassSymbol)5 AnnotatedTypeMirror (org.checkerframework.framework.type.AnnotatedTypeMirror)4 AnnotationExpr (com.github.javaparser.ast.expr.AnnotationExpr)3 Parameter (com.github.javaparser.ast.body.Parameter)2 ReceiverParameter (com.github.javaparser.ast.body.ReceiverParameter)2 BlockStmt (com.github.javaparser.ast.stmt.BlockStmt)2 ArrayType (com.github.javaparser.ast.type.ArrayType)2 ClassOrInterfaceType (com.github.javaparser.ast.type.ClassOrInterfaceType)2 PrimitiveType (com.github.javaparser.ast.type.PrimitiveType)2 ReferenceType (com.github.javaparser.ast.type.ReferenceType)2 Type (com.github.javaparser.ast.type.Type)2 TypeParameter (com.github.javaparser.ast.type.TypeParameter)2 VoidType (com.github.javaparser.ast.type.VoidType)2 WildcardType (com.github.javaparser.ast.type.WildcardType)2 Map (java.util.Map)2 VariableElement (javax.lang.model.element.VariableElement)2