Search in sources :

Example 1 with ConstraintViolation

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

the class ContainsEmbeddedWithId method checkRecursivelyHasNoIdAnnotationPresent.

private void checkRecursivelyHasNoIdAnnotationPresent(final Set<Class<?>> classesToInspect, final HashSet<Class<?>> alreadyInspectedClasses, final MappedClass mc, final Set<ConstraintViolation> ve) {
    for (final Class<?> clazz : classesToInspect) {
        if (alreadyInspectedClasses.contains(clazz)) {
            continue;
        }
        if (hasTypeFieldAnnotation(clazz, Id.class)) {
            ve.add(new ConstraintViolation(Level.FATAL, mc, getClass(), "You cannot use @Id on any field of an Embedded/Property object"));
        }
        alreadyInspectedClasses.add(clazz);
        final Set<Class<?>> extraClassesToInspect = new HashSet<Class<?>>();
        for (final Field field : ReflectionUtils.getDeclaredAndInheritedFields(clazz, true)) {
            if (isFieldToInspect(field)) {
                extraClassesToInspect.add(field.getType());
            }
        }
        checkRecursivelyHasNoIdAnnotationPresent(extraClassesToInspect, alreadyInspectedClasses, mc, ve);
    }
}
Also used : Field(java.lang.reflect.Field) ConstraintViolation(org.mongodb.morphia.mapping.validation.ConstraintViolation) MappedClass(org.mongodb.morphia.mapping.MappedClass) HashSet(java.util.HashSet)

Example 2 with ConstraintViolation

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

the class MapNotSerializable method check.

@Override
protected void check(final Mapper mapper, final MappedClass mc, final MappedField mf, final Set<ConstraintViolation> ve) {
    if (mf.isMap()) {
        if (mf.hasAnnotation(Serialized.class)) {
            final Class<?> keyClass = ReflectionUtils.getParameterizedClass(mf.getField(), 0);
            final Class<?> valueClass = ReflectionUtils.getParameterizedClass(mf.getField(), 1);
            if (keyClass != null) {
                if (!Serializable.class.isAssignableFrom(keyClass)) {
                    ve.add(new ConstraintViolation(Level.FATAL, mc, mf, getClass(), "Key class (" + keyClass.getName() + ") is not Serializable"));
                }
            }
            if (valueClass != null) {
                if (!Serializable.class.isAssignableFrom(valueClass)) {
                    ve.add(new ConstraintViolation(Level.FATAL, mc, mf, getClass(), "Value class (" + valueClass.getName() + ") is not Serializable"));
                }
            }
        }
    }
}
Also used : Serializable(java.io.Serializable) ConstraintViolation(org.mongodb.morphia.mapping.validation.ConstraintViolation)

Aggregations

ConstraintViolation (org.mongodb.morphia.mapping.validation.ConstraintViolation)2 Serializable (java.io.Serializable)1 Field (java.lang.reflect.Field)1 HashSet (java.util.HashSet)1 MappedClass (org.mongodb.morphia.mapping.MappedClass)1