Search in sources :

Example 1 with Property

use of core.framework.api.json.Property in project core-ng-project by neowu.

the class JSONAnnotationIntrospector method findEnumValues.

@Override
public String[] findEnumValues(Class<?> enumType, Enum<?>[] enumValues, String[] names) {
    Map<String, String> mappings = null;
    for (Field field : enumType.getDeclaredFields()) {
        if (!field.isEnumConstant())
            continue;
        Property enumValue = field.getDeclaredAnnotation(Property.class);
        if (enumValue == null)
            continue;
        String value = enumValue.name();
        if (value.isEmpty())
            continue;
        if (mappings == null)
            mappings = new HashMap<>();
        mappings.put(field.getName(), value);
    }
    if (mappings != null) {
        int length = enumValues.length;
        for (int i = 0; i < length; i++) {
            String name = enumValues[i].name();
            String value = mappings.get(name);
            if (value != null)
                names[i] = value;
        }
    }
    return names;
}
Also used : Field(java.lang.reflect.Field) HashMap(java.util.HashMap) Property(core.framework.api.json.Property)

Example 2 with Property

use of core.framework.api.json.Property in project core-ng-project by neowu.

the class DatabaseClassValidator method validateEnumClass.

private void validateEnumClass(Class<?> enumClass, Field field) {
    Enum<?>[] constants = (Enum<?>[]) enumClass.getEnumConstants();
    Set<String> enumValues = Sets.newHashSet();
    for (Enum<?> constant : constants) {
        DBEnumValue enumValue = Enums.constantAnnotation(constant, DBEnumValue.class);
        if (enumValue == null) {
            throw Exceptions.error("db enum must have @DBEnumValue, field={}, enum={}", Fields.path(field), Enums.path(constant));
        }
        boolean added = enumValues.add(enumValue.value());
        if (!added) {
            throw Exceptions.error("db enum value must be unique, enum={}, value={}", Enums.path(constant), enumValue.value());
        }
        Property property = Enums.constantAnnotation(constant, Property.class);
        if (property != null) {
            throw Exceptions.error("db enum must not have json annotation, please separate view and entity, field={}, enum={}", Fields.path(field), Enums.path(constant));
        }
    }
}
Also used : DBEnumValue(core.framework.db.DBEnumValue) Property(core.framework.api.json.Property)

Example 3 with Property

use of core.framework.api.json.Property in project core-ng-project by neowu.

the class JSONTypeValidator method visitField.

@Override
public void visitField(Field field, String parentPath) {
    Property property = field.getDeclaredAnnotation(Property.class);
    if (property == null)
        throw Exceptions.error("field must have @Property, field={}", Fields.path(field));
    String name = property.name();
    if (Strings.isEmpty(name)) {
        throw Exceptions.error("@Property name attribute must not be empty, field={}", Fields.path(field));
    }
    Set<String> properties = this.properties.computeIfAbsent(parentPath, key -> Sets.newHashSet());
    if (properties.contains(name)) {
        throw Exceptions.error("found duplicate property, field={}, name={}", Fields.path(field), name);
    }
    properties.add(name);
    Class<?> fieldClass = field.getType();
    if (fieldClass.isEnum()) {
        validateEnumClass(fieldClass);
    }
}
Also used : Property(core.framework.api.json.Property)

Example 4 with Property

use of core.framework.api.json.Property in project core-ng-project by neowu.

the class QueryParamBeanTypeValidator method visitField.

@Override
public void visitField(Field field, String parentPath) {
    QueryParam queryParam = field.getDeclaredAnnotation(QueryParam.class);
    if (queryParam == null)
        throw Exceptions.error("field must have @QueryParam, field={}", Fields.path(field));
    Property property = field.getDeclaredAnnotation(Property.class);
    if (property != null)
        throw Exceptions.error("field must not have @Property, field={}", Fields.path(field));
    String name = queryParam.name();
    boolean added = visitedQueryParams.add(queryParam.name());
    if (!added) {
        throw Exceptions.error("found duplicate query param, field={}, name={}", Fields.path(field), name);
    }
    Class<?> fieldClass = field.getType();
    if (fieldClass.isEnum()) {
        JSONTypeValidator.validateEnumClass(fieldClass);
    }
}
Also used : QueryParam(core.framework.api.web.service.QueryParam) Property(core.framework.api.json.Property)

Example 5 with Property

use of core.framework.api.json.Property in project core-ng-project by neowu.

the class MongoClassValidator method validateEnumClass.

private void validateEnumClass(Class<?> enumClass, Field field) {
    Enum<?>[] constants = (Enum<?>[]) enumClass.getEnumConstants();
    Set<String> enumValues = Sets.newHashSet();
    for (Enum<?> constant : constants) {
        MongoEnumValue enumValue = Enums.constantAnnotation(constant, MongoEnumValue.class);
        if (enumValue == null) {
            throw Exceptions.error("mongo enum must have @MongoEnumValue, field={}, enum={}", Fields.path(field), Enums.path(constant));
        }
        boolean added = enumValues.add(enumValue.value());
        if (!added) {
            throw Exceptions.error("mongo enum value must be unique, enum={}, value={}", Enums.path(constant), enumValue.value());
        }
        Property property = Enums.constantAnnotation(constant, Property.class);
        if (property != null) {
            throw Exceptions.error("mongo enum must not have json annotation, please separate view and entity, field={}, enum={}", Fields.path(field), Enums.path(constant));
        }
    }
}
Also used : MongoEnumValue(core.framework.mongo.MongoEnumValue) Property(core.framework.api.json.Property)

Aggregations

Property (core.framework.api.json.Property)5 QueryParam (core.framework.api.web.service.QueryParam)1 DBEnumValue (core.framework.db.DBEnumValue)1 MongoEnumValue (core.framework.mongo.MongoEnumValue)1 Field (java.lang.reflect.Field)1 HashMap (java.util.HashMap)1