@Test public void combineWithUnknownFields_noUnknowns() throws InvalidProtocolBufferException { TestPerson personWithUnknowns = TestPerson.newBuilder() .setName("Peter Parker") .setAge(23) .build(); TestPerson localRepresentation = TestPerson.newBuilder() .setName("Spider-Man") .setAge(23) .build(); TestPerson combinedWithUnknowns = ProtoUtil.combineWithUnknownFields(localRepresentation, personWithUnknowns.toByteArray()); TestPersonWithNewString reparsedPerson = TestPersonWithNewString.parseFrom(combinedWithUnknowns.toByteArray()); Assert.assertEquals("Spider-Man", reparsedPerson.getName()); Assert.assertEquals(23, reparsedPerson.getAge()); }
@SuppressWarnings({"rawtypes", "unchecked"}) public static <Proto extends GeneratedMessageLite> Proto combineWithUnknownFields(Proto proto, byte[] serializedWithUnknownFields) { if (serializedWithUnknownFields == null) { return proto; } try { Proto protoWithUnknownFields = (Proto) proto.getParserForType().parseFrom(serializedWithUnknownFields); byte[] unknownFields = getUnknownFields(protoWithUnknownFields); if (unknownFields == null) { return proto; } byte[] combined = ByteUtil.combine(proto.toByteArray(), unknownFields); return (Proto) proto.getParserForType().parseFrom(combined); } catch (InvalidProtocolBufferException e) { throw new IllegalArgumentException(); } }
ProtoUtil { @SuppressWarnings({"rawtypes", "unchecked"}) public static <Proto extends GeneratedMessageLite> Proto combineWithUnknownFields(Proto proto, byte[] serializedWithUnknownFields) { if (serializedWithUnknownFields == null) { return proto; } try { Proto protoWithUnknownFields = (Proto) proto.getParserForType().parseFrom(serializedWithUnknownFields); byte[] unknownFields = getUnknownFields(protoWithUnknownFields); if (unknownFields == null) { return proto; } byte[] combined = ByteUtil.combine(proto.toByteArray(), unknownFields); return (Proto) proto.getParserForType().parseFrom(combined); } catch (InvalidProtocolBufferException e) { throw new IllegalArgumentException(); } } }
ProtoUtil { @SuppressWarnings({"rawtypes", "unchecked"}) public static <Proto extends GeneratedMessageLite> Proto combineWithUnknownFields(Proto proto, byte[] serializedWithUnknownFields) { if (serializedWithUnknownFields == null) { return proto; } try { Proto protoWithUnknownFields = (Proto) proto.getParserForType().parseFrom(serializedWithUnknownFields); byte[] unknownFields = getUnknownFields(protoWithUnknownFields); if (unknownFields == null) { return proto; } byte[] combined = ByteUtil.combine(proto.toByteArray(), unknownFields); return (Proto) proto.getParserForType().parseFrom(combined); } catch (InvalidProtocolBufferException e) { throw new IllegalArgumentException(); } } private  ProtoUtil(); }
ProtoUtil { @SuppressWarnings({"rawtypes", "unchecked"}) public static <Proto extends GeneratedMessageLite> Proto combineWithUnknownFields(Proto proto, byte[] serializedWithUnknownFields) { if (serializedWithUnknownFields == null) { return proto; } try { Proto protoWithUnknownFields = (Proto) proto.getParserForType().parseFrom(serializedWithUnknownFields); byte[] unknownFields = getUnknownFields(protoWithUnknownFields); if (unknownFields == null) { return proto; } byte[] combined = ByteUtil.combine(proto.toByteArray(), unknownFields); return (Proto) proto.getParserForType().parseFrom(combined); } catch (InvalidProtocolBufferException e) { throw new IllegalArgumentException(); } } private  ProtoUtil(); @SuppressWarnings("rawtypes") static boolean hasUnknownFields(GeneratedMessageLite rootProto); @SuppressWarnings({"rawtypes", "unchecked"}) static Proto combineWithUnknownFields(Proto proto, byte[] serializedWithUnknownFields); }
ProtoUtil { @SuppressWarnings({"rawtypes", "unchecked"}) public static <Proto extends GeneratedMessageLite> Proto combineWithUnknownFields(Proto proto, byte[] serializedWithUnknownFields) { if (serializedWithUnknownFields == null) { return proto; } try { Proto protoWithUnknownFields = (Proto) proto.getParserForType().parseFrom(serializedWithUnknownFields); byte[] unknownFields = getUnknownFields(protoWithUnknownFields); if (unknownFields == null) { return proto; } byte[] combined = ByteUtil.combine(proto.toByteArray(), unknownFields); return (Proto) proto.getParserForType().parseFrom(combined); } catch (InvalidProtocolBufferException e) { throw new IllegalArgumentException(); } } private  ProtoUtil(); @SuppressWarnings("rawtypes") static boolean hasUnknownFields(GeneratedMessageLite rootProto); @SuppressWarnings({"rawtypes", "unchecked"}) static Proto combineWithUnknownFields(Proto proto, byte[] serializedWithUnknownFields);  }