-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Remove specialization-related conditional logic from PojoCodecImpl
by introducing LazyPropertyModelCodec.NeedsSpecializationCodec
#1136
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -50,36 +50,28 @@ final class PojoCodecImpl<T> extends PojoCodec<T> { | |
private final CodecRegistry registry; | ||
private final PropertyCodecRegistry propertyCodecRegistry; | ||
private final DiscriminatorLookup discriminatorLookup; | ||
private final boolean specialized; | ||
|
||
PojoCodecImpl(final ClassModel<T> classModel, final CodecRegistry codecRegistry, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks like the constructors can now be chained - ie. this one calls the below. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wish ... : one of them ( As far as I can see, only refactoring of the code around |
||
final List<PropertyCodecProvider> propertyCodecProviders, final DiscriminatorLookup discriminatorLookup) { | ||
final List<PropertyCodecProvider> propertyCodecProviders, final DiscriminatorLookup discriminatorLookup) { | ||
this.classModel = classModel; | ||
this.registry = codecRegistry; | ||
this.discriminatorLookup = discriminatorLookup; | ||
this.propertyCodecRegistry = new PropertyCodecRegistryImpl(this, registry, propertyCodecProviders); | ||
this.specialized = shouldSpecialize(classModel); | ||
specialize(); | ||
} | ||
|
||
PojoCodecImpl(final ClassModel<T> classModel, final CodecRegistry codecRegistry, final PropertyCodecRegistry propertyCodecRegistry, | ||
final DiscriminatorLookup discriminatorLookup, final boolean specialized) { | ||
PojoCodecImpl(final ClassModel<T> classModel, final CodecRegistry codecRegistry, | ||
final PropertyCodecRegistry propertyCodecRegistry, final DiscriminatorLookup discriminatorLookup) { | ||
this.classModel = classModel; | ||
this.registry = codecRegistry; | ||
this.discriminatorLookup = discriminatorLookup; | ||
this.propertyCodecRegistry = propertyCodecRegistry; | ||
this.specialized = specialized; | ||
specialize(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can this There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unless we want to duplicate a bunch of code, this can only be done if one of the constructors reuses the other one, which does not seem possible. |
||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
@Override | ||
public void encode(final BsonWriter writer, final T value, final EncoderContext encoderContext) { | ||
if (!specialized) { | ||
throw new CodecConfigurationException(format("%s contains generic types that have not been specialised.%n" | ||
+ "Top level classes with generic types are not supported by the PojoCodec.", classModel.getName())); | ||
} | ||
|
||
if (areEquivalentTypes(value.getClass(), classModel.getType())) { | ||
writer.writeStartDocument(); | ||
|
||
|
@@ -104,10 +96,6 @@ public void encode(final BsonWriter writer, final T value, final EncoderContext | |
@Override | ||
public T decode(final BsonReader reader, final DecoderContext decoderContext) { | ||
if (decoderContext.hasCheckedDiscriminator()) { | ||
if (!specialized) { | ||
throw new CodecConfigurationException(format("%s contains generic types that have not been specialised.%n" | ||
+ "Top level classes with generic types are not supported by the PojoCodec.", classModel.getName())); | ||
} | ||
InstanceCreator<T> instanceCreator = classModel.getInstanceCreator(); | ||
decodeProperties(reader, decoderContext, instanceCreator); | ||
return instanceCreator.getInstance(); | ||
|
@@ -264,15 +252,13 @@ private <S> void setPropertyValueBsonExtraElements(final InstanceCreator<T> inst | |
} | ||
|
||
private void specialize() { | ||
if (specialized) { | ||
classModel.getPropertyModels().forEach(this::cachePropertyModelCodec); | ||
} | ||
classModel.getPropertyModels().forEach(this::cachePropertyModelCodec); | ||
} | ||
|
||
private <S> void cachePropertyModelCodec(final PropertyModel<S> propertyModel) { | ||
rozza marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (propertyModel.getCachedCodec() == null) { | ||
Codec<S> codec = propertyModel.getCodec() != null ? propertyModel.getCodec() | ||
: new LazyPropertyModelCodec<>(propertyModel, registry, propertyCodecRegistry, discriminatorLookup); | ||
: new LazyPropertyModelCodec<>(propertyModel, registry, propertyCodecRegistry); | ||
propertyModel.cachedCodec(codec); | ||
} | ||
} | ||
|
@@ -328,21 +314,6 @@ private PropertyModel<?> getPropertyModelByWriteName(final ClassModel<T> classMo | |
return null; | ||
} | ||
|
||
private static <T> boolean shouldSpecialize(final ClassModel<T> classModel) { | ||
if (!classModel.hasTypeParameters()) { | ||
return true; | ||
} | ||
|
||
for (Map.Entry<String, TypeParameterMap> entry : classModel.getPropertyNameToTypeParameterMap().entrySet()) { | ||
TypeParameterMap typeParameterMap = entry.getValue(); | ||
PropertyModel<?> propertyModel = classModel.getPropertyModel(entry.getKey()); | ||
if (typeParameterMap.hasTypeParameters() && (propertyModel == null || propertyModel.getCodec() == null)) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
@Override | ||
DiscriminatorLookup getDiscriminatorLookup() { | ||
return discriminatorLookup; | ||
|
Uh oh!
There was an error while loading. Please reload this page.