From a5644e77bafdd6d2a5a859f93513a6d83e11c3a1 Mon Sep 17 00:00:00 2001 From: Jeff Yemin Date: Wed, 25 Oct 2023 23:20:17 -0400 Subject: [PATCH] Remove deprecated record annotations JAVA-5141 --- .../org/bson/codecs/record/RecordCodec.java | 44 +++++++++---------- .../codecs/record/annotations/BsonId.java | 36 --------------- .../record/annotations/BsonProperty.java | 42 ------------------ .../annotations/BsonRepresentation.java | 44 ------------------- .../record/annotations/package-info.java | 20 --------- .../bson/codecs/record/RecordCodecTest.java | 37 ++-------------- .../TestRecordWithDeprecatedAnnotations.java | 36 --------------- 7 files changed, 24 insertions(+), 235 deletions(-) delete mode 100644 bson-record-codec/src/main/org/bson/codecs/record/annotations/BsonId.java delete mode 100644 bson-record-codec/src/main/org/bson/codecs/record/annotations/BsonProperty.java delete mode 100644 bson-record-codec/src/main/org/bson/codecs/record/annotations/BsonRepresentation.java delete mode 100644 bson-record-codec/src/main/org/bson/codecs/record/annotations/package-info.java delete mode 100644 bson-record-codec/src/test/unit/org/bson/codecs/record/samples/TestRecordWithDeprecatedAnnotations.java diff --git a/bson-record-codec/src/main/org/bson/codecs/record/RecordCodec.java b/bson-record-codec/src/main/org/bson/codecs/record/RecordCodec.java index 8a304760f31..464609e6693 100644 --- a/bson-record-codec/src/main/org/bson/codecs/record/RecordCodec.java +++ b/bson-record-codec/src/main/org/bson/codecs/record/RecordCodec.java @@ -25,9 +25,13 @@ import org.bson.codecs.RepresentationConfigurable; import org.bson.codecs.configuration.CodecConfigurationException; import org.bson.codecs.configuration.CodecRegistry; -import org.bson.codecs.record.annotations.BsonId; -import org.bson.codecs.record.annotations.BsonProperty; -import org.bson.codecs.record.annotations.BsonRepresentation; +import org.bson.codecs.pojo.annotations.BsonCreator; +import org.bson.codecs.pojo.annotations.BsonDiscriminator; +import org.bson.codecs.pojo.annotations.BsonExtraElements; +import org.bson.codecs.pojo.annotations.BsonId; +import org.bson.codecs.pojo.annotations.BsonIgnore; +import org.bson.codecs.pojo.annotations.BsonProperty; +import org.bson.codecs.pojo.annotations.BsonRepresentation; import org.bson.diagnostics.Logger; import org.bson.diagnostics.Loggers; @@ -84,7 +88,6 @@ Object getValue(final Record record) throws InvocationTargetException, IllegalAc return component.getAccessor().invoke(record); } - @SuppressWarnings("deprecation") private static Codec computeCodec(final List typeParameters, final RecordComponent component, final CodecRegistry codecRegistry) { var rawType = toWrapper(resolveComponentType(typeParameters, component)); @@ -94,11 +97,9 @@ private static Codec computeCodec(final List typeParameters, final Reco : codecRegistry.get(rawType); BsonType bsonRepresentationType = null; - if (component.isAnnotationPresent(BsonRepresentation.class)) { - bsonRepresentationType = component.getAnnotation(BsonRepresentation.class).value(); - } else if (isAnnotationPresentOnField(component, org.bson.codecs.pojo.annotations.BsonRepresentation.class)) { + if (isAnnotationPresentOnField(component, BsonRepresentation.class)) { bsonRepresentationType = getAnnotationOnField(component, - org.bson.codecs.pojo.annotations.BsonRepresentation.class).value(); + BsonRepresentation.class).value(); } if (bsonRepresentationType != null) { if (codec instanceof RepresentationConfigurable representationConfigurable) { @@ -142,16 +143,11 @@ private static int getIndexOfTypeParameter(final String typeParameterName, final recordClass.getName(), typeParameterName)); } - @SuppressWarnings("deprecation") private static String computeFieldName(final RecordComponent component) { - if (component.isAnnotationPresent(BsonId.class)) { + if (isAnnotationPresentOnField(component, BsonId.class)) { return "_id"; - } else if (isAnnotationPresentOnField(component, org.bson.codecs.pojo.annotations.BsonId.class)) { - return "_id"; - } else if (component.isAnnotationPresent(BsonProperty.class)) { - return component.getAnnotation(BsonProperty.class).value(); - } else if (isAnnotationPresentOnField(component, org.bson.codecs.pojo.annotations.BsonProperty.class)) { - return getAnnotationOnField(component, org.bson.codecs.pojo.annotations.BsonProperty.class).value(); + } else if (isAnnotationPresentOnField(component, BsonProperty.class)) { + return getAnnotationOnField(component, BsonProperty.class).value(); } return component.getName(); } @@ -179,14 +175,14 @@ private static T getAnnotationOnField(final RecordCompone } private static void validateAnnotations(final RecordComponent component, final int index) { - validateAnnotationNotPresentOnType(component.getDeclaringRecord(), org.bson.codecs.pojo.annotations.BsonDiscriminator.class); - validateAnnotationNotPresentOnConstructor(component.getDeclaringRecord(), org.bson.codecs.pojo.annotations.BsonCreator.class); - validateAnnotationNotPresentOnMethod(component.getDeclaringRecord(), org.bson.codecs.pojo.annotations.BsonCreator.class); - validateAnnotationNotPresentOnFieldOrAccessor(component, org.bson.codecs.pojo.annotations.BsonIgnore.class); - validateAnnotationNotPresentOnFieldOrAccessor(component, org.bson.codecs.pojo.annotations.BsonExtraElements.class); - validateAnnotationOnlyOnField(component, index, org.bson.codecs.pojo.annotations.BsonId.class); - validateAnnotationOnlyOnField(component, index, org.bson.codecs.pojo.annotations.BsonProperty.class); - validateAnnotationOnlyOnField(component, index, org.bson.codecs.pojo.annotations.BsonRepresentation.class); + validateAnnotationNotPresentOnType(component.getDeclaringRecord(), BsonDiscriminator.class); + validateAnnotationNotPresentOnConstructor(component.getDeclaringRecord(), BsonCreator.class); + validateAnnotationNotPresentOnMethod(component.getDeclaringRecord(), BsonCreator.class); + validateAnnotationNotPresentOnFieldOrAccessor(component, BsonIgnore.class); + validateAnnotationNotPresentOnFieldOrAccessor(component, BsonExtraElements.class); + validateAnnotationOnlyOnField(component, index, BsonId.class); + validateAnnotationOnlyOnField(component, index, BsonProperty.class); + validateAnnotationOnlyOnField(component, index, BsonRepresentation.class); } private static void validateAnnotationNotPresentOnType(final Class clazz, diff --git a/bson-record-codec/src/main/org/bson/codecs/record/annotations/BsonId.java b/bson-record-codec/src/main/org/bson/codecs/record/annotations/BsonId.java deleted file mode 100644 index 24a409dc3a5..00000000000 --- a/bson-record-codec/src/main/org/bson/codecs/record/annotations/BsonId.java +++ /dev/null @@ -1,36 +0,0 @@ -/* - * Copyright 2008-present MongoDB, Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.bson.codecs.record.annotations; - -import java.lang.annotation.Documented; -import java.lang.annotation.ElementType; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; - -/** - * An annotation that configures the record component as the _id field of the document - * - * @since 4.6 - * @deprecated Prefer {@link org.bson.codecs.pojo.annotations.BsonId} - */ -@Deprecated -@Documented -@Retention(RetentionPolicy.RUNTIME) -@Target({ElementType.RECORD_COMPONENT}) -public @interface BsonId { -} diff --git a/bson-record-codec/src/main/org/bson/codecs/record/annotations/BsonProperty.java b/bson-record-codec/src/main/org/bson/codecs/record/annotations/BsonProperty.java deleted file mode 100644 index 428584d6539..00000000000 --- a/bson-record-codec/src/main/org/bson/codecs/record/annotations/BsonProperty.java +++ /dev/null @@ -1,42 +0,0 @@ -/* - * Copyright 2008-present MongoDB, Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.bson.codecs.record.annotations; - -import java.lang.annotation.Documented; -import java.lang.annotation.ElementType; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; - -/** - * An annotation that configures a record component. - * - * @since 4.6 - * @deprecated Prefer {@link org.bson.codecs.pojo.annotations.BsonProperty} - */ -@Deprecated -@Documented -@Target({ElementType.RECORD_COMPONENT}) -@Retention(RetentionPolicy.RUNTIME) -public @interface BsonProperty { - /** - * The field name of the record component. - * - * @return the field name to use for the record component - */ - String value() default ""; -} diff --git a/bson-record-codec/src/main/org/bson/codecs/record/annotations/BsonRepresentation.java b/bson-record-codec/src/main/org/bson/codecs/record/annotations/BsonRepresentation.java deleted file mode 100644 index 5838734fba5..00000000000 --- a/bson-record-codec/src/main/org/bson/codecs/record/annotations/BsonRepresentation.java +++ /dev/null @@ -1,44 +0,0 @@ -/* - * Copyright 2008-present MongoDB, Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.bson.codecs.record.annotations; - -import org.bson.BsonType; - -import java.lang.annotation.Documented; -import java.lang.annotation.ElementType; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; - -/** - * An annotation that specifies what type the record component is stored as in the database. - * - * @since 4.6 - * @deprecated Prefer {@link org.bson.codecs.pojo.annotations.BsonRepresentation} - */ -@Deprecated -@Documented -@Retention(RetentionPolicy.RUNTIME) -@Target({ElementType.RECORD_COMPONENT}) -public @interface BsonRepresentation { - /** - * The type that the property is stored as in the database. - * - * @return the type that the property should be stored as. - */ - BsonType value(); -} diff --git a/bson-record-codec/src/main/org/bson/codecs/record/annotations/package-info.java b/bson-record-codec/src/main/org/bson/codecs/record/annotations/package-info.java deleted file mode 100644 index 60bab08a860..00000000000 --- a/bson-record-codec/src/main/org/bson/codecs/record/annotations/package-info.java +++ /dev/null @@ -1,20 +0,0 @@ -/* - * Copyright 2008-present MongoDB, Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -/** - * This package contains annotations for encoding and decoding Java records. - */ -package org.bson.codecs.record.annotations; diff --git a/bson-record-codec/src/test/unit/org/bson/codecs/record/RecordCodecTest.java b/bson-record-codec/src/test/unit/org/bson/codecs/record/RecordCodecTest.java index 606bc68e59a..986ef753f69 100644 --- a/bson-record-codec/src/test/unit/org/bson/codecs/record/RecordCodecTest.java +++ b/bson-record-codec/src/test/unit/org/bson/codecs/record/RecordCodecTest.java @@ -30,7 +30,6 @@ import org.bson.codecs.configuration.CodecRegistry; import org.bson.codecs.record.samples.TestRecordEmbedded; import org.bson.codecs.record.samples.TestRecordParameterized; -import org.bson.codecs.record.samples.TestRecordWithDeprecatedAnnotations; import org.bson.codecs.record.samples.TestRecordWithIllegalBsonCreatorOnConstructor; import org.bson.codecs.record.samples.TestRecordWithIllegalBsonCreatorOnMethod; import org.bson.codecs.record.samples.TestRecordWithIllegalBsonDiscriminatorOnRecord; @@ -66,34 +65,6 @@ public class RecordCodecTest { - @Test - public void testRecordWithDeprecatedAnnotations() { - var codec = createRecordCodec(TestRecordWithDeprecatedAnnotations.class, Bson.DEFAULT_CODEC_REGISTRY); - var identifier = new ObjectId(); - var testRecord = new TestRecordWithDeprecatedAnnotations("Lucas", 14, List.of("soccer", "basketball"), identifier.toHexString()); - - var document = new BsonDocument(); - var writer = new BsonDocumentWriter(document); - - // when - codec.encode(writer, testRecord, EncoderContext.builder().build()); - - // then - assertEquals( - new BsonDocument("_id", new BsonObjectId(identifier)) - .append("name", new BsonString("Lucas")) - .append("hobbies", new BsonArray(List.of(new BsonString("soccer"), new BsonString("basketball")))) - .append("a", new BsonInt32(14)), - document); - assertEquals("_id", document.getFirstKey()); - - // when - var decoded = codec.decode(new BsonDocumentReader(document), DecoderContext.builder().build()); - - // then - assertEquals(testRecord, decoded); - } - @Test public void testRecordWithPojoAnnotations() { var codec = createRecordCodec(TestRecordWithPojoAnnotations.class, Bson.DEFAULT_CODEC_REGISTRY); @@ -302,9 +273,9 @@ public void testRecordWithNestedParameterizedRecordWithDifferentlyOrderedTypePar @Test public void testRecordWithNulls() { - var codec = createRecordCodec(TestRecordWithDeprecatedAnnotations.class, Bson.DEFAULT_CODEC_REGISTRY); + var codec = createRecordCodec(TestRecordWithPojoAnnotations.class, Bson.DEFAULT_CODEC_REGISTRY); var identifier = new ObjectId(); - var testRecord = new TestRecordWithDeprecatedAnnotations(null, 14, null, identifier.toHexString()); + var testRecord = new TestRecordWithPojoAnnotations(null, 14, null, identifier.toHexString()); var document = new BsonDocument(); var writer = new BsonDocumentWriter(document); @@ -327,9 +298,9 @@ public void testRecordWithNulls() { @Test public void testRecordWithExtraData() { - var codec = createRecordCodec(TestRecordWithDeprecatedAnnotations.class, Bson.DEFAULT_CODEC_REGISTRY); + var codec = createRecordCodec(TestRecordWithPojoAnnotations.class, Bson.DEFAULT_CODEC_REGISTRY); var identifier = new ObjectId(); - var testRecord = new TestRecordWithDeprecatedAnnotations("Felix", 13, List.of("rugby", "badminton"), identifier.toHexString()); + var testRecord = new TestRecordWithPojoAnnotations("Felix", 13, List.of("rugby", "badminton"), identifier.toHexString()); var document = new BsonDocument("_id", new BsonObjectId(identifier)) .append("nationality", new BsonString("British")) diff --git a/bson-record-codec/src/test/unit/org/bson/codecs/record/samples/TestRecordWithDeprecatedAnnotations.java b/bson-record-codec/src/test/unit/org/bson/codecs/record/samples/TestRecordWithDeprecatedAnnotations.java deleted file mode 100644 index 851ef55efc5..00000000000 --- a/bson-record-codec/src/test/unit/org/bson/codecs/record/samples/TestRecordWithDeprecatedAnnotations.java +++ /dev/null @@ -1,36 +0,0 @@ -/* - * Copyright 2008-present MongoDB, Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.bson.codecs.record.samples; - -import org.bson.BsonType; -import org.bson.codecs.record.annotations.BsonId; -import org.bson.codecs.record.annotations.BsonProperty; -import org.bson.codecs.record.annotations.BsonRepresentation; - -import java.util.List; - -@SuppressWarnings("deprecation") -public record TestRecordWithDeprecatedAnnotations(String name, - @BsonProperty("a") int age, - List hobbies, - @BsonRepresentation(BsonType.OBJECT_ID) @BsonId String identifier) { - - // To test that the canonical constructor is always used for decoding - public TestRecordWithDeprecatedAnnotations(final String identifier) { - this("Adrian", 17, List.of("soccer", "music"), identifier); - } -}