Search in sources :

Example 1 with NotNull

use of core.framework.api.validate.NotNull in project core-ng-project by neowu.

the class ObjectValidatorBuilder method validateObjectMethod.

private String validateObjectMethod(Class<?> beanClass, String parentPath) {
    String methodName = "validate" + beanClass.getSimpleName() + (index++);
    CodeBuilder builder = new CodeBuilder().append("private void {}({} bean, {} errors, boolean partial) {\n", methodName, type(beanClass), type(ValidationErrors.class));
    for (Field field : Classes.instanceFields(beanClass)) {
        if (!hasValidationAnnotation(field))
            continue;
        validateAnnotations(field);
        Type fieldType = field.getGenericType();
        Class<?> fieldClass = GenericTypes.rawClass(fieldType);
        String pathLiteral = variable(path(field, parentPath));
        builder.indent(1).append("if (bean.{} == null) {\n", field.getName());
        NotNull notNull = field.getDeclaredAnnotation(NotNull.class);
        if (notNull != null)
            builder.indent(2).append("if (!partial) errors.add({}, {});\n", pathLiteral, variable(notNull.message()));
        builder.indent(1).append("} else {\n");
        if (String.class.equals(fieldClass)) {
            buildStringValidation(builder, field, pathLiteral);
        } else if (GenericTypes.isList(fieldType)) {
            buildListValidation(builder, field, pathLiteral, parentPath);
        } else if (GenericTypes.isMap(fieldType)) {
            buildMapValidation(builder, field, pathLiteral, parentPath);
        } else if (Number.class.isAssignableFrom(fieldClass)) {
            buildNumberValidation(builder, field, pathLiteral);
        } else if (!isValueClass(fieldClass)) {
            String method = validateObjectMethod(fieldClass, path(field, parentPath));
            builder.indent(2).append("{}(bean.{}, errors, partial);\n", method, field.getName());
        }
        builder.indent(1).append("}\n");
    }
    builder.append('}');
    this.builder.addMethod(builder.build());
    return methodName;
}
Also used : Field(java.lang.reflect.Field) Type(java.lang.reflect.Type) NotNull(core.framework.api.validate.NotNull) CodeBuilder(core.framework.impl.asm.CodeBuilder)

Aggregations

NotNull (core.framework.api.validate.NotNull)1 CodeBuilder (core.framework.impl.asm.CodeBuilder)1 Field (java.lang.reflect.Field)1 Type (java.lang.reflect.Type)1