IntegerToEnumTypeMapper implements TypeMapper<Integer, T> { @Override public Integer toDatabaseType(T constant) { if (constant == null) { return null; } else { final Class<?> enumClass = constant.getClass(); final Method ordinal; try { ordinal = enumClass.getMethod("ordinal"); } catch (final NoSuchMethodException ex) { throw new IllegalArgumentException("Could not find generated 'ordinal()'-method in enum class '" + constant.getClass().getName() + "'.", ex); } try { @SuppressWarnings("unchecked") final Integer result = (Integer) ordinal.invoke(constant); return result; } catch (final IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) { throw new IllegalArgumentException("Error executing 'ordinal()' in generated enum class '" + constant.getClass().getName() + "'.", ex); } } } IntegerToEnumTypeMapper(); @Override String getLabel(); @Override boolean isToolApplicable(); @Override Type getJavaType(Column column); @Override TypeMapper.Category getJavaTypeCategory(Column column); @Override T toJavaType(Column column, Class<?> entityType, Integer value); @Override Integer toDatabaseType(T constant); @Inject public Injector injector; }
@Test void toDatabaseType() { assertEquals(0, instance.toDatabaseType(User.Name.ZERO)); }
@Test void toDatabaseTypeNull() { assertNull(instance.toDatabaseType(null)); }