1 // Protocol Buffers - Google's data interchange format 2 // Copyright 2008 Google Inc. All rights reserved. 3 // https://developers.google.com/protocol-buffers/ 4 // 5 // Redistribution and use in source and binary forms, with or without 6 // modification, are permitted provided that the following conditions are 7 // met: 8 // 9 // * Redistributions of source code must retain the above copyright 10 // notice, this list of conditions and the following disclaimer. 11 // * Redistributions in binary form must reproduce the above 12 // copyright notice, this list of conditions and the following disclaimer 13 // in the documentation and/or other materials provided with the 14 // distribution. 15 // * Neither the name of Google Inc. nor the names of its 16 // contributors may be used to endorse or promote products derived from 17 // this software without specific prior written permission. 18 // 19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 31 package com.google.protobuf; 32 33 import java.util.Collections; 34 import java.util.List; 35 36 /** 37 * Thrown when attempting to build a protocol message that is missing required fields. This is a 38 * {@code RuntimeException} because it normally represents a programming error: it happens when some 39 * code which constructs a message fails to set all the fields. {@code parseFrom()} methods <b>do 40 * not</b> throw this; they throw an {@link InvalidProtocolBufferException} if required fields are 41 * missing, because it is not a programming error to receive an incomplete message. In other words, 42 * {@code UninitializedMessageException} should never be thrown by correct code, but {@code 43 * InvalidProtocolBufferException} might be. 44 * 45 * @author kenton@google.com Kenton Varda 46 */ 47 public class UninitializedMessageException extends RuntimeException { 48 private static final long serialVersionUID = -7466929953374883507L; 49 UninitializedMessageException(final MessageLite message)50 public UninitializedMessageException(final MessageLite message) { 51 super( 52 "Message was missing required fields. (Lite runtime could not " 53 + "determine which fields were missing)."); 54 missingFields = null; 55 } 56 UninitializedMessageException(final List<String> missingFields)57 public UninitializedMessageException(final List<String> missingFields) { 58 super(buildDescription(missingFields)); 59 this.missingFields = missingFields; 60 } 61 62 private final List<String> missingFields; 63 64 /** 65 * Get a list of human-readable names of required fields missing from this message. Each name is a 66 * full path to a field, e.g. "foo.bar[5].baz". Returns null if the lite runtime was used, since 67 * it lacks the ability to find missing fields. 68 */ getMissingFields()69 public List<String> getMissingFields() { 70 return Collections.unmodifiableList(missingFields); 71 } 72 73 /** 74 * Converts this exception to an {@link InvalidProtocolBufferException}. When a parsed message is 75 * missing required fields, this should be thrown instead of {@code 76 * UninitializedMessageException}. 77 */ asInvalidProtocolBufferException()78 public InvalidProtocolBufferException asInvalidProtocolBufferException() { 79 return new InvalidProtocolBufferException(getMessage()); 80 } 81 82 /** Construct the description string for this exception. */ buildDescription(final List<String> missingFields)83 private static String buildDescription(final List<String> missingFields) { 84 final StringBuilder description = new StringBuilder("Message missing required fields: "); 85 boolean first = true; 86 for (final String field : missingFields) { 87 if (first) { 88 first = false; 89 } else { 90 description.append(", "); 91 } 92 description.append(field); 93 } 94 return description.toString(); 95 } 96 } 97