MapNotSerializable extends FieldConstraint { @Override protected void check(MappedClass mc, MappedField mf, Set<ConstraintViolation> ve) { if (mf.isMap()) { if (mf.hasAnnotation(Serialized.class)) { Class<?> keyClass = ReflectionUtils.getParameterizedClass(mf.getField(), 0); Class<?> valueClass = ReflectionUtils.getParameterizedClass(mf.getField(), 1); if (keyClass!=null) { if (!Serializable.class.isAssignableFrom(keyClass)) ve.add(new ConstraintViolation(Level.FATAL, mc, mf, this.getClass(), "Key class (" + keyClass.getName() + ") is not Serializable")); } if (valueClass != null) { if (!Serializable.class.isAssignableFrom(keyClass)) ve.add(new ConstraintViolation(Level.FATAL, mc, mf, this.getClass(), "Value class (" + valueClass.getName() + ") is not Serializable")); } } } }    }
@Test public void testCheck() { morphia.map(Map1.class); new AssertedFailure() { public void thisMustFail() throws Throwable { morphia.map(Map2.class); } }; new AssertedFailure() { public void thisMustFail() throws Throwable { morphia.map(Map3.class); } }; }