diff options
author | Jon Skeet <skeet@pobox.com> | 2008-08-14 20:37:11 +0100 |
---|---|---|
committer | Jon Skeet <skeet@pobox.com> | 2008-08-14 20:37:11 +0100 |
commit | db9d2fcd06bd8604f2ff9f30489640f64c70c4cb (patch) | |
tree | 8742e53773b2e2b39588887994f7d35938d202a7 /java | |
parent | 3b3150881a677e3c8e1ceda5be25bb1ac9a725ea (diff) | |
download | protobuf-db9d2fcd06bd8604f2ff9f30489640f64c70c4cb.tar.gz protobuf-db9d2fcd06bd8604f2ff9f30489640f64c70c4cb.tar.bz2 protobuf-db9d2fcd06bd8604f2ff9f30489640f64c70c4cb.zip |
isInitialized optimisation for reflection-based generated protos
Diffstat (limited to 'java')
-rw-r--r-- | java/src/main/java/com/google/protobuf/GeneratedMessage.java | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/java/src/main/java/com/google/protobuf/GeneratedMessage.java b/java/src/main/java/com/google/protobuf/GeneratedMessage.java index 957965b7..d4b7dab8 100644 --- a/java/src/main/java/com/google/protobuf/GeneratedMessage.java +++ b/java/src/main/java/com/google/protobuf/GeneratedMessage.java @@ -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 |