Search in sources :

Example 1 with ConstraintViolation

use of dev.morphia.mapping.validation.ConstraintViolation in project morphia by mongodb.

the class MapKeyTypeConstraint method check.

@Override
protected void check(Mapper mapper, EntityModel entityModel, PropertyModel propertyModel, Set<ConstraintViolation> ve) {
    if (propertyModel.isMap()) {
        Class<?> aClass = null;
        List<TypeData<?>> typeParameters = propertyModel.getTypeData().getTypeParameters();
        if (!typeParameters.isEmpty()) {
            TypeData<?> typeData = typeParameters.get(0);
            aClass = typeData.getType();
        }
        // WARN if not parameterized : null or Object...
        if (aClass == null || Object.class.equals(aClass)) {
            ve.add(new ConstraintViolation(Level.WARNING, entityModel, propertyModel, getClass(), "Maps cannot be keyed by Object (Map<Object,?>); Use a parametrized type that is supported " + SUPPORTED));
        } else if (!aClass.equals(String.class) && !aClass.equals(ObjectId.class) && !isPrimitiveLike(aClass)) {
            ve.add(new ConstraintViolation(Level.FATAL, entityModel, propertyModel, getClass(), "Maps must be keyed by a simple type " + SUPPORTED + "; " + aClass + " is not supported as a map key type."));
        }
    }
}
Also used : TypeData(dev.morphia.mapping.codec.pojo.TypeData) ConstraintViolation(dev.morphia.mapping.validation.ConstraintViolation)

Aggregations

TypeData (dev.morphia.mapping.codec.pojo.TypeData)1 ConstraintViolation (dev.morphia.mapping.validation.ConstraintViolation)1