Search in sources :

Example 1 with FieldDeclaration

use of com.github.javaparser.ast.body.FieldDeclaration in project butterknife by JakeWharton.

the class FinalRClassBuilder method addResourceType.

private static void addResourceType(List<String> supportedTypes, TypeSpec.Builder result, TypeDeclaration node) {
    if (!supportedTypes.contains(node.getName())) {
        return;
    }
    String type = node.getName();
    TypeSpec.Builder resourceType = TypeSpec.classBuilder(type).addModifiers(PUBLIC, STATIC, FINAL);
    for (BodyDeclaration field : node.getMembers()) {
        if (field instanceof FieldDeclaration) {
            addResourceField(resourceType, ((FieldDeclaration) field).getVariables().get(0), getSupportAnnotationClass(type));
        }
    }
    result.addType(resourceType.build());
}
Also used : BodyDeclaration(com.github.javaparser.ast.body.BodyDeclaration) FieldDeclaration(com.github.javaparser.ast.body.FieldDeclaration) TypeSpec(com.squareup.javapoet.TypeSpec)

Example 2 with FieldDeclaration

use of com.github.javaparser.ast.body.FieldDeclaration 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)

Aggregations

FieldDeclaration (com.github.javaparser.ast.body.FieldDeclaration)2 AnnotationDeclaration (com.github.javaparser.ast.body.AnnotationDeclaration)1 BodyDeclaration (com.github.javaparser.ast.body.BodyDeclaration)1 ClassOrInterfaceDeclaration (com.github.javaparser.ast.body.ClassOrInterfaceDeclaration)1 ConstructorDeclaration (com.github.javaparser.ast.body.ConstructorDeclaration)1 EnumConstantDeclaration (com.github.javaparser.ast.body.EnumConstantDeclaration)1 EnumDeclaration (com.github.javaparser.ast.body.EnumDeclaration)1 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)1 GenericVisitorAdapter (com.github.javaparser.ast.visitor.GenericVisitorAdapter)1 TypeSpec (com.squareup.javapoet.TypeSpec)1 JApiClass (japicmp.model.JApiClass)1 JApiConstructor (japicmp.model.JApiConstructor)1 JApiField (japicmp.model.JApiField)1 JApiMethod (japicmp.model.JApiMethod)1