use of dev.morphia.mapping.codec.MorphiaInstanceCreator in project morphia by mongodb.
the class EntityDecoder method decode.
@Override
@SuppressWarnings("unchecked")
public T decode(BsonReader reader, DecoderContext decoderContext) {
T entity;
if (decoderContext.hasCheckedDiscriminator()) {
MorphiaInstanceCreator instanceCreator = getInstanceCreator();
decodeProperties(reader, decoderContext, instanceCreator, classModel);
return (T) instanceCreator.getInstance();
} else {
entity = getCodecFromDocument(reader, classModel.useDiscriminator(), classModel.getDiscriminatorKey(), morphiaCodec.getRegistry(), morphiaCodec.getDiscriminatorLookup(), morphiaCodec).decode(reader, DecoderContext.builder().checkedDiscriminator(true).build());
}
return entity;
}
use of dev.morphia.mapping.codec.MorphiaInstanceCreator in project morphia by mongodb.
the class LifecycleDecoder method decode.
@Override
@SuppressWarnings("unchecked")
public T decode(BsonReader reader, DecoderContext decoderContext) {
Document document = getMorphiaCodec().getRegistry().get(Document.class).decode(reader, decoderContext);
EntityModel model = getMorphiaCodec().getEntityModel();
if (model.useDiscriminator()) {
String discriminator = document.getString(model.getDiscriminatorKey());
if (discriminator != null) {
Class<?> discriminatorClass = getMorphiaCodec().getDiscriminatorLookup().lookup(discriminator);
// need to load the codec to initialize cachedCodecs in field models
Codec<?> codec = getMorphiaCodec().getRegistry().get(discriminatorClass);
if (codec instanceof MorphiaCodec) {
model = ((MorphiaCodec<?>) codec).getEntityModel();
} else {
throw new CodecConfigurationException(format("Non-entity class used as discriminator: '%s'.", discriminator));
}
}
}
final MorphiaInstanceCreator instanceCreator = model.getInstanceCreator();
T entity = (T) instanceCreator.getInstance();
model.callLifecycleMethods(PreLoad.class, entity, document, getMorphiaCodec().getDatastore());
decodeProperties(new DocumentReader(document), decoderContext, instanceCreator, model);
model.callLifecycleMethods(PostLoad.class, entity, document, getMorphiaCodec().getDatastore());
return entity;
}
Aggregations