Skip to content

Remove deprecated Parameterizable interface #1248

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

Merged
merged 2 commits into from
Nov 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 0 additions & 49 deletions bson/src/main/org/bson/codecs/Parameterizable.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ public interface CodecRegistry extends CodecProvider {
* @throws CodecConfigurationException if no codec can be found for the given class and type arguments.
* @throws AssertionError by default, if the implementation does not override this method, or if no codec can be found
* for the given class and type arguments.
* @see org.bson.codecs.Parameterizable
* @since 4.8
*/
default <T> Codec<T> get(Class<T> clazz, List<Type> typeArguments) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import java.util.List;

import static org.bson.assertions.Assertions.notNull;
import static org.bson.internal.ProvidersCodecRegistry.getFromCodecProvider;

final class OverridableUuidRepresentationCodecProvider implements CodecProvider {

Expand All @@ -44,7 +43,7 @@ public <T> Codec<T> get(final Class<T> clazz, final CodecRegistry registry) {

@Override
public <T> Codec<T> get(final Class<T> clazz, final List<Type> typeArguments, final CodecRegistry registry) {
Codec<T> codec = getFromCodecProvider(wrapped, clazz, typeArguments, registry);
Codec<T> codec = wrapped.get(clazz, typeArguments, registry);
if (codec instanceof OverridableUuidRepresentationCodec) {
@SuppressWarnings("unchecked")
Codec<T> codecWithUuidRepresentation = ((OverridableUuidRepresentationCodec<T>) codec).withUuidRepresentation(uuidRepresentation);
Expand Down
19 changes: 2 additions & 17 deletions bson/src/main/org/bson/internal/ProvidersCodecRegistry.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import org.bson.codecs.configuration.CodecRegistry;
import org.bson.internal.CodecCache.CodecCacheKey;

import javax.annotation.Nullable;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Collections;
Expand Down Expand Up @@ -67,7 +66,7 @@ public <T> Codec<T> get(final Class<T> clazz, final CodecRegistry registry) {
@Override
public <T> Codec<T> get(final Class<T> clazz, final List<Type> typeArguments, final CodecRegistry registry) {
for (CodecProvider provider : codecProviders) {
Codec<T> codec = getFromCodecProvider(provider, clazz, typeArguments, registry);
Codec<T> codec = provider.get(clazz, typeArguments, registry);
if (codec != null) {
return codec;
}
Expand All @@ -79,7 +78,7 @@ public <T> Codec<T> get(final ChildCodecRegistry<T> context) {
CodecCacheKey codecCacheKey = new CodecCacheKey(context.getCodecClass(), context.getTypes().orElse(null));
return codecCache.<T>get(codecCacheKey).orElseGet(() -> {
for (CodecProvider provider : codecProviders) {
Codec<T> codec = getFromCodecProvider(provider, context.getCodecClass(), context.getTypes().orElse(emptyList()), context);
Codec<T> codec = provider.get(context.getCodecClass(), context.getTypes().orElse(emptyList()), context);
if (codec != null) {
return codecCache.putIfAbsent(codecCacheKey, codec);
}
Expand All @@ -88,20 +87,6 @@ public <T> Codec<T> get(final ChildCodecRegistry<T> context) {
});
}

@Nullable
@SuppressWarnings("deprecation")
public static <T> Codec<T> getFromCodecProvider(final CodecProvider provider,
final Class<T> clazz, final List<Type> typeArguments, final CodecRegistry registry) {
Codec<T> codec = provider.get(clazz, typeArguments, registry);
// `Parameterizable` is deprecated, but we still have to support it until it is removed
if (codec instanceof org.bson.codecs.Parameterizable && !typeArguments.isEmpty()) {
@SuppressWarnings("unchecked")
Codec<T> parameterizedCodec = (Codec<T>) ((org.bson.codecs.Parameterizable) codec).parameterize(registry, typeArguments);
codec = parameterizedCodec;
}
return codec;
}

@Override
public boolean equals(final Object o) {
if (this == o) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,7 @@ import org.bson.ByteBufNIO
import org.bson.codecs.Codec
import org.bson.codecs.DecoderContext
import org.bson.codecs.EncoderContext
import org.bson.codecs.IntegerCodec
import org.bson.codecs.MinKeyCodec
import org.bson.codecs.Parameterizable
import org.bson.codecs.ValueCodecProvider
import org.bson.codecs.configuration.CodecConfigurationException
import org.bson.codecs.configuration.CodecProvider
import org.bson.codecs.configuration.CodecRegistry
Expand All @@ -38,12 +35,9 @@ import org.bson.types.MaxKey
import org.bson.types.MinKey
import spock.lang.Specification

import java.lang.reflect.Type
import java.nio.ByteBuffer

import static java.util.Arrays.asList
import static org.bson.codecs.ContainerCodecHelper.getCodec
import static org.bson.codecs.configuration.CodecRegistries.fromCodecs

class ProvidersCodecRegistrySpecification extends Specification {

Expand Down Expand Up @@ -169,93 +163,6 @@ class ProvidersCodecRegistrySpecification extends Specification {
expect:
((SimpleCodec) provider.get(Simple, registry)).registry.is(registry)
}

def 'should parameterize codec'() {
given:
def registry = new ProvidersCodecRegistry([fromCodecs(new CollectionCodec()), new ValueCodecProvider()])

when:
def codec = registry.get(Collection, [Integer])

then:
codec instanceof ParameterizedCollectionCodec
(codec as ParameterizedCollectionCodec).getCodec() instanceof IntegerCodec

when:
def secondCodec = registry.get(Collection, [Integer])

then:
codec == secondCodec
}

def 'should parameterize codec with cycles'() {
given:
def registry = new ProvidersCodecRegistry([fromCodecs(new CollectionCodec()), new ValueCodecProvider()])

when:
def codec = registry.get(Collection, [Holder.getField('c').getGenericType()])

then:
codec instanceof ParameterizedCollectionCodec
(codec as ParameterizedCollectionCodec).getCodec() instanceof LazyCodec

when:
def secondCodec = registry.get(Collection, [Holder.getField('c').getGenericType()])

then:
codec == secondCodec
}
}

class CollectionCodec implements Codec<Collection<?>>, Parameterizable {

@Override
Collection<?> decode(BsonReader reader, DecoderContext decoderContext) {
throw new UnsupportedOperationException()
}

@Override
void encode(BsonWriter writer, Collection<?> value, EncoderContext encoderContext) {
throw new UnsupportedOperationException()
}

@Override
Class<Collection<?>> getEncoderClass() {
Collection
}

@Override
Codec<?> parameterize(CodecRegistry codecRegistry, List<Type> types) {
new ParameterizedCollectionCodec(getCodec(codecRegistry, types.get(0)))
}
}

class ParameterizedCollectionCodec<T> implements Codec<Collection<T>> {

private final Codec<T> codec

ParameterizedCollectionCodec(Codec<T> codec) {
this.codec = codec
}

Codec<T> getCodec() {
codec
}

@Override
Collection<T> decode(BsonReader reader, DecoderContext decoderContext) {
throw new UnsupportedOperationException()
}

@Override
void encode(BsonWriter writer, Collection<T> value, EncoderContext encoderContext) {
throw new UnsupportedOperationException()
}

@Override
Class<Collection<T>> getEncoderClass() {
Collection
}
}

class SingleCodecProvider implements CodecProvider {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import java.util.List;

import static com.mongodb.internal.VisibleForTesting.AccessModifier.PRIVATE;
import static org.bson.internal.ProvidersCodecRegistry.getFromCodecProvider;


/**
Expand Down Expand Up @@ -66,7 +65,7 @@ public <T> Codec<T> get(final Class<T> clazz, final CodecRegistry registry) {
@Override
@Nullable
public <T> Codec<T> get(final Class<T> clazz, final List<Type> typeArguments, final CodecRegistry registry) {
return RECORD_CODEC_PROVIDER != null ? getFromCodecProvider(RECORD_CODEC_PROVIDER, clazz, typeArguments, registry) : null;
return RECORD_CODEC_PROVIDER != null ? RECORD_CODEC_PROVIDER.get(clazz, typeArguments, registry) : null;
}

/**
Expand Down
6 changes: 2 additions & 4 deletions driver-core/src/main/com/mongodb/KotlinCodecProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@
import java.util.Collections;
import java.util.List;

import static org.bson.internal.ProvidersCodecRegistry.getFromCodecProvider;

/**
* A CodecProvider for Kotlin data classes.
* Delegates to {@code org.bson.codecs.kotlinx.KotlinSerializerCodecProvider}
Expand Down Expand Up @@ -76,11 +74,11 @@ public <T> Codec<T> get(final Class<T> clazz, final CodecRegistry registry) {
public <T> Codec<T> get(final Class<T> clazz, final List<Type> typeArguments, final CodecRegistry registry) {
Codec<T> codec = null;
if (KOTLIN_SERIALIZABLE_CODEC_PROVIDER != null) {
codec = getFromCodecProvider(KOTLIN_SERIALIZABLE_CODEC_PROVIDER, clazz, typeArguments, registry);
codec = KOTLIN_SERIALIZABLE_CODEC_PROVIDER.get(clazz, typeArguments, registry);
}

if (codec == null && DATA_CLASS_CODEC_PROVIDER != null) {
codec = getFromCodecProvider(DATA_CLASS_CODEC_PROVIDER, clazz, typeArguments, registry);
codec = DATA_CLASS_CODEC_PROVIDER.get(clazz, typeArguments, registry);
}
return codec;
}
Expand Down