Commit db9d2fcd authored by Jon Skeet's avatar Jon Skeet

isInitialized optimisation for reflection-based generated protos

parent 3b315088
......@@ -73,6 +73,36 @@ public abstract class GeneratedMessage extends AbstractMessage {
}
return result;
}
public boolean isInitialized() {
// Check that all required fields are present.
for (FieldDescriptor field : getDescriptorForType().getFields()) {
if (field.isRequired()) {
if (!hasField(field)) {
return false;
}
}
}
// Check that embedded messages are initialized.
for (FieldDescriptor field : getDescriptorForType().getFields()) {
if (field.getJavaType() == FieldDescriptor.JavaType.MESSAGE) {
if (field.isRepeated()) {
for (Message element : (List<Message>) getField(field)) {
if (!element.isInitialized()) {
return false;
}
}
} else {
if (!((Message) getField(field)).isInitialized()) {
return false;
}
}
}
}
return true;
}
public Map<FieldDescriptor, Object> getAllFields() {
return Collections.unmodifiableMap(getAllFieldsMutable());
......@@ -356,6 +386,10 @@ public abstract class GeneratedMessage extends AbstractMessage {
protected boolean extensionsAreInitialized() {
return extensions.isInitialized();
}
public boolean isInitialized() {
return super.isInitialized() && extensionsAreInitialized();
}
/**
* Used by subclasses to serialize extensions. Extension ranges may be
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment