Skip to content

Commit a59a124

Browse files
committed
PojoCodecProvider improvements for automatic / package based classes.
Logs a warning if a class being lookup is incompatible with the PojoCodec JAVA-2838
1 parent 27e5321 commit a59a124

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

bson/src/main/org/bson/codecs/pojo/PojoCodecProvider.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
import org.bson.codecs.Codec;
1919
import org.bson.codecs.configuration.CodecProvider;
2020
import org.bson.codecs.configuration.CodecRegistry;
21+
import org.bson.diagnostics.Logger;
22+
import org.bson.diagnostics.Loggers;
2123

2224
import java.util.ArrayList;
2325
import java.util.Collections;
@@ -27,6 +29,7 @@
2729
import java.util.Map;
2830
import java.util.Set;
2931

32+
import static java.lang.String.format;
3033
import static java.util.Arrays.asList;
3134
import static org.bson.assertions.Assertions.notNull;
3235

@@ -36,6 +39,7 @@
3639
* @since 3.5
3740
*/
3841
public final class PojoCodecProvider implements CodecProvider {
42+
static final Logger LOGGER = Loggers.getLogger("codecs.pojo");
3943
private final boolean automatic;
4044
private final Map<Class<?>, ClassModel<?>> classModels;
4145
private final Set<String> packages;
@@ -76,14 +80,15 @@ private <T> PojoCodec<T> getPojoCodec(final Class<T> clazz, final CodecRegistry
7680
} else if (automatic || (clazz.getPackage() != null && packages.contains(clazz.getPackage().getName()))) {
7781
try {
7882
classModel = createClassModel(clazz, conventions);
79-
} catch (IllegalStateException e) {
80-
return null;
81-
}
82-
if (!clazz.isInterface() && classModel.getPropertyModels().isEmpty()) {
83+
if (!clazz.isInterface() && classModel.getPropertyModels().isEmpty()) {
84+
return null;
85+
}
86+
discriminatorLookup.addClassModel(classModel);
87+
return new AutomaticPojoCodec<T>(new PojoCodecImpl<T>(classModel, registry, propertyCodecProviders, discriminatorLookup));
88+
} catch (Exception e) {
89+
LOGGER.warn(format("Cannot use '%s' with the PojoCodec.", clazz.getSimpleName()), e);
8390
return null;
8491
}
85-
discriminatorLookup.addClassModel(classModel);
86-
return new AutomaticPojoCodec<T>(new PojoCodecImpl<T>(classModel, registry, propertyCodecProviders, discriminatorLookup));
8792
}
8893
return null;
8994
}

bson/src/test/unit/org/bson/codecs/pojo/PojoCodecProviderTest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import org.bson.codecs.ValueCodecProvider;
2121
import org.bson.codecs.configuration.CodecRegistry;
2222
import org.bson.codecs.pojo.entities.SimpleModel;
23+
import org.bson.codecs.pojo.entities.conventions.CreatorInvalidMethodModel;
2324
import org.junit.Test;
2425

2526
import static org.bson.codecs.configuration.CodecRegistries.fromProviders;
@@ -60,4 +61,12 @@ public void testAutomaticNoProperty() {
6061
assertNull(codec);
6162
}
6263

64+
@Test
65+
public void testAutomaticInvalidModel() {
66+
PojoCodecProvider provider = PojoCodecProvider.builder().automatic(true).build();
67+
CodecRegistry registry = fromProviders(provider, new ValueCodecProvider());
68+
Codec<CreatorInvalidMethodModel> codec = provider.get(CreatorInvalidMethodModel.class, registry);
69+
assertNull(codec);
70+
}
71+
6372
}

0 commit comments

Comments
 (0)