Search in sources :

Example 11 with AnnotationExpr

use of com.github.javaparser.ast.expr.AnnotationExpr in project checker-framework by typetools.

the class StubParser method annotate.

/**
 * Add to {@code atype}:
 *
 * <ol>
 *   <li>the annotations from {@code typeDef}, and
 *   <li>any type annotations that parsed as declaration annotations (ie those in {@code
 *       declAnnos}).
 * </ol>
 *
 * @param atype annotated type to which to add annotations
 * @param typeDef parsed type
 * @param declAnnos annotations stored on the declaration of the variable with this type, or
 *     null
 */
private void annotate(AnnotatedTypeMirror atype, Type typeDef, NodeList<AnnotationExpr> declAnnos) {
    if (atype.getKind() == TypeKind.ARRAY) {
        annotateAsArray((AnnotatedArrayType) atype, (ReferenceType) typeDef, declAnnos);
        return;
    }
    clearAnnotations(atype, typeDef);
    // Primary annotations for the type of a variable declaration are not stored in typeDef, but
    // rather as declaration annotations (passed as declAnnos to this method).  But, if typeDef
    // is not the type of a variable, then the primary annotations are stored in typeDef.
    NodeList<AnnotationExpr> primaryAnnotations;
    if (typeDef.getAnnotations().isEmpty() && declAnnos != null) {
        primaryAnnotations = declAnnos;
    } else {
        primaryAnnotations = typeDef.getAnnotations();
    }
    if (atype.getKind() != TypeKind.WILDCARD) {
        // The primary annotation on a wildcard applies to the super or extends bound and
        // are added below.
        annotate(atype, primaryAnnotations);
    }
    switch(atype.getKind()) {
        case DECLARED:
            ClassOrInterfaceType declType = unwrapDeclaredType(typeDef);
            if (declType == null) {
                break;
            }
            AnnotatedDeclaredType adeclType = (AnnotatedDeclaredType) atype;
            if (declType.getTypeArguments().isPresent() && !declType.getTypeArguments().get().isEmpty() && !adeclType.getTypeArguments().isEmpty()) {
                assert declType.getTypeArguments().get().size() == adeclType.getTypeArguments().size() : String.format("Mismatch in type argument size between %s (%d) and %s (%d)", declType, declType.getTypeArguments().get().size(), adeclType, adeclType.getTypeArguments().size());
                for (int i = 0; i < declType.getTypeArguments().get().size(); ++i) {
                    annotate(adeclType.getTypeArguments().get(i), declType.getTypeArguments().get().get(i), null);
                }
            }
            break;
        case WILDCARD:
            AnnotatedWildcardType wildcardType = (AnnotatedWildcardType) atype;
            WildcardType wildcardDef = (WildcardType) typeDef;
            if (wildcardDef.getExtendedType().isPresent()) {
                annotate(wildcardType.getExtendsBound(), wildcardDef.getExtendedType().get(), null);
                annotate(wildcardType.getSuperBound(), primaryAnnotations);
            } else if (wildcardDef.getSuperType().isPresent()) {
                annotate(wildcardType.getSuperBound(), wildcardDef.getSuperType().get(), null);
                annotate(wildcardType.getExtendsBound(), primaryAnnotations);
            } else {
                annotate(atype, primaryAnnotations);
            }
            break;
        case TYPEVAR:
            // Add annotations from the declaration of the TypeVariable
            AnnotatedTypeVariable typeVarUse = (AnnotatedTypeVariable) atype;
            for (AnnotatedTypeVariable typePar : typeParameters) {
                if (typePar.getUnderlyingType() == atype.getUnderlyingType()) {
                    AnnotatedTypeMerger.merge(typePar.getUpperBound(), typeVarUse.getUpperBound());
                    AnnotatedTypeMerger.merge(typePar.getLowerBound(), typeVarUse.getLowerBound());
                }
            }
            break;
        default:
    }
}
Also used : AnnotatedWildcardType(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedWildcardType) WildcardType(com.github.javaparser.ast.type.WildcardType) AnnotationExpr(com.github.javaparser.ast.expr.AnnotationExpr) MarkerAnnotationExpr(com.github.javaparser.ast.expr.MarkerAnnotationExpr) SingleMemberAnnotationExpr(com.github.javaparser.ast.expr.SingleMemberAnnotationExpr) NormalAnnotationExpr(com.github.javaparser.ast.expr.NormalAnnotationExpr) AnnotatedDeclaredType(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedDeclaredType) AnnotatedWildcardType(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedWildcardType) ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) AnnotatedTypeVariable(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedTypeVariable)

Example 12 with AnnotationExpr

use of com.github.javaparser.ast.expr.AnnotationExpr in project checker-framework by typetools.

the class StubParser method processCompilationUnit.

private void processCompilationUnit(CompilationUnit cu) {
    final String packageName;
    final List<AnnotationExpr> packageAnnos;
    if (!cu.getPackageDeclaration().isPresent()) {
        packageName = null;
        packageAnnos = null;
        parseState = new FqName(null, null);
    } else {
        PackageDeclaration pDecl = cu.getPackageDeclaration().get();
        packageName = pDecl.getNameAsString();
        packageAnnos = pDecl.getAnnotations();
        processPackage(pDecl);
    }
    if (cu.getTypes() != null) {
        for (TypeDeclaration<?> typeDeclaration : cu.getTypes()) {
            processTypeDecl(typeDeclaration, null, packageAnnos);
        }
    }
}
Also used : AnnotationExpr(com.github.javaparser.ast.expr.AnnotationExpr) MarkerAnnotationExpr(com.github.javaparser.ast.expr.MarkerAnnotationExpr) SingleMemberAnnotationExpr(com.github.javaparser.ast.expr.SingleMemberAnnotationExpr) NormalAnnotationExpr(com.github.javaparser.ast.expr.NormalAnnotationExpr) PackageDeclaration(com.github.javaparser.ast.PackageDeclaration)

Example 13 with AnnotationExpr

use of com.github.javaparser.ast.expr.AnnotationExpr in project checker-framework by typetools.

the class ToIndexFileConverter method visitType.

/**
 * Copies information from an AST type node to an {@link ATypeElement}.
 */
private Void visitType(Type type, final ATypeElement elem) {
    List<AnnotationExpr> exprs = type.getAnnotations();
    if (exprs != null) {
        for (AnnotationExpr expr : exprs) {
            Annotation anno = extractAnnotation(expr);
            if (anno != null) {
                elem.tlAnnotationsHere.add(anno);
            }
        }
    }
    visitInnerTypes(type, elem);
    return null;
}
Also used : AnnotationExpr(com.github.javaparser.ast.expr.AnnotationExpr) Annotation(scenelib.annotations.Annotation)

Example 14 with AnnotationExpr

use of com.github.javaparser.ast.expr.AnnotationExpr in project checker-framework by typetools.

the class ToIndexFileConverter method visitDecl.

/**
 * Copies information from an AST declaration node to an {@link ADeclaration}. Called by
 * visitors for BodyDeclaration subclasses.
 */
private Void visitDecl(BodyDeclaration<?> decl, ADeclaration elem) {
    NodeList<AnnotationExpr> annoExprs = decl.getAnnotations();
    if (annoExprs != null) {
        for (AnnotationExpr annoExpr : annoExprs) {
            Annotation anno = extractAnnotation(annoExpr);
            elem.tlAnnotationsHere.add(anno);
        }
    }
    return null;
}
Also used : AnnotationExpr(com.github.javaparser.ast.expr.AnnotationExpr) Annotation(scenelib.annotations.Annotation)

Example 15 with AnnotationExpr

use of com.github.javaparser.ast.expr.AnnotationExpr 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

AnnotationExpr (com.github.javaparser.ast.expr.AnnotationExpr)19 SingleMemberAnnotationExpr (com.github.javaparser.ast.expr.SingleMemberAnnotationExpr)11 MarkerAnnotationExpr (com.github.javaparser.ast.expr.MarkerAnnotationExpr)9 NormalAnnotationExpr (com.github.javaparser.ast.expr.NormalAnnotationExpr)9 Comment (com.github.javaparser.ast.comments.Comment)5 JavadocComment (com.github.javaparser.ast.comments.JavadocComment)5 Annotation (scenelib.annotations.Annotation)5 BlockComment (com.github.javaparser.ast.comments.BlockComment)4 LineComment (com.github.javaparser.ast.comments.LineComment)4 ClassOrInterfaceType (com.github.javaparser.ast.type.ClassOrInterfaceType)4 WildcardType (com.github.javaparser.ast.type.WildcardType)4 ReferenceType (com.github.javaparser.ast.type.ReferenceType)3 Type (com.github.javaparser.ast.type.Type)3 AField (scenelib.annotations.el.AField)3 AMethod (scenelib.annotations.el.AMethod)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