Search in sources :

Example 6 with AnnotationDeclaration

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

the class CompilationUnit method addAnnotationDeclaration.

/**
 * Add an annotation declaration to the types of this compilation unit
 *
 * @param name the annotation name
 * @param modifiers the modifiers (like Modifier.PUBLIC)
 * @return the newly created class
 */
public AnnotationDeclaration addAnnotationDeclaration(String name, Modifier... modifiers) {
    AnnotationDeclaration annotationDeclaration = new AnnotationDeclaration(Arrays.stream(modifiers).collect(Collectors.toCollection(() -> EnumSet.noneOf(Modifier.class))), name);
    getTypes().add(annotationDeclaration);
    return annotationDeclaration;
}
Also used : AnnotationDeclaration(com.github.javaparser.ast.body.AnnotationDeclaration)

Example 7 with AnnotationDeclaration

use of com.github.javaparser.ast.body.AnnotationDeclaration 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 8 with AnnotationDeclaration

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

the class CompilationUnitContext method solveType.

@Override
public SymbolReference<ResolvedTypeDeclaration> solveType(String name, TypeSolver typeSolver) {
    if (wrappedNode.getTypes() != null) {
        for (TypeDeclaration<?> type : wrappedNode.getTypes()) {
            if (type.getName().getId().equals(name)) {
                if (type instanceof ClassOrInterfaceDeclaration) {
                    return SymbolReference.solved(JavaParserFacade.get(typeSolver).getTypeDeclaration((ClassOrInterfaceDeclaration) type));
                } else if (type instanceof AnnotationDeclaration) {
                    return SymbolReference.solved(new JavaParserAnnotationDeclaration((AnnotationDeclaration) type, typeSolver));
                } else if (type instanceof EnumDeclaration) {
                    return SymbolReference.solved(new JavaParserEnumDeclaration((EnumDeclaration) type, typeSolver));
                } else {
                    throw new UnsupportedOperationException(type.getClass().getCanonicalName());
                }
            }
        }
    }
    // Look in current package
    if (this.wrappedNode.getPackageDeclaration().isPresent()) {
        String qName = this.wrappedNode.getPackageDeclaration().get().getName().toString() + "." + name;
        SymbolReference<ResolvedReferenceTypeDeclaration> ref = typeSolver.tryToSolveType(qName);
        if (ref != null && ref.isSolved()) {
            return SymbolReference.adapt(ref, ResolvedTypeDeclaration.class);
        }
    } else {
        // look for classes in the default package
        String qName = name;
        SymbolReference<ResolvedReferenceTypeDeclaration> ref = typeSolver.tryToSolveType(qName);
        if (ref != null && ref.isSolved()) {
            return SymbolReference.adapt(ref, ResolvedTypeDeclaration.class);
        }
    }
    if (wrappedNode.getImports() != null) {
        int dotPos = name.indexOf('.');
        String prefix = null;
        if (dotPos > -1) {
            prefix = name.substring(0, dotPos);
        }
        // look into type imports
        for (ImportDeclaration importDecl : wrappedNode.getImports()) {
            if (!importDecl.isAsterisk()) {
                String qName = importDecl.getNameAsString();
                boolean defaultPackage = !importDecl.getName().getQualifier().isPresent();
                boolean found = !defaultPackage && importDecl.getName().getIdentifier().equals(name);
                if (!found) {
                    if (prefix != null) {
                        found = qName.endsWith("." + prefix);
                        if (found) {
                            qName = qName + name.substring(dotPos);
                        }
                    }
                }
                if (found) {
                    SymbolReference<ResolvedReferenceTypeDeclaration> ref = typeSolver.tryToSolveType(qName);
                    if (ref.isSolved()) {
                        return SymbolReference.adapt(ref, ResolvedTypeDeclaration.class);
                    }
                }
            }
        }
        // look into type imports on demand
        for (ImportDeclaration importDecl : wrappedNode.getImports()) {
            if (importDecl.isAsterisk()) {
                String qName = importDecl.getNameAsString() + "." + name;
                SymbolReference<ResolvedReferenceTypeDeclaration> ref = typeSolver.tryToSolveType(qName);
                if (ref.isSolved()) {
                    return SymbolReference.adapt(ref, ResolvedTypeDeclaration.class);
                }
            }
        }
    }
    // Look in the java.lang package
    SymbolReference<ResolvedReferenceTypeDeclaration> ref = typeSolver.tryToSolveType("java.lang." + name);
    if (ref.isSolved()) {
        return SymbolReference.adapt(ref, ResolvedTypeDeclaration.class);
    }
    // DO NOT look for absolute name if this name is not qualified: you cannot import classes from the default package
    if (isQualifiedName(name)) {
        return SymbolReference.adapt(typeSolver.tryToSolveType(name), ResolvedTypeDeclaration.class);
    } else {
        return SymbolReference.unsolved(ResolvedReferenceTypeDeclaration.class);
    }
}
Also used : ResolvedReferenceTypeDeclaration(com.github.javaparser.resolution.declarations.ResolvedReferenceTypeDeclaration) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) EnumDeclaration(com.github.javaparser.ast.body.EnumDeclaration) JavaParserEnumDeclaration(com.github.javaparser.symbolsolver.javaparsermodel.declarations.JavaParserEnumDeclaration) JavaParserAnnotationDeclaration(com.github.javaparser.symbolsolver.javaparsermodel.declarations.JavaParserAnnotationDeclaration) JavaParserEnumDeclaration(com.github.javaparser.symbolsolver.javaparsermodel.declarations.JavaParserEnumDeclaration) JavaParserAnnotationDeclaration(com.github.javaparser.symbolsolver.javaparsermodel.declarations.JavaParserAnnotationDeclaration) AnnotationDeclaration(com.github.javaparser.ast.body.AnnotationDeclaration) ImportDeclaration(com.github.javaparser.ast.ImportDeclaration)

Example 9 with AnnotationDeclaration

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

the class LexicalDifferenceCalculatorTest method annotationDeclarationNameExampleModified.

@Test
public void annotationDeclarationNameExampleModified() throws IOException {
    considerExample("AnnotationDeclaration_Example1_original");
    AnnotationDeclaration annotationDeclaration = (AnnotationDeclaration) cu.getType(0);
    CsmElement element = ConcreteSyntaxModel.forClass(annotationDeclaration.getClass());
    SimpleName newName = new SimpleName("NewName");
    LexicalDifferenceCalculator.CalculatedSyntaxModel csm = new LexicalDifferenceCalculator().calculatedSyntaxModelAfterPropertyChange(element, annotationDeclaration, ObservableProperty.NAME, annotationDeclaration.getName(), newName);
    csm.removeIndentationElements();
    int i = 0;
    assertEquals(new CsmToken(GeneratedJavaParserConstants.AT), csm.elements.get(i++));
    assertEquals(new CsmToken(GeneratedJavaParserConstants.INTERFACE), csm.elements.get(i++));
    assertEquals(new CsmToken(spaceTokenKind()), csm.elements.get(i++));
    assertEquals(new LexicalDifferenceCalculator.CsmChild(newName), csm.elements.get(i++));
    assertEquals(new CsmToken(spaceTokenKind()), csm.elements.get(i++));
    assertEquals(new CsmToken(GeneratedJavaParserConstants.LBRACE), csm.elements.get(i++));
    assertEquals(new CsmToken(eolTokenKind()), csm.elements.get(i++));
    assertEquals(new LexicalDifferenceCalculator.CsmChild(annotationDeclaration.getMember(0)), csm.elements.get(i++));
    assertEquals(new CsmToken(eolTokenKind()), csm.elements.get(i++));
    assertEquals(new LexicalDifferenceCalculator.CsmChild(annotationDeclaration.getMember(1)), csm.elements.get(i++));
    assertEquals(new CsmToken(eolTokenKind()), csm.elements.get(i++));
    assertEquals(new LexicalDifferenceCalculator.CsmChild(annotationDeclaration.getMember(2)), csm.elements.get(i++));
    assertEquals(new CsmToken(eolTokenKind()), csm.elements.get(i++));
    assertEquals(new LexicalDifferenceCalculator.CsmChild(annotationDeclaration.getMember(3)), csm.elements.get(i++));
    assertEquals(new CsmToken(eolTokenKind()), csm.elements.get(i++));
    assertEquals(new LexicalDifferenceCalculator.CsmChild(annotationDeclaration.getMember(4)), csm.elements.get(i++));
    assertEquals(new CsmToken(eolTokenKind()), csm.elements.get(i++));
    assertEquals(new LexicalDifferenceCalculator.CsmChild(annotationDeclaration.getMember(5)), csm.elements.get(i++));
    assertEquals(new CsmToken(eolTokenKind()), csm.elements.get(i++));
    assertEquals(new CsmToken(GeneratedJavaParserConstants.RBRACE), csm.elements.get(i++));
    assertEquals(i, csm.elements.size());
}
Also used : CsmToken(com.github.javaparser.printer.concretesyntaxmodel.CsmToken) AnnotationDeclaration(com.github.javaparser.ast.body.AnnotationDeclaration) CsmElement(com.github.javaparser.printer.concretesyntaxmodel.CsmElement) Test(org.junit.Test)

Example 10 with AnnotationDeclaration

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

the class LexicalDifferenceCalculatorTest method annotationDeclaratioJavadocExampleAddingJavadoc.

@Test
public void annotationDeclaratioJavadocExampleAddingJavadoc() throws IOException {
    considerExample("AnnotationDeclaration_Example3_original");
    AnnotationDeclaration annotationDeclaration = (AnnotationDeclaration) cu.getType(0);
    CsmElement element = ConcreteSyntaxModel.forClass(annotationDeclaration.getClass());
    JavadocComment comment = new JavadocComment("Cool this annotation!");
    LexicalDifferenceCalculator.CalculatedSyntaxModel csm = new LexicalDifferenceCalculator().calculatedSyntaxModelAfterPropertyChange(element, annotationDeclaration, ObservableProperty.COMMENT, null, comment);
    csm.removeIndentationElements();
    int i = 0;
    assertEquals(new CsmToken(GeneratedJavaParserConstants.PUBLIC), csm.elements.get(i++));
    assertEquals(new CsmToken(spaceTokenKind()), csm.elements.get(i++));
    assertEquals(new CsmToken(GeneratedJavaParserConstants.AT), csm.elements.get(i++));
    assertEquals(new CsmToken(GeneratedJavaParserConstants.INTERFACE), csm.elements.get(i++));
    assertEquals(new CsmToken(spaceTokenKind()), csm.elements.get(i++));
    assertEquals(new LexicalDifferenceCalculator.CsmChild(annotationDeclaration.getName()), csm.elements.get(i++));
    assertEquals(new CsmToken(spaceTokenKind()), csm.elements.get(i++));
    assertEquals(new CsmToken(GeneratedJavaParserConstants.LBRACE), csm.elements.get(i++));
    assertEquals(new CsmToken(eolTokenKind()), csm.elements.get(i++));
    assertEquals(new LexicalDifferenceCalculator.CsmChild(annotationDeclaration.getMember(0)), csm.elements.get(i++));
    assertEquals(new CsmToken(eolTokenKind()), csm.elements.get(i++));
    assertEquals(new LexicalDifferenceCalculator.CsmChild(annotationDeclaration.getMember(1)), csm.elements.get(i++));
    assertEquals(new CsmToken(eolTokenKind()), csm.elements.get(i++));
    assertEquals(new LexicalDifferenceCalculator.CsmChild(annotationDeclaration.getMember(2)), csm.elements.get(i++));
    assertEquals(new CsmToken(eolTokenKind()), csm.elements.get(i++));
    assertEquals(new LexicalDifferenceCalculator.CsmChild(annotationDeclaration.getMember(3)), csm.elements.get(i++));
    assertEquals(new CsmToken(eolTokenKind()), csm.elements.get(i++));
    assertEquals(new LexicalDifferenceCalculator.CsmChild(annotationDeclaration.getMember(4)), csm.elements.get(i++));
    assertEquals(new CsmToken(eolTokenKind()), csm.elements.get(i++));
    assertEquals(new LexicalDifferenceCalculator.CsmChild(annotationDeclaration.getMember(5)), csm.elements.get(i++));
    assertEquals(new CsmToken(eolTokenKind()), csm.elements.get(i++));
    assertEquals(new CsmToken(GeneratedJavaParserConstants.RBRACE), csm.elements.get(i++));
    assertEquals(i, csm.elements.size());
}
Also used : JavadocComment(com.github.javaparser.ast.comments.JavadocComment) CsmToken(com.github.javaparser.printer.concretesyntaxmodel.CsmToken) AnnotationDeclaration(com.github.javaparser.ast.body.AnnotationDeclaration) CsmElement(com.github.javaparser.printer.concretesyntaxmodel.CsmElement) Test(org.junit.Test)

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