Search in sources :

Example 6 with AMethod

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

the class ToIndexFileConverter method visit.

@Override
public Void visit(MethodDeclaration decl, AElement elem) {
    Type type = decl.getType();
    List<Parameter> params = decl.getParameters();
    List<TypeParameter> typeParams = decl.getTypeParameters();
    Optional<ReceiverParameter> rcvrParam = decl.getReceiverParameter();
    BlockStmt body = decl.getBody().orElse(null);
    StringBuilder sb = new StringBuilder(decl.getNameAsString()).append('(');
    AClass clazz = (AClass) elem;
    AMethod method;
    if (params != null) {
        for (Parameter param : params) {
            Type ptype = param.getType();
            sb.append(getJVML(ptype));
        }
    }
    sb.append(')').append(getJVML(type));
    method = clazz.methods.vivify(sb.toString());
    visitDecl(decl, method);
    visitType(type, method.returnType);
    if (params != null) {
        for (int i = 0; i < params.size(); i++) {
            Parameter param = params.get(i);
            AField field = method.parameters.vivify(i);
            visitType(param.getType(), field.type);
        }
    }
    if (rcvrParam.isPresent()) {
        for (AnnotationExpr expr : rcvrParam.get().getAnnotations()) {
            Annotation anno = extractAnnotation(expr);
            method.receiver.type.tlAnnotationsHere.add(anno);
        }
    }
    if (typeParams != null) {
        for (int i = 0; i < typeParams.size(); i++) {
            TypeParameter typeParam = typeParams.get(i);
            List<ClassOrInterfaceType> bounds = typeParam.getTypeBound();
            if (bounds != null) {
                for (int j = 0; j < bounds.size(); j++) {
                    ClassOrInterfaceType bound = bounds.get(j);
                    BoundLocation loc = new BoundLocation(i, j);
                    bound.accept(this, method.bounds.vivify(loc));
                }
            }
        }
    }
    return body == null ? null : body.accept(this, method);
}
Also used : TypeParameter(com.github.javaparser.ast.type.TypeParameter) AnnotationExpr(com.github.javaparser.ast.expr.AnnotationExpr) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) Annotation(scenelib.annotations.Annotation) AField(scenelib.annotations.el.AField) ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) Type(com.github.javaparser.ast.type.Type) ReferenceType(com.github.javaparser.ast.type.ReferenceType) VoidType(com.github.javaparser.ast.type.VoidType) ArrayType(com.github.javaparser.ast.type.ArrayType) PrimitiveType(com.github.javaparser.ast.type.PrimitiveType) WildcardType(com.github.javaparser.ast.type.WildcardType) ReceiverParameter(com.github.javaparser.ast.body.ReceiverParameter) ReceiverParameter(com.github.javaparser.ast.body.ReceiverParameter) Parameter(com.github.javaparser.ast.body.Parameter) TypeParameter(com.github.javaparser.ast.type.TypeParameter) AClass(scenelib.annotations.el.AClass) BoundLocation(scenelib.annotations.el.BoundLocation) AMethod(scenelib.annotations.el.AMethod)

Example 7 with AMethod

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

the class WholeProgramInferenceScenes method updateInferredMethodParameterTypes.

/**
 * Updates the parameter types of the method {@code methodElt} in the Scene of the method's
 * enclosing class based on the overridden method {@code overriddenMethod} parameter types.
 *
 * <p>For each method parameter in methodElt:
 *
 * <ul>
 *   <li>If the Scene does not contain an annotated type for that parameter, then the type of
 *       the respective parameter on the overridden method 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 parameter
 *       on the overridden method.
 * </ul>
 *
 * <p>
 *
 * @param methodTree the tree of the method that contains the parameter
 * @param methodElt the element of the method
 * @param overriddenMethod the AnnotatedExecutableType of the overridden method
 * @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 updateInferredMethodParameterTypes(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);
    for (int i = 0; i < overriddenMethod.getParameterTypes().size(); i++) {
        VariableElement ve = methodElt.getParameters().get(i);
        AnnotatedTypeMirror paramATM = atf.getAnnotatedType(ve);
        AnnotatedTypeMirror argATM = overriddenMethod.getParameterTypes().get(i);
        AField param = method.parameters.vivify(i);
        helper.updateAnnotationSetInScene(param.type, atf, jaifPath, argATM, paramATM, TypeUseLocation.PARAMETER);
    }
}
Also used : ClassSymbol(com.sun.tools.javac.code.Symbol.ClassSymbol) AClass(scenelib.annotations.el.AClass) VariableElement(javax.lang.model.element.VariableElement) AMethod(scenelib.annotations.el.AMethod) AnnotatedTypeMirror(org.checkerframework.framework.type.AnnotatedTypeMirror) AField(scenelib.annotations.el.AField)

Example 8 with AMethod

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

the class WholeProgramInferenceScenes method updateInferredMethodParameterTypes.

/**
 * Updates the parameter types of the method methodElt in the Scene of the receiverTree's
 * enclosing class based on the arguments to the method.
 *
 * <p>For each method parameter in methodElt:
 *
 * <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 methodInvNode the node representing a method invocation
 * @param receiverTree the Tree of the class that contains the method being invoked
 * @param methodElt the element of the method being invoked
 * @param atf the annotated type factory of a given type system, whose type hierarchy will be
 *     used to update the method parameters' types
 */
@Override
public void updateInferredMethodParameterTypes(MethodInvocationNode methodInvNode, Tree receiverTree, ExecutableElement methodElt, AnnotatedTypeFactory atf) {
    if (receiverTree == null) {
        // https://github.com/typetools/checker-framework/issues/682
        return;
    }
    ClassSymbol classSymbol = getEnclosingClassSymbol(receiverTree);
    if (classSymbol == null) {
        // https://github.com/typetools/checker-framework/issues/682
        return;
    }
    // https://github.com/typetools/checker-framework/issues/682
    if (!classSymbol.getEnclosedElements().contains((Symbol) methodElt)) {
        return;
    }
    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);
    List<Node> arguments = methodInvNode.getArguments();
    updateInferredExecutableParameterTypes(methodElt, atf, jaifPath, method, arguments);
}
Also used : ClassSymbol(com.sun.tools.javac.code.Symbol.ClassSymbol) MethodSymbol(com.sun.tools.javac.code.Symbol.MethodSymbol) ClassSymbol(com.sun.tools.javac.code.Symbol.ClassSymbol) VarSymbol(com.sun.tools.javac.code.Symbol.VarSymbol) TypeSymbol(com.sun.tools.javac.code.Symbol.TypeSymbol) Symbol(com.sun.tools.javac.code.Symbol) 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 9 with AMethod

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

the class WholeProgramInferenceScenesHelper method removeIgnoredAnnosFromScene.

/**
 * Removes all annotations that should be ignored from an AScene. (See {@link #shouldIgnore}).
 */
private void removeIgnoredAnnosFromScene(AScene scene) {
    for (AClass aclass : scene.classes.values()) {
        for (AField field : aclass.fields.values()) {
            removeIgnoredAnnosFromATypeElement(field.type, TypeUseLocation.FIELD);
        }
        for (AMethod method : aclass.methods.values()) {
            // Return type
            removeIgnoredAnnosFromATypeElement(method.returnType, TypeUseLocation.RETURN);
            // Receiver type
            removeIgnoredAnnosFromATypeElement(method.receiver.type, TypeUseLocation.RECEIVER);
            // Parameter type
            for (AField param : method.parameters.values()) {
                removeIgnoredAnnosFromATypeElement(param.type, TypeUseLocation.PARAMETER);
            }
        }
    }
}
Also used : AClass(scenelib.annotations.el.AClass) AMethod(scenelib.annotations.el.AMethod) AField(scenelib.annotations.el.AField)

Example 10 with AMethod

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

the class ToIndexFileConverter method visit.

@Override
public Void visit(ConstructorDeclaration decl, AElement elem) {
    List<Parameter> params = decl.getParameters();
    List<AnnotationExpr> rcvrAnnos = decl.getAnnotations();
    BlockStmt body = decl.getBody();
    StringBuilder sb = new StringBuilder("<init>(");
    AClass clazz = (AClass) elem;
    AMethod method;
    // an empty list.
    if (params != null) {
        for (Parameter param : params) {
            Type ptype = param.getType();
            sb.append(getJVML(ptype));
        }
    }
    sb.append(")V");
    method = clazz.methods.vivify(sb.toString());
    visitDecl(decl, method);
    if (params != null) {
        for (int i = 0; i < params.size(); i++) {
            Parameter param = params.get(i);
            AField field = method.parameters.vivify(i);
            visitType(param.getType(), field.type);
        }
    }
    if (rcvrAnnos != null) {
        for (AnnotationExpr expr : rcvrAnnos) {
            Annotation anno = extractAnnotation(expr);
            method.receiver.tlAnnotationsHere.add(anno);
        }
    }
    return body == null ? null : body.accept(this, method);
// return super.visit(decl, elem);
}
Also used : ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) Type(com.github.javaparser.ast.type.Type) ReferenceType(com.github.javaparser.ast.type.ReferenceType) VoidType(com.github.javaparser.ast.type.VoidType) ArrayType(com.github.javaparser.ast.type.ArrayType) PrimitiveType(com.github.javaparser.ast.type.PrimitiveType) WildcardType(com.github.javaparser.ast.type.WildcardType) AnnotationExpr(com.github.javaparser.ast.expr.AnnotationExpr) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) ReceiverParameter(com.github.javaparser.ast.body.ReceiverParameter) Parameter(com.github.javaparser.ast.body.Parameter) TypeParameter(com.github.javaparser.ast.type.TypeParameter) AClass(scenelib.annotations.el.AClass) AMethod(scenelib.annotations.el.AMethod) Annotation(scenelib.annotations.Annotation) AField(scenelib.annotations.el.AField)

Aggregations

AMethod (scenelib.annotations.el.AMethod)10 AClass (scenelib.annotations.el.AClass)9 AField (scenelib.annotations.el.AField)7 ClassSymbol (com.sun.tools.javac.code.Symbol.ClassSymbol)5 AnnotatedTypeMirror (org.checkerframework.framework.type.AnnotatedTypeMirror)4 AnnotationExpr (com.github.javaparser.ast.expr.AnnotationExpr)3 Annotation (scenelib.annotations.Annotation)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 FieldAccessNode (org.checkerframework.dataflow.cfg.node.FieldAccessNode)2 ImplicitThisLiteralNode (org.checkerframework.dataflow.cfg.node.ImplicitThisLiteralNode)2