use of com.google.protobuf.GeneratedMessageLite in project Signal-Android by WhisperSystems.
the class ProtoUtil method getInnerProtos.
/**
* Recursively retrieves all inner complex proto types inside a given proto.
*/
@SuppressWarnings("rawtypes")
private static List<GeneratedMessageLite> getInnerProtos(GeneratedMessageLite proto) {
List<GeneratedMessageLite> innerProtos = new LinkedList<>();
try {
Field[] fields = proto.getClass().getDeclaredFields();
for (Field field : fields) {
if (!field.getName().equals(DEFAULT_INSTANCE) && GeneratedMessageLite.class.isAssignableFrom(field.getType())) {
field.setAccessible(true);
GeneratedMessageLite inner = (GeneratedMessageLite) field.get(proto);
if (inner != null) {
innerProtos.add(inner);
innerProtos.addAll(getInnerProtos(inner));
}
}
}
} catch (IllegalAccessException e) {
Log.w(TAG, "Failed to get inner protos!", e);
}
return innerProtos;
}
use of com.google.protobuf.GeneratedMessageLite in project Signal-Android by WhisperSystems.
the class ProtoUtil method hasUnknownFields.
/**
* True if there are unknown fields anywhere inside the proto or its nested protos.
*/
@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;
}
Aggregations