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
pull/18681/head
Mark Hansen 2 months ago committed by Copybara-Service
parent 49ce8d87b2
commit 429a93d5fd
  1. 45
      java/core/src/main/java/com/google/protobuf/ArrayDecoders.java
  2. 2
      java/core/src/main/java/com/google/protobuf/MessageSchema.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 <T> int decodeMessageField(
Schema<T> 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 <T> int decodeGroupField(
Schema<T> 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 <T> int mergeMessageField(
Object msg, Schema<T> 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 <T> int mergeGroupField(
Object msg,
Schema schema,
Schema<T> 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<T> messageSchema = (MessageSchema<T>) 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<Object> output,
Registers registers)
throws IOException {
final ProtobufList<Object> output = (ProtobufList<Object>) 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()) {

@ -3714,7 +3714,7 @@ final class MessageSchema<T> implements Schema<T> {
data,
position,
limit,
list,
(ProtobufList<Object>) list,
registers);
}
break;

Loading…
Cancel
Save