MapKeyDifferentFromString extends FieldConstraint { @Override protected void check(MappedClass mc, MappedField mf, Set<ConstraintViolation> ve) { if (mf.isMap() && (!mf.hasAnnotation(Serialized.class))) { Class<?> parameterizedClass = ReflectionUtils.getParameterizedClass(mf.getField(), 0); if (parameterizedClass == null) { ve.add(new ConstraintViolation(Level.WARNING, mc, mf, this.getClass(), "Maps cannot be keyed by Object (Map<Object,?>); Use a parametrized type that is supported " + supportedExample)); } else if (!parameterizedClass.equals(String.class) && !parameterizedClass.equals(ObjectId.class) && !ReflectionUtils.isPrimitiveLike(parameterizedClass)) ve .add(new ConstraintViolation(Level.FATAL, mc, mf, this.getClass(), "Maps must be keyed by a simple type " + supportedExample + "; " + parameterizedClass + " is not supported as a map key type.")); } }    }
@Test public void testCheck() { morphia.map(MapWithWrongKeyType1.class); new AssertedFailure() { public void thisMustFail() throws Throwable { morphia.map(MapWithWrongKeyType2.class); } }; new AssertedFailure() { public void thisMustFail() throws Throwable { morphia.map(MapWithWrongKeyType3.class); } }; }