Search in sources :

Example 6 with AField

use of scenelib.annotations.el.AField 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 AField

use of scenelib.annotations.el.AField 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 AField

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

the class WholeProgramInferenceScenes method updateInferredFieldType.

/**
 * Updates the type of the field lhs in the Scene of the class with tree classTree. If the field
 * has a declaration annotation with the {@link IgnoreInWholeProgramInference} meta-annotation,
 * no type annotation will be inferred for that field.
 *
 * <p>If the Scene contains no entry for the field lhs, the entry will be created and its type
 * will be the type of rhs. If the Scene previously contained an entry/type for lhs, its new
 * type will be the LUB between the previous type and the type of rhs.
 *
 * <p>
 *
 * @param lhs the field whose type will be refined
 * @param rhs the expression being assigned to the field
 * @param classTree the ClassTree for the enclosing class of the assignment
 * @param atf the annotated type factory of a given type system, whose type hierarchy will be
 *     used to update the field's type
 */
@Override
public void updateInferredFieldType(FieldAccessNode lhs, Node rhs, ClassTree classTree, AnnotatedTypeFactory atf) {
    ClassSymbol classSymbol = getEnclosingClassSymbol(classTree, lhs);
    // https://github.com/typetools/checker-framework/issues/682
    if (classSymbol == null) {
        // TODO: Handle anonymous classes.
        return;
    }
    // https://github.com/typetools/checker-framework/issues/682
    if (!classSymbol.getEnclosedElements().contains((Symbol) lhs.getElement())) {
        return;
    }
    // @IgnoreInWholeProgramInference meta-annotation, exit this routine.
    for (AnnotationMirror declAnno : atf.getDeclAnnotations(TreeUtils.elementFromTree(lhs.getTree()))) {
        if (AnnotationUtils.areSameByClass(declAnno, IgnoreInWholeProgramInference.class)) {
            return;
        }
        Element elt = declAnno.getAnnotationType().asElement();
        if (elt.getAnnotation(IgnoreInWholeProgramInference.class) != null) {
            return;
        }
    }
    String className = classSymbol.flatname.toString();
    String jaifPath = helper.getJaifPath(className);
    AClass clazz = helper.getAClass(className, jaifPath);
    AField field = clazz.fields.vivify(lhs.getFieldName());
    AnnotatedTypeMirror lhsATM = atf.getAnnotatedType(lhs.getTree());
    AnnotatedTypeMirror rhsATM = atf.getAnnotatedType(rhs.getTree());
    helper.updateAnnotationSetInScene(field.type, atf, jaifPath, rhsATM, lhsATM, TypeUseLocation.FIELD);
}
Also used : AnnotationMirror(javax.lang.model.element.AnnotationMirror) 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) VariableElement(javax.lang.model.element.VariableElement) ExecutableElement(javax.lang.model.element.ExecutableElement) Element(javax.lang.model.element.Element) IgnoreInWholeProgramInference(org.checkerframework.framework.qual.IgnoreInWholeProgramInference) AClass(scenelib.annotations.el.AClass) AnnotatedTypeMirror(org.checkerframework.framework.type.AnnotatedTypeMirror) AField(scenelib.annotations.el.AField)

Example 9 with AField

use of scenelib.annotations.el.AField 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 AField

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

the class ToIndexFileConverter method visit.

@Override
public Void visit(EnumConstantDeclaration decl, AElement elem) {
    AField field = ((AClass) elem).fields.vivify(decl.getNameAsString());
    visitDecl(decl, field);
    return super.visit(decl, field);
}
Also used : AField(scenelib.annotations.el.AField)

Aggregations

AField (scenelib.annotations.el.AField)11 AClass (scenelib.annotations.el.AClass)8 AMethod (scenelib.annotations.el.AMethod)7 AnnotatedTypeMirror (org.checkerframework.framework.type.AnnotatedTypeMirror)5 ClassSymbol (com.sun.tools.javac.code.Symbol.ClassSymbol)4 AnnotationExpr (com.github.javaparser.ast.expr.AnnotationExpr)3 VariableElement (javax.lang.model.element.VariableElement)3 Annotation (scenelib.annotations.Annotation)3 Parameter (com.github.javaparser.ast.body.Parameter)2 ReceiverParameter (com.github.javaparser.ast.body.ReceiverParameter)2 VariableDeclarator (com.github.javaparser.ast.body.VariableDeclarator)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