@Test public void hasUnknownFields_unknownString() throws InvalidProtocolBufferException { TestPersonWithNewString person = TestPersonWithNewString.newBuilder() .setName("Peter Parker") .setAge(23) .setJob("Reporter") .build(); TestPerson personWithUnknowns = TestPerson.parseFrom(person.toByteArray()); assertTrue(ProtoUtil.hasUnknownFields(personWithUnknowns)); }
@SuppressWarnings("rawtypes") public static boolean hasUnknownFields(GeneratedMessageLite rootProto) { try { List<GeneratedMessageLite> allProtos = getInnerProtos(rootProto); allProtos.add(rootProto); for (GeneratedMessageLite proto : allProtos) { Field field = GeneratedMessageLite.class.getDeclaredField("unknownFields"); field.setAccessible(true); UnknownFieldSetLite unknownFields = (UnknownFieldSetLite) field.get(proto); if (unknownFields != null && unknownFields.getSerializedSize() > 0) { return true; } } } catch (NoSuchFieldException | IllegalAccessException e) { Log.w(TAG, "Failed to read proto private fields! Assuming no unknown fields."); } return false; }
ProtoUtil { @SuppressWarnings("rawtypes") public static boolean hasUnknownFields(GeneratedMessageLite rootProto) { try { List<GeneratedMessageLite> allProtos = getInnerProtos(rootProto); allProtos.add(rootProto); for (GeneratedMessageLite proto : allProtos) { Field field = GeneratedMessageLite.class.getDeclaredField("unknownFields"); field.setAccessible(true); UnknownFieldSetLite unknownFields = (UnknownFieldSetLite) field.get(proto); if (unknownFields != null && unknownFields.getSerializedSize() > 0) { return true; } } } catch (NoSuchFieldException | IllegalAccessException e) { Log.w(TAG, "Failed to read proto private fields! Assuming no unknown fields."); } return false; } }
ProtoUtil { @SuppressWarnings("rawtypes") public static boolean hasUnknownFields(GeneratedMessageLite rootProto) { try { List<GeneratedMessageLite> allProtos = getInnerProtos(rootProto); allProtos.add(rootProto); for (GeneratedMessageLite proto : allProtos) { Field field = GeneratedMessageLite.class.getDeclaredField("unknownFields"); field.setAccessible(true); UnknownFieldSetLite unknownFields = (UnknownFieldSetLite) field.get(proto); if (unknownFields != null && unknownFields.getSerializedSize() > 0) { return true; } } } catch (NoSuchFieldException | IllegalAccessException e) { Log.w(TAG, "Failed to read proto private fields! Assuming no unknown fields."); } return false; } private  ProtoUtil(); }
ProtoUtil { @SuppressWarnings("rawtypes") public static boolean hasUnknownFields(GeneratedMessageLite rootProto) { try { List<GeneratedMessageLite> allProtos = getInnerProtos(rootProto); allProtos.add(rootProto); for (GeneratedMessageLite proto : allProtos) { Field field = GeneratedMessageLite.class.getDeclaredField("unknownFields"); field.setAccessible(true); UnknownFieldSetLite unknownFields = (UnknownFieldSetLite) field.get(proto); if (unknownFields != null && unknownFields.getSerializedSize() > 0) { return true; } } } catch (NoSuchFieldException | IllegalAccessException e) { Log.w(TAG, "Failed to read proto private fields! Assuming no unknown fields."); } return false; } private  ProtoUtil(); @SuppressWarnings("rawtypes") static boolean hasUnknownFields(GeneratedMessageLite rootProto); @SuppressWarnings({"rawtypes", "unchecked"}) static Proto combineWithUnknownFields(Proto proto, byte[] serializedWithUnknownFields); }
ProtoUtil { @SuppressWarnings("rawtypes") public static boolean hasUnknownFields(GeneratedMessageLite rootProto) { try { List<GeneratedMessageLite> allProtos = getInnerProtos(rootProto); allProtos.add(rootProto); for (GeneratedMessageLite proto : allProtos) { Field field = GeneratedMessageLite.class.getDeclaredField("unknownFields"); field.setAccessible(true); UnknownFieldSetLite unknownFields = (UnknownFieldSetLite) field.get(proto); if (unknownFields != null && unknownFields.getSerializedSize() > 0) { return true; } } } catch (NoSuchFieldException | IllegalAccessException e) { Log.w(TAG, "Failed to read proto private fields! Assuming no unknown fields."); } return false; } private  ProtoUtil(); @SuppressWarnings("rawtypes") static boolean hasUnknownFields(GeneratedMessageLite rootProto); @SuppressWarnings({"rawtypes", "unchecked"}) static Proto combineWithUnknownFields(Proto proto, byte[] serializedWithUnknownFields);  }