2020import com .google .common .annotations .VisibleForTesting ;
2121import com .google .common .base .Defaults ;
2222import com .google .common .collect .ImmutableList ;
23+ import com .google .common .collect .ImmutableListMultimap ;
2324import com .google .common .collect .ImmutableMap ;
2425import com .google .common .collect .Multimap ;
2526import com .google .common .collect .Multimaps ;
4546import java .util .List ;
4647import java .util .Map ;
4748import java .util .NoSuchElementException ;
49+ import java .util .Optional ;
4850import java .util .TreeMap ;
4951
5052/**
@@ -80,7 +82,7 @@ private static Object readPrimitiveField(
8082 case INT64 :
8183 return inputStream .readInt64 ();
8284 case UINT32 :
83- return UnsignedLong .fromLongBits (inputStream .readUInt32 ());
85+ return UnsignedLong .fromLongBits (Integer . toUnsignedLong ( inputStream .readUInt32 () ));
8486 case UINT64 :
8587 return UnsignedLong .fromLongBits (inputStream .readUInt64 ());
8688 case BOOL :
@@ -160,6 +162,19 @@ Object getDefaultCelValue(String protoTypeName, String fieldName) {
160162 return toRuntimeValue (defaultValue );
161163 }
162164
165+ @ Internal
166+ public Optional <FieldLiteDescriptor > findFieldDescriptor (String protoTypeName , int fieldNumber ) {
167+ return descriptorPool
168+ .findDescriptor (protoTypeName )
169+ .flatMap (desc -> desc .findByFieldNumber (fieldNumber ));
170+ }
171+
172+ @ Internal
173+ public Optional <Object > findDefaultCelValue (String protoTypeName , int fieldNumber ) {
174+ return findFieldDescriptor (protoTypeName , fieldNumber )
175+ .map (fieldDescriptor -> toRuntimeValue (getDefaultValue (fieldDescriptor )));
176+ }
177+
163178 @ Override
164179 @ SuppressWarnings ("LiteProtoToString" ) // No alternative identifier to use. Debug only info is OK.
165180 public Object toRuntimeValue (Object value ) {
@@ -193,7 +208,10 @@ protected Object fromWellKnownProto(MessageLiteOrBuilder msg, WellKnownProto wel
193208 descriptorPool
194209 .findDescriptor (message )
195210 .orElseThrow (
196- () -> new NoSuchElementException ("Could not find a descriptor for: " + message ));
211+ () ->
212+ new NoSuchElementException (
213+ "Could not find a descriptor for message of type: "
214+ + message .getClass ().getName ()));
197215 return ProtoMessageLiteValue .create (message , descriptor .getProtoTypeName (), this );
198216 }
199217
@@ -369,11 +387,14 @@ MessageFields readAllFields(byte[] bytes, String protoTypeName) throws IOExcepti
369387
370388 ImmutableMap <String , Object > readAllFields (MessageLite msg , String protoTypeName )
371389 throws IOException {
372- return readAllFields (msg . toByteArray () , protoTypeName ).values ();
390+ return readMessageFields (msg , protoTypeName ).values ();
373391 }
374392
375- private static Object readUnknownField (int tagWireType , CodedInputStream inputStream )
376- throws IOException {
393+ MessageFields readMessageFields (MessageLite msg , String protoTypeName ) throws IOException {
394+ return readAllFields (msg .toByteArray (), protoTypeName );
395+ }
396+
397+ static Object readUnknownField (int tagWireType , CodedInputStream inputStream ) throws IOException {
377398 switch (tagWireType ) {
378399 case WireFormat .WIRETYPE_VARINT :
379400 return inputStream .readInt64 ();
@@ -393,16 +414,19 @@ private static Object readUnknownField(int tagWireType, CodedInputStream inputSt
393414 }
394415
395416 @ AutoValue
396- @ SuppressWarnings ("AutoValueImmutableFields" ) // Unknowns are inaccessible to users.
417+ @ AutoValue .CopyAnnotations
418+ @ Immutable
419+ @ SuppressWarnings ("Immutable" ) // Safe immutable fields
397420 abstract static class MessageFields {
398421
399422 abstract ImmutableMap <String , Object > values ();
400423
401- abstract Multimap <Integer , Object > unknowns ();
424+ abstract ImmutableListMultimap <Integer , Object > unknowns ();
402425
403426 static MessageFields create (
404427 ImmutableMap <String , Object > fieldValues , Multimap <Integer , Object > unknownFields ) {
405- return new AutoValue_ProtoLiteCelValueConverter_MessageFields (fieldValues , unknownFields );
428+ return new AutoValue_ProtoLiteCelValueConverter_MessageFields (
429+ fieldValues , ImmutableListMultimap .copyOf (unknownFields ));
406430 }
407431 }
408432
0 commit comments