Skip to content

Commit bea8ecf

Browse files
committed
Remove deprecated record annotations (#1249)
JAVA-5141
1 parent ac603f3 commit bea8ecf

File tree

7 files changed

+24
-235
lines changed

7 files changed

+24
-235
lines changed

bson-record-codec/src/main/org/bson/codecs/record/RecordCodec.java

+20-24
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,13 @@
2626
import org.bson.codecs.RepresentationConfigurable;
2727
import org.bson.codecs.configuration.CodecConfigurationException;
2828
import org.bson.codecs.configuration.CodecRegistry;
29-
import org.bson.codecs.record.annotations.BsonId;
30-
import org.bson.codecs.record.annotations.BsonProperty;
31-
import org.bson.codecs.record.annotations.BsonRepresentation;
29+
import org.bson.codecs.pojo.annotations.BsonCreator;
30+
import org.bson.codecs.pojo.annotations.BsonDiscriminator;
31+
import org.bson.codecs.pojo.annotations.BsonExtraElements;
32+
import org.bson.codecs.pojo.annotations.BsonId;
33+
import org.bson.codecs.pojo.annotations.BsonIgnore;
34+
import org.bson.codecs.pojo.annotations.BsonProperty;
35+
import org.bson.codecs.pojo.annotations.BsonRepresentation;
3236
import org.bson.diagnostics.Logger;
3337
import org.bson.diagnostics.Loggers;
3438

@@ -87,7 +91,6 @@ Object getValue(final Record record) throws InvocationTargetException, IllegalAc
8791
return component.getAccessor().invoke(record);
8892
}
8993

90-
@SuppressWarnings("deprecation")
9194
private static Codec<?> computeCodec(final List<Type> typeParameters, final RecordComponent component,
9295
final CodecRegistry codecRegistry) {
9396
var rawType = toWrapper(resolveComponentType(typeParameters, component));
@@ -97,11 +100,9 @@ private static Codec<?> computeCodec(final List<Type> typeParameters, final Reco
97100
: codecRegistry.get(rawType);
98101
BsonType bsonRepresentationType = null;
99102

100-
if (component.isAnnotationPresent(BsonRepresentation.class)) {
101-
bsonRepresentationType = component.getAnnotation(BsonRepresentation.class).value();
102-
} else if (isAnnotationPresentOnField(component, org.bson.codecs.pojo.annotations.BsonRepresentation.class)) {
103+
if (isAnnotationPresentOnField(component, BsonRepresentation.class)) {
103104
bsonRepresentationType = getAnnotationOnField(component,
104-
org.bson.codecs.pojo.annotations.BsonRepresentation.class).value();
105+
BsonRepresentation.class).value();
105106
}
106107
if (bsonRepresentationType != null) {
107108
if (codec instanceof RepresentationConfigurable<?> representationConfigurable) {
@@ -145,16 +146,11 @@ private static int getIndexOfTypeParameter(final String typeParameterName, final
145146
recordClass.getName(), typeParameterName));
146147
}
147148

148-
@SuppressWarnings("deprecation")
149149
private static String computeFieldName(final RecordComponent component) {
150-
if (component.isAnnotationPresent(BsonId.class)) {
150+
if (isAnnotationPresentOnField(component, BsonId.class)) {
151151
return "_id";
152-
} else if (isAnnotationPresentOnField(component, org.bson.codecs.pojo.annotations.BsonId.class)) {
153-
return "_id";
154-
} else if (component.isAnnotationPresent(BsonProperty.class)) {
155-
return component.getAnnotation(BsonProperty.class).value();
156-
} else if (isAnnotationPresentOnField(component, org.bson.codecs.pojo.annotations.BsonProperty.class)) {
157-
return getAnnotationOnField(component, org.bson.codecs.pojo.annotations.BsonProperty.class).value();
152+
} else if (isAnnotationPresentOnField(component, BsonProperty.class)) {
153+
return getAnnotationOnField(component, BsonProperty.class).value();
158154
}
159155
return component.getName();
160156
}
@@ -182,14 +178,14 @@ private static <T extends Annotation> T getAnnotationOnField(final RecordCompone
182178
}
183179

184180
private static void validateAnnotations(final RecordComponent component, final int index) {
185-
validateAnnotationNotPresentOnType(component.getDeclaringRecord(), org.bson.codecs.pojo.annotations.BsonDiscriminator.class);
186-
validateAnnotationNotPresentOnConstructor(component.getDeclaringRecord(), org.bson.codecs.pojo.annotations.BsonCreator.class);
187-
validateAnnotationNotPresentOnMethod(component.getDeclaringRecord(), org.bson.codecs.pojo.annotations.BsonCreator.class);
188-
validateAnnotationNotPresentOnFieldOrAccessor(component, org.bson.codecs.pojo.annotations.BsonIgnore.class);
189-
validateAnnotationNotPresentOnFieldOrAccessor(component, org.bson.codecs.pojo.annotations.BsonExtraElements.class);
190-
validateAnnotationOnlyOnField(component, index, org.bson.codecs.pojo.annotations.BsonId.class);
191-
validateAnnotationOnlyOnField(component, index, org.bson.codecs.pojo.annotations.BsonProperty.class);
192-
validateAnnotationOnlyOnField(component, index, org.bson.codecs.pojo.annotations.BsonRepresentation.class);
181+
validateAnnotationNotPresentOnType(component.getDeclaringRecord(), BsonDiscriminator.class);
182+
validateAnnotationNotPresentOnConstructor(component.getDeclaringRecord(), BsonCreator.class);
183+
validateAnnotationNotPresentOnMethod(component.getDeclaringRecord(), BsonCreator.class);
184+
validateAnnotationNotPresentOnFieldOrAccessor(component, BsonIgnore.class);
185+
validateAnnotationNotPresentOnFieldOrAccessor(component, BsonExtraElements.class);
186+
validateAnnotationOnlyOnField(component, index, BsonId.class);
187+
validateAnnotationOnlyOnField(component, index, BsonProperty.class);
188+
validateAnnotationOnlyOnField(component, index, BsonRepresentation.class);
193189
}
194190

195191
private static <T extends Annotation> void validateAnnotationNotPresentOnType(final Class<?> clazz,

bson-record-codec/src/main/org/bson/codecs/record/annotations/BsonId.java

-36
This file was deleted.

bson-record-codec/src/main/org/bson/codecs/record/annotations/BsonProperty.java

-42
This file was deleted.

bson-record-codec/src/main/org/bson/codecs/record/annotations/BsonRepresentation.java

-44
This file was deleted.

bson-record-codec/src/main/org/bson/codecs/record/annotations/package-info.java

-20
This file was deleted.

bson-record-codec/src/test/unit/org/bson/codecs/record/RecordCodecTest.java

+4-33
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
import org.bson.codecs.configuration.CodecRegistry;
3333
import org.bson.codecs.record.samples.TestRecordEmbedded;
3434
import org.bson.codecs.record.samples.TestRecordParameterized;
35-
import org.bson.codecs.record.samples.TestRecordWithDeprecatedAnnotations;
3635
import org.bson.codecs.record.samples.TestRecordWithIllegalBsonCreatorOnConstructor;
3736
import org.bson.codecs.record.samples.TestRecordWithIllegalBsonCreatorOnMethod;
3837
import org.bson.codecs.record.samples.TestRecordWithIllegalBsonDiscriminatorOnRecord;
@@ -69,34 +68,6 @@
6968

7069
public class RecordCodecTest {
7170

72-
@Test
73-
public void testRecordWithDeprecatedAnnotations() {
74-
var codec = createRecordCodec(TestRecordWithDeprecatedAnnotations.class, Bson.DEFAULT_CODEC_REGISTRY);
75-
var identifier = new ObjectId();
76-
var testRecord = new TestRecordWithDeprecatedAnnotations("Lucas", 14, List.of("soccer", "basketball"), identifier.toHexString());
77-
78-
var document = new BsonDocument();
79-
var writer = new BsonDocumentWriter(document);
80-
81-
// when
82-
codec.encode(writer, testRecord, EncoderContext.builder().build());
83-
84-
// then
85-
assertEquals(
86-
new BsonDocument("_id", new BsonObjectId(identifier))
87-
.append("name", new BsonString("Lucas"))
88-
.append("hobbies", new BsonArray(List.of(new BsonString("soccer"), new BsonString("basketball"))))
89-
.append("a", new BsonInt32(14)),
90-
document);
91-
assertEquals("_id", document.getFirstKey());
92-
93-
// when
94-
var decoded = codec.decode(new BsonDocumentReader(document), DecoderContext.builder().build());
95-
96-
// then
97-
assertEquals(testRecord, decoded);
98-
}
99-
10071
@Test
10172
public void testRecordWithPojoAnnotations() {
10273
var codec = createRecordCodec(TestRecordWithPojoAnnotations.class, Bson.DEFAULT_CODEC_REGISTRY);
@@ -305,9 +276,9 @@ public void testRecordWithNestedParameterizedRecordWithDifferentlyOrderedTypePar
305276

306277
@Test
307278
public void testRecordWithNulls() {
308-
var codec = createRecordCodec(TestRecordWithDeprecatedAnnotations.class, Bson.DEFAULT_CODEC_REGISTRY);
279+
var codec = createRecordCodec(TestRecordWithPojoAnnotations.class, Bson.DEFAULT_CODEC_REGISTRY);
309280
var identifier = new ObjectId();
310-
var testRecord = new TestRecordWithDeprecatedAnnotations(null, 14, null, identifier.toHexString());
281+
var testRecord = new TestRecordWithPojoAnnotations(null, 14, null, identifier.toHexString());
311282

312283
var document = new BsonDocument();
313284
var writer = new BsonDocumentWriter(document);
@@ -359,9 +330,9 @@ public void testExceptionsWithStoredNullsOnPrimitiveField() {
359330

360331
@Test
361332
public void testRecordWithExtraData() {
362-
var codec = createRecordCodec(TestRecordWithDeprecatedAnnotations.class, Bson.DEFAULT_CODEC_REGISTRY);
333+
var codec = createRecordCodec(TestRecordWithPojoAnnotations.class, Bson.DEFAULT_CODEC_REGISTRY);
363334
var identifier = new ObjectId();
364-
var testRecord = new TestRecordWithDeprecatedAnnotations("Felix", 13, List.of("rugby", "badminton"), identifier.toHexString());
335+
var testRecord = new TestRecordWithPojoAnnotations("Felix", 13, List.of("rugby", "badminton"), identifier.toHexString());
365336

366337
var document = new BsonDocument("_id", new BsonObjectId(identifier))
367338
.append("nationality", new BsonString("British"))

bson-record-codec/src/test/unit/org/bson/codecs/record/samples/TestRecordWithDeprecatedAnnotations.java

-36
This file was deleted.

0 commit comments

Comments
 (0)