Java 类org.bson.codecs.CollectibleCodec 实例源码

项目:polymorphia    文件:PojoCodecProvider.java   
@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;
}
项目:polymorphia    文件:CollectibleCodecTest.java   
@Test
public void testEntityWithId() {
    Codec<EntityWithId> entityWithIdCodec = codecRegistry.get(EntityWithId.class);
    Assert.assertFalse(entityWithIdCodec instanceof CollectibleCodec);
}
项目:polymorphia    文件:CollectibleCodecTest.java   
@Test
public void testEntityWithCollectibleId() {
    Codec<EntityWithCollectibleId> codec = codecRegistry.get(EntityWithCollectibleId.class);
    Assert.assertTrue(codec instanceof CollectibleCodec);
}
项目:polymorphia    文件:CollectibleCodecTest.java   
@Test
public void testEntityWithoutId() {
    Codec<EntityWithoutId> entityWithIdCodec = codecRegistry.get(EntityWithoutId.class);
    Assert.assertFalse(entityWithIdCodec instanceof CollectibleCodec);
}