From 429a93d5fdc1f0a05d6ff3a683744965b6bbf728 Mon Sep 17 00:00:00 2001 From: Mark Hansen Date: Tue, 8 Oct 2024 15:53:33 -0700 Subject: [PATCH] Remove some rawtypes warning suppressions in ArrayDecoders I think all these generics only change things at compile-time, not at runtime. So should be safe. It's not a huge win; there's still some unchecked casts. But I was able to remove some unchecked casts, and some warning suppressions, so it's incrementally better. PiperOrigin-RevId: 683793374 --- .../com/google/protobuf/ArrayDecoders.java | 45 +++++++++---------- .../com/google/protobuf/MessageSchema.java | 2 +- 2 files changed, 22 insertions(+), 25 deletions(-) diff --git a/java/core/src/main/java/com/google/protobuf/ArrayDecoders.java b/java/core/src/main/java/com/google/protobuf/ArrayDecoders.java index 94cbafc895..f6b9b67b54 100644 --- a/java/core/src/main/java/com/google/protobuf/ArrayDecoders.java +++ b/java/core/src/main/java/com/google/protobuf/ArrayDecoders.java @@ -215,10 +215,10 @@ final class ArrayDecoders { } /** Decodes a message value. */ - @SuppressWarnings({"unchecked", "rawtypes"}) - static int decodeMessageField( - Schema schema, byte[] data, int position, int limit, Registers registers) throws IOException { - Object msg = schema.newInstance(); + static int decodeMessageField( + Schema schema, byte[] data, int position, int limit, Registers registers) + throws IOException { + T msg = schema.newInstance(); int offset = mergeMessageField(msg, schema, data, position, limit, registers); schema.makeImmutable(msg); registers.object1 = msg; @@ -226,20 +226,19 @@ final class ArrayDecoders { } /** Decodes a group value. */ - @SuppressWarnings({"unchecked", "rawtypes"}) - static int decodeGroupField( - Schema schema, byte[] data, int position, int limit, int endGroup, Registers registers) + static int decodeGroupField( + Schema schema, byte[] data, int position, int limit, int endGroup, Registers registers) throws IOException { - Object msg = schema.newInstance(); + T msg = schema.newInstance(); int offset = mergeGroupField(msg, schema, data, position, limit, endGroup, registers); schema.makeImmutable(msg); registers.object1 = msg; return offset; } - @SuppressWarnings({"unchecked", "rawtypes"}) - static int mergeMessageField( - Object msg, Schema schema, byte[] data, int position, int limit, Registers registers) + @SuppressWarnings("unchecked") + static int mergeMessageField( + Object msg, Schema schema, byte[] data, int position, int limit, Registers registers) throws IOException { int length = data[position++]; if (length < 0) { @@ -251,16 +250,16 @@ final class ArrayDecoders { } registers.recursionDepth++; checkRecursionLimit(registers.recursionDepth); - schema.mergeFrom(msg, data, position, position + length, registers); + schema.mergeFrom((T) msg, data, position, position + length, registers); registers.recursionDepth--; registers.object1 = msg; return position + length; } - @SuppressWarnings({"unchecked", "rawtypes"}) - static int mergeGroupField( + @SuppressWarnings("unchecked") + static int mergeGroupField( Object msg, - Schema schema, + Schema schema, byte[] data, int position, int limit, @@ -269,11 +268,11 @@ final class ArrayDecoders { throws IOException { // A group field must has a MessageSchema (the only other subclass of Schema is MessageSetSchema // and it can't be used in group fields). - final MessageSchema messageSchema = (MessageSchema) schema; + final MessageSchema messageSchema = (MessageSchema) schema; registers.recursionDepth++; checkRecursionLimit(registers.recursionDepth); final int endPosition = - messageSchema.parseMessage(msg, data, position, limit, endGroup, registers); + messageSchema.parseMessage((T) msg, data, position, limit, endGroup, registers); registers.recursionDepth--; registers.object1 = msg; return endPosition; @@ -758,17 +757,15 @@ final class ArrayDecoders { * * @return The position of after read all groups */ - @SuppressWarnings({"unchecked", "rawtypes"}) static int decodeGroupList( - Schema schema, + Schema schema, int tag, byte[] data, int position, int limit, - ProtobufList list, + ProtobufList output, Registers registers) throws IOException { - final ProtobufList output = (ProtobufList) list; final int endgroup = (tag & ~0x7) | WireFormat.WIRETYPE_END_GROUP; position = decodeGroupField(schema, data, position, limit, endgroup, registers); output.add(registers.object1); @@ -791,7 +788,7 @@ final class ArrayDecoders { Registers registers) throws IOException { final int number = tag >>> 3; - GeneratedMessageLite.GeneratedExtension extension = + GeneratedMessageLite.GeneratedExtension extension = registers.extensionRegistry.findLiteExtensionByNumber(defaultInstance, number); if (extension == null) { return decodeUnknownField( @@ -972,7 +969,7 @@ final class ArrayDecoders { case GROUP: { final int endTag = (fieldNumber << 3) | WireFormat.WIRETYPE_END_GROUP; - final Schema fieldSchema = + final Schema fieldSchema = Protobuf.getInstance() .schemaFor(extension.getMessageDefaultInstance().getClass()); if (extension.isRepeated()) { @@ -992,7 +989,7 @@ final class ArrayDecoders { } case MESSAGE: { - final Schema fieldSchema = + final Schema fieldSchema = Protobuf.getInstance() .schemaFor(extension.getMessageDefaultInstance().getClass()); if (extension.isRepeated()) { diff --git a/java/core/src/main/java/com/google/protobuf/MessageSchema.java b/java/core/src/main/java/com/google/protobuf/MessageSchema.java index 335da2d6ab..350fea80bd 100644 --- a/java/core/src/main/java/com/google/protobuf/MessageSchema.java +++ b/java/core/src/main/java/com/google/protobuf/MessageSchema.java @@ -3714,7 +3714,7 @@ final class MessageSchema implements Schema { data, position, limit, - list, + (ProtobufList) list, registers); } break;