@Override public <T> Codec<T> get(Class<T> clazz, CodecRegistry registry) { // if clazz has type parameters, we warn the user that generic class definitions are problematic Codec<T> codec = pojoContext.get(clazz, registry); if (codec instanceof TypeCodec) { if (clazz != null && clazz.getTypeParameters().length > 0) { LOGGER.warn("Generic classes will only be encoded/decoded with their upper bounds! " + "We could prohibit handling of the pojo codec for those generic classes, " + "but then user would loose flexibility when subclassing such classes. Class: " + clazz.toGenericString()); } TypeCodec typeCodec = (TypeCodec) codec; // generate dynamic proxy to add CollectibleCodec functionality if (typeCodec.isCollectible()) { LOGGER.debug("Enhancing {} to be collectible codec.", typeCodec); Class[] proxyInterfaces = new Class[]{CollectibleCodec.class}; CollectibleCodec collectibleCodec = (CollectibleCodec) Proxy.newProxyInstance( PojoCodecProvider.class.getClassLoader(), proxyInterfaces, new CollectibleCodecDelegator(typeCodec)); return collectibleCodec; } } return codec; }
@Test public void testEntityWithId() { Codec<EntityWithId> entityWithIdCodec = codecRegistry.get(EntityWithId.class); Assert.assertFalse(entityWithIdCodec instanceof CollectibleCodec); }
@Test public void testEntityWithCollectibleId() { Codec<EntityWithCollectibleId> codec = codecRegistry.get(EntityWithCollectibleId.class); Assert.assertTrue(codec instanceof CollectibleCodec); }
@Test public void testEntityWithoutId() { Codec<EntityWithoutId> entityWithIdCodec = codecRegistry.get(EntityWithoutId.class); Assert.assertFalse(entityWithIdCodec instanceof CollectibleCodec); }