Search in sources :

Example 1 with Method

use of com.github.havardh.javaflow.ast.Method in project javaflow by havardh.

the class ClassGetterNamingVerifier method validate.

private List<Exception> validate(Class classToValidate) {
    List<Method> getters = classToValidate.getGetters();
    List<Field> fields = classToValidate.getFields();
    if (getters.size() != fields.size()) {
        return singletonList(new FieldGettersMismatchException(classToValidate.getCanonicalName(), format("Number of getters and fields is not the same.\n" + "Fields in model: %s\n" + "Getters in model: %s", fields, getters)));
    }
    List<Exception> exceptions = new ArrayList<>();
    for (Method getter : getters) {
        try {
            Field correspondingField = findFieldByGetter(classToValidate, fields, getter);
            if (!correspondingField.getType().equals(getter.getType())) {
                throw new FieldGettersMismatchException(classToValidate.getCanonicalName(), format("Type of getter %s (%s) does not correspond to field %s (%s)", getter.getName(), getter.getType(), correspondingField.getName(), correspondingField.getType()));
            }
        } catch (FieldGettersMismatchException e) {
            exceptions.add(e);
        }
    }
    return exceptions;
}
Also used : Field(com.github.havardh.javaflow.ast.Field) ArrayList(java.util.ArrayList) Method(com.github.havardh.javaflow.ast.Method) FieldGettersMismatchException(com.github.havardh.javaflow.exceptions.FieldGettersMismatchException) AggregatedException(com.github.havardh.javaflow.exceptions.AggregatedException) FieldGettersMismatchException(com.github.havardh.javaflow.exceptions.FieldGettersMismatchException)

Example 2 with Method

use of com.github.havardh.javaflow.ast.Method in project javaflow by havardh.

the class ClassVisitor method visit.

@Override
public void visit(MethodDeclaration method, ClassBuilder builder) {
    super.visit(method, builder);
    TypeFactory factory = new TypeFactory(packageName, imports);
    if (isClass((ClassOrInterfaceDeclaration) method.getParentNode()) && isGetter(method.getName(), method.getType().toString())) {
        builder.withGetter(new Method(method.getName(), factory.build(method.getType().toString(), method.getType() instanceof PrimitiveType)));
    }
}
Also used : ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) PrimitiveType(com.github.javaparser.ast.type.PrimitiveType) Method(com.github.havardh.javaflow.ast.Method)

Aggregations

Method (com.github.havardh.javaflow.ast.Method)2 Field (com.github.havardh.javaflow.ast.Field)1 AggregatedException (com.github.havardh.javaflow.exceptions.AggregatedException)1 FieldGettersMismatchException (com.github.havardh.javaflow.exceptions.FieldGettersMismatchException)1 ClassOrInterfaceDeclaration (com.github.javaparser.ast.body.ClassOrInterfaceDeclaration)1 PrimitiveType (com.github.javaparser.ast.type.PrimitiveType)1 ArrayList (java.util.ArrayList)1