• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc.  All rights reserved.
3 //
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file or at
6 // https://developers.google.com/open-source/licenses/bsd
7 
8 package com.google.protobuf;
9 
10 import java.util.Collections;
11 import java.util.List;
12 
13 /**
14  * Thrown when attempting to build a protocol message that is missing required fields. This is a
15  * {@code RuntimeException} because it normally represents a programming error: it happens when some
16  * code which constructs a message fails to set all the fields. {@code parseFrom()} methods <b>do
17  * not</b> throw this; they throw an {@link InvalidProtocolBufferException} if required fields are
18  * missing, because it is not a programming error to receive an incomplete message. In other words,
19  * {@code UninitializedMessageException} should never be thrown by correct code, but {@code
20  * InvalidProtocolBufferException} might be.
21  *
22  * @author kenton@google.com Kenton Varda
23  */
24 public class UninitializedMessageException extends RuntimeException {
25   private static final long serialVersionUID = -7466929953374883507L;
26 
UninitializedMessageException(final MessageLite message)27   public UninitializedMessageException(final MessageLite message) {
28     super(
29         "Message was missing required fields.  (Lite runtime could not "
30             + "determine which fields were missing).");
31     missingFields = null;
32   }
33 
UninitializedMessageException(final List<String> missingFields)34   public UninitializedMessageException(final List<String> missingFields) {
35     super(buildDescription(missingFields));
36     this.missingFields = missingFields;
37   }
38 
39   private final List<String> missingFields;
40 
41   /**
42    * Get a list of human-readable names of required fields missing from this message. Each name is a
43    * full path to a field, e.g. "foo.bar[5].baz". Returns null if the lite runtime was used, since
44    * it lacks the ability to find missing fields.
45    */
getMissingFields()46   public List<String> getMissingFields() {
47     return Collections.unmodifiableList(missingFields);
48   }
49 
50   /**
51    * Converts this exception to an {@link InvalidProtocolBufferException}. When a parsed message is
52    * missing required fields, this should be thrown instead of {@code
53    * UninitializedMessageException}.
54    */
asInvalidProtocolBufferException()55   public InvalidProtocolBufferException asInvalidProtocolBufferException() {
56     return new InvalidProtocolBufferException(getMessage());
57   }
58 
59   /** Construct the description string for this exception. */
buildDescription(final List<String> missingFields)60   private static String buildDescription(final List<String> missingFields) {
61     final StringBuilder description = new StringBuilder("Message missing required fields: ");
62     boolean first = true;
63     for (final String field : missingFields) {
64       if (first) {
65         first = false;
66       } else {
67         description.append(", ");
68       }
69       description.append(field);
70     }
71     return description.toString();
72   }
73 }
74