Search in sources :

Example 6 with EnumConstantDeclaration

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

the class AnnotationFileParser method putNewElement.

// Used only by getMembers().
/**
 * If {@code typeElt} contains an element for {@code member}, adds to {@code elementsToDecl} a
 * mapping from member's element to member. Does nothing if a mapping already exists.
 *
 * <p>Otherwise (if there is no element for {@code member}), adds to {@code fakeOverrideDecls}
 * zero or more mappings. Each mapping is from an element that {@code member} would override to
 * {@code member}.
 *
 * <p>This method does not read or write field {@link annotationFileAnnos}.
 *
 * @param elementsToDecl the mapping that is side-effected by this method
 * @param fakeOverrideDecls fake overrides, also side-effected by this method
 * @param typeElt the class in which {@code member} is declared
 * @param member the stub file declaration of a method
 * @param typeDeclName used only for debugging
 * @param astNode where to report errors
 */
private void putNewElement(Map<Element, BodyDeclaration<?>> elementsToDecl, Map<Element, List<BodyDeclaration<?>>> fakeOverrideDecls, TypeElement typeElt, BodyDeclaration<?> member, String typeDeclName, NodeWithRange<?> astNode) {
    if (member instanceof MethodDeclaration) {
        MethodDeclaration method = (MethodDeclaration) member;
        Element elt = findElement(typeElt, method, /*noWarn=*/
        true);
        if (elt != null) {
            putIfAbsent(elementsToDecl, elt, method);
        } else {
            ExecutableElement overriddenMethod = fakeOverriddenMethod(typeElt, method);
            if (overriddenMethod == null) {
                // Didn't find the element and it isn't a fake override.  Issue a warning.
                findElement(typeElt, method, /*noWarn=*/
                false);
            } else {
                List<BodyDeclaration<?>> l = fakeOverrideDecls.computeIfAbsent(overriddenMethod, __ -> new ArrayList<>());
                l.add(member);
            }
        }
    } else if (member instanceof ConstructorDeclaration) {
        Element elt = findElement(typeElt, (ConstructorDeclaration) member);
        if (elt != null) {
            putIfAbsent(elementsToDecl, elt, member);
        }
    } else if (member instanceof FieldDeclaration) {
        FieldDeclaration fieldDecl = (FieldDeclaration) member;
        for (VariableDeclarator var : fieldDecl.getVariables()) {
            Element varelt = findElement(typeElt, var);
            if (varelt != null) {
                putIfAbsent(elementsToDecl, varelt, fieldDecl);
            }
        }
    } else if (member instanceof EnumConstantDeclaration) {
        Element elt = findElement(typeElt, (EnumConstantDeclaration) member, astNode);
        if (elt != null) {
            putIfAbsent(elementsToDecl, elt, member);
        }
    } else if (member instanceof ClassOrInterfaceDeclaration) {
        Element elt = findElement(typeElt, (ClassOrInterfaceDeclaration) member);
        if (elt != null) {
            putIfAbsent(elementsToDecl, elt, member);
        }
    } else if (member instanceof EnumDeclaration) {
        Element elt = findElement(typeElt, (EnumDeclaration) member);
        if (elt != null) {
            putIfAbsent(elementsToDecl, elt, member);
        }
    } else {
        stubDebug(String.format("Ignoring element of type %s in %s", member.getClass(), typeDeclName));
    }
}
Also used : EnumConstantDeclaration(com.github.javaparser.ast.body.EnumConstantDeclaration) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) ConstructorDeclaration(com.github.javaparser.ast.body.ConstructorDeclaration) 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) BodyDeclaration(com.github.javaparser.ast.body.BodyDeclaration) FieldDeclaration(com.github.javaparser.ast.body.FieldDeclaration) VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator) EnumDeclaration(com.github.javaparser.ast.body.EnumDeclaration)

Example 7 with EnumConstantDeclaration

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

the class JointJavacJavaParserVisitor method visitVariable.

@Override
public Void visitVariable(VariableTree javacTree, Node javaParserNode) {
    // variable declarations, parameters, and fields.
    if (javaParserNode instanceof VariableDeclarator) {
        // JavaParser uses VariableDeclarator as parts of other declaration types like
        // VariableDeclarationExpr when multiple variables may be declared.
        VariableDeclarator node = (VariableDeclarator) javaParserNode;
        processVariable(javacTree, node);
        // Don't process the variable type when it's the Java keyword "var".
        if (!node.getType().isVarType() && (!node.getType().isClassOrInterfaceType() || !node.getType().asClassOrInterfaceType().getName().asString().equals("var"))) {
            javacTree.getType().accept(this, node.getType());
        }
        // The name expression can be null, even when a name exists.
        if (javacTree.getNameExpression() != null) {
            javacTree.getNameExpression().accept(this, node.getName());
        }
        visitOptional(javacTree.getInitializer(), node.getInitializer());
    } else if (javaParserNode instanceof Parameter) {
        Parameter node = (Parameter) javaParserNode;
        processVariable(javacTree, node);
        if (node.isVarArgs()) {
            ArrayTypeTree arrayType;
            // AnnotatedType depending on whether it has an annotation.
            if (javacTree.getType().getKind() == Tree.Kind.ARRAY_TYPE) {
                arrayType = (ArrayTypeTree) javacTree.getType();
            } else {
                AnnotatedTypeTree annotatedType = (AnnotatedTypeTree) javacTree.getType();
                arrayType = (ArrayTypeTree) annotatedType.getUnderlyingType();
            }
            arrayType.getType().accept(this, node.getType());
        } else {
            // don't process them.
            if (!node.getType().isUnknownType()) {
                javacTree.getType().accept(this, node.getType());
            }
        }
        // The name expression can be null, even when a name exists.
        if (javacTree.getNameExpression() != null) {
            javacTree.getNameExpression().accept(this, node.getName());
        }
        assert javacTree.getInitializer() == null;
    } else if (javaParserNode instanceof ReceiverParameter) {
        ReceiverParameter node = (ReceiverParameter) javaParserNode;
        processVariable(javacTree, node);
        javacTree.getType().accept(this, node.getType());
        // The name expression can be null, even when a name exists.
        if (javacTree.getNameExpression() != null) {
            javacTree.getNameExpression().accept(this, node.getName());
        }
        assert javacTree.getInitializer() == null;
    } else if (javaParserNode instanceof EnumConstantDeclaration) {
        // In javac, an enum constant is expanded as a variable declaration initialized to a
        // constuctor call.
        EnumConstantDeclaration node = (EnumConstantDeclaration) javaParserNode;
        processVariable(javacTree, node);
        if (javacTree.getNameExpression() != null) {
            javacTree.getNameExpression().accept(this, node.getName());
        }
        assert javacTree.getInitializer().getKind() == Tree.Kind.NEW_CLASS;
        NewClassTree constructor = (NewClassTree) javacTree.getInitializer();
        visitLists(constructor.getArguments(), node.getArguments());
        if (constructor.getClassBody() != null) {
            visitAnonymousClassBody(constructor.getClassBody(), node.getClassBody());
        } else {
            assert node.getClassBody().isEmpty();
        }
    } else {
        throwUnexpectedNodeType(javacTree, javaParserNode);
    }
    return null;
}
Also used : AnnotatedTypeTree(com.sun.source.tree.AnnotatedTypeTree) EnumConstantDeclaration(com.github.javaparser.ast.body.EnumConstantDeclaration) 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) NewClassTree(com.sun.source.tree.NewClassTree) VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator) ArrayTypeTree(com.sun.source.tree.ArrayTypeTree)

Example 8 with EnumConstantDeclaration

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

the class DoubleJavaParserVisitor method visit.

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

Example 9 with EnumConstantDeclaration

use of com.github.javaparser.ast.body.EnumConstantDeclaration in project gradle by gradle.

the class SinceAnnotationMissingRule method maybeViolation.

@Override
public Violation maybeViolation(final JApiCompatibility member) {
    String className = null;
    GenericVisitorAdapter<Object, Void> visitor = null;
    if (member instanceof JApiMethod && !isOverride((JApiMethod) member)) {
        final JApiMethod method = (JApiMethod) member;
        if (isDeprecated(method)) {
            return null;
        }
        className = method.getjApiClass().getFullyQualifiedName();
        visitor = new GenericVisitorAdapter<Object, Void>() {

            @Override
            public Object visit(ClassOrInterfaceDeclaration classDeclaration, Void arg) {
                if (matchesNameAndContainsAnnotation(classDeclaration.getName(), toSimpleName(method.getjApiClass().getFullyQualifiedName()), classDeclaration)) {
                    return new Object();
                }
                return super.visit(classDeclaration, arg);
            }

            @Override
            public Object visit(AnnotationDeclaration annotationDeclaration, Void arg) {
                if (matchesNameAndContainsAnnotation(annotationDeclaration.getName(), toSimpleName(method.getjApiClass().getFullyQualifiedName()), annotationDeclaration)) {
                    return new Object();
                }
                return super.visit(annotationDeclaration, arg);
            }

            @Override
            public Object visit(EnumDeclaration enumDeclaration, Void arg) {
                if (matchesNameAndContainsAnnotation(enumDeclaration.getName(), toSimpleName(method.getjApiClass().getFullyQualifiedName()), enumDeclaration)) {
                    return new Object();
                }
                return super.visit(enumDeclaration, arg);
            }

            @Override
            public Object visit(MethodDeclaration methodDeclaration, Void arg) {
                if (matchesNameAndContainsAnnotation(methodDeclaration.getName(), method.getName(), methodDeclaration)) {
                    return new Object();
                }
                return null;
            }
        };
    } else if (member instanceof JApiField) {
        final JApiField field = (JApiField) member;
        if (isDeprecated(field)) {
            return null;
        }
        className = field.getjApiClass().getFullyQualifiedName();
        visitor = new GenericVisitorAdapter<Object, Void>() {

            @Override
            public Object visit(ClassOrInterfaceDeclaration classDeclaration, Void arg) {
                if (matchesNameAndContainsAnnotation(classDeclaration.getName(), toSimpleName(field.getjApiClass().getFullyQualifiedName()), classDeclaration)) {
                    return new Object();
                }
                return super.visit(classDeclaration, arg);
            }

            @Override
            public Object visit(AnnotationDeclaration annotationDeclaration, Void arg) {
                if (matchesNameAndContainsAnnotation(annotationDeclaration.getName(), toSimpleName(field.getjApiClass().getFullyQualifiedName()), annotationDeclaration)) {
                    return new Object();
                }
                return super.visit(annotationDeclaration, arg);
            }

            @Override
            public Object visit(EnumDeclaration enumDeclaration, Void arg) {
                if (matchesNameAndContainsAnnotation(enumDeclaration.getName(), toSimpleName(field.getjApiClass().getFullyQualifiedName()), enumDeclaration)) {
                    return new Object();
                }
                return super.visit(enumDeclaration, arg);
            }

            @Override
            public Object visit(FieldDeclaration fieldDeclaration, Void arg) {
                if (matchesNameAndContainsAnnotation(fieldDeclaration.getVariables().get(0).getId().getName(), field.getName(), fieldDeclaration)) {
                    return new Object();
                }
                return null;
            }

            @Override
            public Object visit(EnumConstantDeclaration enumConstantDeclaration, Void arg) {
                if (matchesNameAndContainsAnnotation(enumConstantDeclaration.getName(), field.getName(), enumConstantDeclaration)) {
                    return new Object();
                }
                return null;
            }
        };
    } else if (member instanceof JApiConstructor) {
        final JApiConstructor constructor = (JApiConstructor) member;
        if (isDeprecated(constructor) || isInject(constructor)) {
            return null;
        }
        className = constructor.getjApiClass().getFullyQualifiedName();
        visitor = new GenericVisitorAdapter<Object, Void>() {

            @Override
            public Object visit(ConstructorDeclaration constructorDeclaration, Void arg) {
                if (matchesNameAndContainsAnnotation(constructorDeclaration.getName(), toSimpleName(constructor.getjApiClass().getFullyQualifiedName()), constructorDeclaration)) {
                    return new Object();
                }
                return super.visit(constructorDeclaration, arg);
            }

            @Override
            public Object visit(ClassOrInterfaceDeclaration classDeclaration, Void arg) {
                if (matchesNameAndContainsAnnotation(classDeclaration.getName(), toSimpleName(constructor.getjApiClass().getFullyQualifiedName()), classDeclaration)) {
                    return new Object();
                }
                return super.visit(classDeclaration, arg);
            }

            @Override
            public Object visit(AnnotationDeclaration annotationDeclaration, Void arg) {
                if (matchesNameAndContainsAnnotation(annotationDeclaration.getName(), toSimpleName(constructor.getjApiClass().getFullyQualifiedName()), annotationDeclaration)) {
                    return new Object();
                }
                return super.visit(annotationDeclaration, arg);
            }

            @Override
            public Object visit(EnumDeclaration enumDeclaration, Void arg) {
                if (matchesNameAndContainsAnnotation(enumDeclaration.getName(), toSimpleName(constructor.getjApiClass().getFullyQualifiedName()), enumDeclaration)) {
                    return new Object();
                }
                return super.visit(enumDeclaration, arg);
            }

            @Override
            public Object visit(FieldDeclaration fieldDeclaration, Void arg) {
                if (matchesNameAndContainsAnnotation(fieldDeclaration.getVariables().get(0).getId().getName(), constructor.getName(), fieldDeclaration)) {
                    return new Object();
                }
                return null;
            }

            @Override
            public Object visit(EnumConstantDeclaration enumConstantDeclaration, Void arg) {
                if (matchesNameAndContainsAnnotation(enumConstantDeclaration.getName(), constructor.getName(), enumConstantDeclaration)) {
                    return new Object();
                }
                return null;
            }
        };
    } else if (member instanceof JApiClass) {
        final JApiClass clazz = (JApiClass) member;
        if (isDeprecated(clazz)) {
            return null;
        }
        className = clazz.getFullyQualifiedName();
        visitor = new GenericVisitorAdapter<Object, Void>() {

            @Override
            public Object visit(ClassOrInterfaceDeclaration classDeclaration, Void arg) {
                if (matchesNameAndContainsAnnotation(classDeclaration.getName(), toSimpleName(clazz.getFullyQualifiedName()), classDeclaration)) {
                    return new Object();
                }
                return super.visit(classDeclaration, arg);
            }

            @Override
            public Object visit(AnnotationDeclaration annotationDeclaration, Void arg) {
                if (matchesNameAndContainsAnnotation(annotationDeclaration.getName(), toSimpleName(clazz.getFullyQualifiedName()), annotationDeclaration)) {
                    return new Object();
                }
                return super.visit(annotationDeclaration, arg);
            }

            @Override
            public Object visit(EnumDeclaration enumDeclaration, Void arg) {
                if (matchesNameAndContainsAnnotation(enumDeclaration.getName(), toSimpleName(clazz.getFullyQualifiedName()), enumDeclaration)) {
                    return new Object();
                }
                return super.visit(enumDeclaration, arg);
            }
        };
    }
    if (className == null) {
        return null;
    }
    try {
        Object result = JavaParser.parse(sourceFileFor(className)).accept(visitor, null);
        if (result == null) {
            return acceptOrReject(member, Violation.error(member, "Is not annotated with @since " + getCurrentVersion()));
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    return null;
}
Also used : JApiConstructor(japicmp.model.JApiConstructor) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) JApiMethod(japicmp.model.JApiMethod) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) JApiField(japicmp.model.JApiField) GenericVisitorAdapter(com.github.javaparser.ast.visitor.GenericVisitorAdapter) FieldDeclaration(com.github.javaparser.ast.body.FieldDeclaration) EnumDeclaration(com.github.javaparser.ast.body.EnumDeclaration) EnumConstantDeclaration(com.github.javaparser.ast.body.EnumConstantDeclaration) AnnotationDeclaration(com.github.javaparser.ast.body.AnnotationDeclaration) JApiClass(japicmp.model.JApiClass) ConstructorDeclaration(com.github.javaparser.ast.body.ConstructorDeclaration)

Example 10 with EnumConstantDeclaration

use of com.github.javaparser.ast.body.EnumConstantDeclaration in project javaparser by javaparser.

the class VoidVisitorAdapter method visit.

@Override
public void visit(final EnumDeclaration n, final A arg) {
    visitComment(n.getComment(), arg);
    visitAnnotations(n, arg);
    n.getNameExpr().accept(this, arg);
    if (n.getImplements() != null) {
        for (final ClassOrInterfaceType c : n.getImplements()) {
            c.accept(this, arg);
        }
    }
    if (n.getEntries() != null) {
        for (final EnumConstantDeclaration e : n.getEntries()) {
            e.accept(this, arg);
        }
    }
    if (n.getMembers() != null) {
        for (final BodyDeclaration<?> member : n.getMembers()) {
            member.accept(this, arg);
        }
    }
}
Also used : EnumConstantDeclaration(com.github.javaparser.ast.body.EnumConstantDeclaration)

Aggregations

EnumConstantDeclaration (com.github.javaparser.ast.body.EnumConstantDeclaration)18 EnumDeclaration (com.github.javaparser.ast.body.EnumDeclaration)8 ClassOrInterfaceDeclaration (com.github.javaparser.ast.body.ClassOrInterfaceDeclaration)5 BodyDeclaration (com.github.javaparser.ast.body.BodyDeclaration)4 AnnotationDeclaration (com.github.javaparser.ast.body.AnnotationDeclaration)3 ConstructorDeclaration (com.github.javaparser.ast.body.ConstructorDeclaration)3 FieldDeclaration (com.github.javaparser.ast.body.FieldDeclaration)3 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)3 RecordDeclaration (com.github.javaparser.ast.body.RecordDeclaration)3 VariableDeclarator (com.github.javaparser.ast.body.VariableDeclarator)3 NewClassTree (com.sun.source.tree.NewClassTree)3 ExecutableElement (javax.lang.model.element.ExecutableElement)3 TypeElement (javax.lang.model.element.TypeElement)3 VariableElement (javax.lang.model.element.VariableElement)3 Parameter (com.github.javaparser.ast.body.Parameter)2 ReceiverParameter (com.github.javaparser.ast.body.ReceiverParameter)2 BlockComment (com.github.javaparser.ast.comments.BlockComment)2 Comment (com.github.javaparser.ast.comments.Comment)2 JavadocComment (com.github.javaparser.ast.comments.JavadocComment)2 LineComment (com.github.javaparser.ast.comments.LineComment)2