Search in sources :

Example 11 with AnnotationDeclaration

use of com.github.javaparser.ast.body.AnnotationDeclaration in project checker-framework by typetools.

the class JointJavacJavaParserVisitor method visitClass.

@Override
public Void visitClass(ClassTree javacTree, Node javaParserNode) {
    if (javaParserNode instanceof ClassOrInterfaceDeclaration) {
        ClassOrInterfaceDeclaration node = (ClassOrInterfaceDeclaration) javaParserNode;
        processClass(javacTree, node);
        visitLists(javacTree.getTypeParameters(), node.getTypeParameters());
        if (javacTree.getKind() == Tree.Kind.CLASS) {
            if (javacTree.getExtendsClause() == null) {
                assert node.getExtendedTypes().isEmpty();
            } else {
                assert node.getExtendedTypes().size() == 1;
                javacTree.getExtendsClause().accept(this, node.getExtendedTypes().get(0));
            }
            visitLists(javacTree.getImplementsClause(), node.getImplementedTypes());
        } else if (javacTree.getKind() == Tree.Kind.INTERFACE) {
            visitLists(javacTree.getImplementsClause(), node.getExtendedTypes());
        }
        visitClassMembers(javacTree.getMembers(), node.getMembers());
    } else if (javaParserNode instanceof RecordDeclaration) {
        RecordDeclaration node = (RecordDeclaration) javaParserNode;
        processClass(javacTree, node);
        visitLists(javacTree.getTypeParameters(), node.getTypeParameters());
        visitLists(javacTree.getImplementsClause(), node.getImplementedTypes());
        List<? extends Tree> membersWithoutAutoGenerated = Lists.newArrayList(Iterables.filter(javacTree.getMembers(), (Predicate<Tree>) (Tree m) -> {
            // Filter out all auto-generated items:
            return !TreeUtils.isAutoGeneratedRecordMember(m);
        }));
        visitClassMembers(membersWithoutAutoGenerated, node.getMembers());
    } else if (javaParserNode instanceof AnnotationDeclaration) {
        AnnotationDeclaration node = (AnnotationDeclaration) javaParserNode;
        processClass(javacTree, node);
        visitClassMembers(javacTree.getMembers(), node.getMembers());
    } else if (javaParserNode instanceof LocalClassDeclarationStmt) {
        javacTree.accept(this, ((LocalClassDeclarationStmt) javaParserNode).getClassDeclaration());
    } else if (javaParserNode instanceof LocalRecordDeclarationStmt) {
        javacTree.accept(this, ((LocalRecordDeclarationStmt) javaParserNode).getRecordDeclaration());
    } else if (javaParserNode instanceof EnumDeclaration) {
        EnumDeclaration node = (EnumDeclaration) javaParserNode;
        processClass(javacTree, node);
        visitLists(javacTree.getImplementsClause(), node.getImplementedTypes());
        // members, whereas JavaParser stores them as one object.  Need to match them.
        assert javacTree.getKind() == Tree.Kind.ENUM;
        List<Tree> javacMembers = new ArrayList<>(javacTree.getMembers());
        // possibly a synthetic constructor.
        if (!node.getEntries().isEmpty()) {
            while (!javacMembers.isEmpty() && javacMembers.get(0).getKind() != Tree.Kind.VARIABLE) {
                javacMembers.remove(0);
            }
        }
        for (EnumConstantDeclaration entry : node.getEntries()) {
            assert !javacMembers.isEmpty();
            javacMembers.get(0).accept(this, entry);
            javacMembers.remove(0);
        }
        visitClassMembers(javacMembers, node.getMembers());
    } else {
        throwUnexpectedNodeType(javacTree, javaParserNode);
    }
    return null;
}
Also used : RecordDeclaration(com.github.javaparser.ast.body.RecordDeclaration) LocalClassDeclarationStmt(com.github.javaparser.ast.stmt.LocalClassDeclarationStmt) EnumConstantDeclaration(com.github.javaparser.ast.body.EnumConstantDeclaration) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) AnnotationDeclaration(com.github.javaparser.ast.body.AnnotationDeclaration) CompoundAssignmentTree(com.sun.source.tree.CompoundAssignmentTree) LiteralTree(com.sun.source.tree.LiteralTree) ExportsTree(com.sun.source.tree.ExportsTree) MethodInvocationTree(com.sun.source.tree.MethodInvocationTree) AssignmentTree(com.sun.source.tree.AssignmentTree) TypeCastTree(com.sun.source.tree.TypeCastTree) LambdaExpressionTree(com.sun.source.tree.LambdaExpressionTree) ForLoopTree(com.sun.source.tree.ForLoopTree) InstanceOfTree(com.sun.source.tree.InstanceOfTree) ModuleTree(com.sun.source.tree.ModuleTree) ParenthesizedTree(com.sun.source.tree.ParenthesizedTree) ConditionalExpressionTree(com.sun.source.tree.ConditionalExpressionTree) OpensTree(com.sun.source.tree.OpensTree) MemberSelectTree(com.sun.source.tree.MemberSelectTree) ExpressionStatementTree(com.sun.source.tree.ExpressionStatementTree) ThrowTree(com.sun.source.tree.ThrowTree) BlockTree(com.sun.source.tree.BlockTree) EnhancedForLoopTree(com.sun.source.tree.EnhancedForLoopTree) ReturnTree(com.sun.source.tree.ReturnTree) ArrayTypeTree(com.sun.source.tree.ArrayTypeTree) LabeledStatementTree(com.sun.source.tree.LabeledStatementTree) UnaryTree(com.sun.source.tree.UnaryTree) VariableTree(com.sun.source.tree.VariableTree) TypeParameterTree(com.sun.source.tree.TypeParameterTree) NewClassTree(com.sun.source.tree.NewClassTree) ParameterizedTypeTree(com.sun.source.tree.ParameterizedTypeTree) BreakTree(com.sun.source.tree.BreakTree) ImportTree(com.sun.source.tree.ImportTree) Tree(com.sun.source.tree.Tree) ExpressionTree(com.sun.source.tree.ExpressionTree) IntersectionTypeTree(com.sun.source.tree.IntersectionTypeTree) WildcardTree(com.sun.source.tree.WildcardTree) RequiresTree(com.sun.source.tree.RequiresTree) UnionTypeTree(com.sun.source.tree.UnionTypeTree) ArrayAccessTree(com.sun.source.tree.ArrayAccessTree) AnnotatedTypeTree(com.sun.source.tree.AnnotatedTypeTree) PackageTree(com.sun.source.tree.PackageTree) IdentifierTree(com.sun.source.tree.IdentifierTree) CatchTree(com.sun.source.tree.CatchTree) NewArrayTree(com.sun.source.tree.NewArrayTree) ContinueTree(com.sun.source.tree.ContinueTree) UsesTree(com.sun.source.tree.UsesTree) CaseTree(com.sun.source.tree.CaseTree) CompilationUnitTree(com.sun.source.tree.CompilationUnitTree) SwitchTree(com.sun.source.tree.SwitchTree) PrimitiveTypeTree(com.sun.source.tree.PrimitiveTypeTree) SynchronizedTree(com.sun.source.tree.SynchronizedTree) AssertTree(com.sun.source.tree.AssertTree) StatementTree(com.sun.source.tree.StatementTree) ModifiersTree(com.sun.source.tree.ModifiersTree) WhileLoopTree(com.sun.source.tree.WhileLoopTree) AnnotationTree(com.sun.source.tree.AnnotationTree) MethodTree(com.sun.source.tree.MethodTree) BinaryTree(com.sun.source.tree.BinaryTree) EmptyStatementTree(com.sun.source.tree.EmptyStatementTree) ClassTree(com.sun.source.tree.ClassTree) IfTree(com.sun.source.tree.IfTree) ProvidesTree(com.sun.source.tree.ProvidesTree) MemberReferenceTree(com.sun.source.tree.MemberReferenceTree) ErroneousTree(com.sun.source.tree.ErroneousTree) DoWhileLoopTree(com.sun.source.tree.DoWhileLoopTree) TryTree(com.sun.source.tree.TryTree) ArrayList(java.util.ArrayList) List(java.util.List) LocalRecordDeclarationStmt(com.github.javaparser.ast.stmt.LocalRecordDeclarationStmt) EnumDeclaration(com.github.javaparser.ast.body.EnumDeclaration)

Example 12 with AnnotationDeclaration

use of com.github.javaparser.ast.body.AnnotationDeclaration in project checker-framework by typetools.

the class AnnotationFileParser method processTypeDecl.

/**
 * Process a type declaration: copy its annotations to {@code #annotationFileAnnos}.
 *
 * <p>This method stores the declaration's type parameters in {@link #typeParameters}. When
 * processing an ajava file, where traversal is handled externaly by a {@link
 * org.checkerframework.framework.ajava.JointJavacJavaParserVisitor}, these type variables must be
 * removed after processing the type's members. Otherwise, this method removes them.
 *
 * @param typeDecl the type declaration to process
 * @param outertypeName the name of the containing class, when processing a nested class;
 *     otherwise null
 * @param classTree the tree corresponding to typeDecl if processing an ajava file, null otherwise
 * @return a list of types variables for {@code typeDecl}. Only non-null if processing an ajava
 *     file, in which case the contents should be removed from {@link #typeParameters} after
 *     processing the type declaration's members
 */
private List<AnnotatedTypeVariable> processTypeDecl(TypeDeclaration<?> typeDecl, String outertypeName, @Nullable ClassTree classTree) {
    assert typeBeingParsed != null;
    if (skipNode(typeDecl)) {
        return null;
    }
    String innerName;
    @FullyQualifiedName String fqTypeName;
    TypeElement typeElt;
    if (classTree != null) {
        typeElt = TreeUtils.elementFromDeclaration(classTree);
        innerName = typeElt.getQualifiedName().toString();
        typeBeingParsed = new FqName(typeBeingParsed.packageName, innerName);
        fqTypeName = typeBeingParsed.toString();
    } else {
        String packagePrefix = outertypeName == null ? "" : outertypeName + ".";
        innerName = packagePrefix + typeDecl.getNameAsString();
        typeBeingParsed = new FqName(typeBeingParsed.packageName, innerName);
        fqTypeName = typeBeingParsed.toString();
        typeElt = elements.getTypeElement(fqTypeName);
    }
    if (!isAnnotatedForThisChecker(typeDecl.getAnnotations())) {
        return null;
    }
    if (typeElt == null) {
        if (debugAnnotationFileParser || (!warnIfNotFoundIgnoresClasses && !hasNoAnnotationFileParserWarning(typeDecl.getAnnotations()) && !hasNoAnnotationFileParserWarning(packageAnnos))) {
            if (elements.getAllTypeElements(fqTypeName).isEmpty()) {
                stubWarnNotFound(typeDecl, "Type not found: " + fqTypeName);
            } else {
                stubWarnNotFound(typeDecl, "Type not found uniquely: " + fqTypeName + " : " + elements.getAllTypeElements(fqTypeName));
            }
        }
        return null;
    }
    List<AnnotatedTypeVariable> typeDeclTypeParameters = null;
    if (typeElt.getKind() == ElementKind.ENUM) {
        if (!(typeDecl instanceof EnumDeclaration)) {
            warn(typeDecl, innerName + " is an enum, but stub file declared it as " + typeDecl.toString().split("\\R", 2)[0] + "...");
            return null;
        }
        typeDeclTypeParameters = processEnum((EnumDeclaration) typeDecl, typeElt);
        typeParameters.addAll(typeDeclTypeParameters);
    } else if (typeElt.getKind() == ElementKind.ANNOTATION_TYPE) {
        if (!(typeDecl instanceof AnnotationDeclaration)) {
            warn(typeDecl, innerName + " is an annotation, but stub file declared it as " + typeDecl.toString().split("\\R", 2)[0] + "...");
            return null;
        }
        stubWarnNotFound(typeDecl, "Skipping annotation type: " + fqTypeName);
    } else if (typeDecl instanceof ClassOrInterfaceDeclaration) {
        if (!(typeDecl instanceof ClassOrInterfaceDeclaration)) {
            warn(typeDecl, innerName + " is a class or interface, but stub file declared it as " + typeDecl.toString().split("\\R", 2)[0] + "...");
            return null;
        }
        typeDeclTypeParameters = processType(typeDecl, typeElt);
        typeParameters.addAll(typeDeclTypeParameters);
    } else if (typeDecl instanceof RecordDeclaration) {
        typeDeclTypeParameters = processType(typeDecl, typeElt);
        typeParameters.addAll(typeDeclTypeParameters);
    }
    // of this method.
    if (fileType == AnnotationFileType.AJAVA) {
        return typeDeclTypeParameters;
    }
    if (typeDecl instanceof RecordDeclaration) {
        NodeList<Parameter> recordMembers = ((RecordDeclaration) typeDecl).getParameters();
        LinkedHashMap<String, RecordComponentStub> byName = new LinkedHashMap<>();
        for (Parameter recordMember : recordMembers) {
            RecordComponentStub stub = processRecordField(recordMember, findFieldElement(typeElt, recordMember.getNameAsString(), recordMember));
            byName.put(recordMember.getNameAsString(), stub);
        }
        annotationFileAnnos.records.put(typeDecl.getFullyQualifiedName().get(), new RecordStub(byName));
    }
    Pair<Map<Element, BodyDeclaration<?>>, Map<Element, List<BodyDeclaration<?>>>> members = getMembers(typeDecl, typeElt, typeDecl);
    for (Map.Entry<Element, BodyDeclaration<?>> entry : members.first.entrySet()) {
        final Element elt = entry.getKey();
        final BodyDeclaration<?> decl = entry.getValue();
        switch(elt.getKind()) {
            case FIELD:
                processField((FieldDeclaration) decl, (VariableElement) elt);
                break;
            case ENUM_CONSTANT:
                // the TRACKER enum constant annotated with DefaultType:
                if (decl instanceof FieldDeclaration) {
                    processField((FieldDeclaration) decl, (VariableElement) elt);
                } else if (decl instanceof EnumConstantDeclaration) {
                    processEnumConstant((EnumConstantDeclaration) decl, (VariableElement) elt);
                } else {
                    throw new BugInCF("Unexpected decl type " + decl.getClass() + " for ENUM_CONSTANT kind, original: " + decl);
                }
                break;
            case CONSTRUCTOR:
            case METHOD:
                processCallableDeclaration((CallableDeclaration<?>) decl, (ExecutableElement) elt);
                break;
            case CLASS:
            case INTERFACE:
                // Not processing an ajava file, so ignore the return value.
                processTypeDecl((ClassOrInterfaceDeclaration) decl, innerName, null);
                break;
            case ENUM:
                // Not processing an ajava file, so ignore the return value.
                processTypeDecl((EnumDeclaration) decl, innerName, null);
                break;
            default:
                /* do nothing */
                stubWarnNotFound(decl, "AnnotationFileParser ignoring: " + elt);
                break;
        }
    }
    for (Map.Entry<Element, List<BodyDeclaration<?>>> entry : members.second.entrySet()) {
        ExecutableElement fakeOverridden = (ExecutableElement) entry.getKey();
        List<BodyDeclaration<?>> fakeOverrideDecls = entry.getValue();
        for (BodyDeclaration<?> bodyDecl : fakeOverrideDecls) {
            processFakeOverride(fakeOverridden, (CallableDeclaration<?>) bodyDecl, typeElt);
        }
    }
    if (typeDeclTypeParameters != null) {
        typeParameters.removeAll(typeDeclTypeParameters);
    }
    return null;
}
Also used : ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) TypeElement(javax.lang.model.element.TypeElement) Element(javax.lang.model.element.Element) PackageElement(javax.lang.model.element.PackageElement) VariableElement(javax.lang.model.element.VariableElement) ExecutableElement(javax.lang.model.element.ExecutableElement) ExecutableElement(javax.lang.model.element.ExecutableElement) FullyQualifiedName(org.checkerframework.checker.signature.qual.FullyQualifiedName) VariableElement(javax.lang.model.element.VariableElement) FieldDeclaration(com.github.javaparser.ast.body.FieldDeclaration) LinkedHashMap(java.util.LinkedHashMap) EnumConstantDeclaration(com.github.javaparser.ast.body.EnumConstantDeclaration) ArrayList(java.util.ArrayList) NodeList(com.github.javaparser.ast.NodeList) List(java.util.List) TypeElement(javax.lang.model.element.TypeElement) BugInCF(org.checkerframework.javacutil.BugInCF) EnumDeclaration(com.github.javaparser.ast.body.EnumDeclaration) RecordDeclaration(com.github.javaparser.ast.body.RecordDeclaration) AnnotationDeclaration(com.github.javaparser.ast.body.AnnotationDeclaration) ReceiverParameter(com.github.javaparser.ast.body.ReceiverParameter) Parameter(com.github.javaparser.ast.body.Parameter) TypeParameter(com.github.javaparser.ast.type.TypeParameter) BodyDeclaration(com.github.javaparser.ast.body.BodyDeclaration) AnnotatedTypeVariable(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedTypeVariable) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap)

Example 13 with AnnotationDeclaration

use of com.github.javaparser.ast.body.AnnotationDeclaration in project checker-framework by typetools.

the class DoubleJavaParserVisitor method visit.

@Override
public void visit(final AnnotationDeclaration node1, final Node other) {
    AnnotationDeclaration node2 = (AnnotationDeclaration) other;
    defaultAction(node1, node2);
    visitLists(node1.getMembers(), node2.getMembers());
    visitLists(node1.getModifiers(), node2.getModifiers());
    node1.getName().accept(this, node2.getName());
}
Also used : AnnotationDeclaration(com.github.javaparser.ast.body.AnnotationDeclaration)

Aggregations

AnnotationDeclaration (com.github.javaparser.ast.body.AnnotationDeclaration)13 Test (org.junit.Test)6 CsmElement (com.github.javaparser.printer.concretesyntaxmodel.CsmElement)5 CsmToken (com.github.javaparser.printer.concretesyntaxmodel.CsmToken)5 ClassOrInterfaceDeclaration (com.github.javaparser.ast.body.ClassOrInterfaceDeclaration)4 EnumDeclaration (com.github.javaparser.ast.body.EnumDeclaration)4 EnumConstantDeclaration (com.github.javaparser.ast.body.EnumConstantDeclaration)3 BodyDeclaration (com.github.javaparser.ast.body.BodyDeclaration)2 FieldDeclaration (com.github.javaparser.ast.body.FieldDeclaration)2 RecordDeclaration (com.github.javaparser.ast.body.RecordDeclaration)2 JavadocComment (com.github.javaparser.ast.comments.JavadocComment)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 ImportDeclaration (com.github.javaparser.ast.ImportDeclaration)1 Modifier (com.github.javaparser.ast.Modifier)1 NodeList (com.github.javaparser.ast.NodeList)1 ConstructorDeclaration (com.github.javaparser.ast.body.ConstructorDeclaration)1 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)1 Parameter (com.github.javaparser.ast.body.Parameter)1 ReceiverParameter (com.github.javaparser.ast.body.ReceiverParameter)1