diff options
Diffstat (limited to 'java/core/src/main/java/com/google/protobuf/AbstractMessageLite.java')
-rw-r--r-- | java/core/src/main/java/com/google/protobuf/AbstractMessageLite.java | 46 |
1 files changed, 22 insertions, 24 deletions
diff --git a/java/core/src/main/java/com/google/protobuf/AbstractMessageLite.java b/java/core/src/main/java/com/google/protobuf/AbstractMessageLite.java index 43736dd1..046030f3 100644 --- a/java/core/src/main/java/com/google/protobuf/AbstractMessageLite.java +++ b/java/core/src/main/java/com/google/protobuf/AbstractMessageLite.java @@ -45,10 +45,10 @@ import java.util.Collection; */ public abstract class AbstractMessageLite< MessageType extends AbstractMessageLite<MessageType, BuilderType>, - BuilderType extends AbstractMessageLite.Builder<MessageType, BuilderType>> + BuilderType extends AbstractMessageLite.Builder<MessageType, BuilderType>> implements MessageLite { protected int memoizedHashCode = 0; - + @Override public ByteString toByteString() { try { @@ -57,9 +57,7 @@ public abstract class AbstractMessageLite< writeTo(out.getCodedOutput()); return out.build(); } catch (IOException e) { - throw new RuntimeException( - "Serializing to a ByteString threw an IOException (should " + - "never happen).", e); + throw new RuntimeException(getSerializingExceptionMessage("ByteString"), e); } } @@ -72,9 +70,7 @@ public abstract class AbstractMessageLite< output.checkNoSpaceLeft(); return result; } catch (IOException e) { - throw new RuntimeException( - "Serializing to a byte array threw an IOException " + - "(should never happen).", e); + throw new RuntimeException(getSerializingExceptionMessage("byte array"), e); } } @@ -109,6 +105,11 @@ public abstract class AbstractMessageLite< return new UninitializedMessageException(this); } + private String getSerializingExceptionMessage(String target) { + return "Serializing " + getClass().getName() + " to a " + target + + " threw an IOException (should never happen)."; + } + protected static void checkByteStringIsUtf8(ByteString byteString) throws IllegalArgumentException { if (!byteString.isValidUtf8()) { @@ -120,7 +121,7 @@ public abstract class AbstractMessageLite< final Collection<? super T> list) { Builder.addAll(values, list); } - + /** * A partial implementation of the {@link Message.Builder} interface which * implements as many methods of that interface as possible in terms of @@ -156,9 +157,7 @@ public abstract class AbstractMessageLite< } catch (InvalidProtocolBufferException e) { throw e; } catch (IOException e) { - throw new RuntimeException( - "Reading from a ByteString threw an IOException (should " + - "never happen).", e); + throw new RuntimeException(getReadingExceptionMessage("ByteString"), e); } } @@ -174,9 +173,7 @@ public abstract class AbstractMessageLite< } catch (InvalidProtocolBufferException e) { throw e; } catch (IOException e) { - throw new RuntimeException( - "Reading from a ByteString threw an IOException (should " + - "never happen).", e); + throw new RuntimeException(getReadingExceptionMessage("ByteString"), e); } } @@ -197,9 +194,7 @@ public abstract class AbstractMessageLite< } catch (InvalidProtocolBufferException e) { throw e; } catch (IOException e) { - throw new RuntimeException( - "Reading from a byte array threw an IOException (should " + - "never happen).", e); + throw new RuntimeException(getReadingExceptionMessage("byte array"), e); } } @@ -225,9 +220,7 @@ public abstract class AbstractMessageLite< } catch (InvalidProtocolBufferException e) { throw e; } catch (IOException e) { - throw new RuntimeException( - "Reading from a byte array threw an IOException (should " + - "never happen).", e); + throw new RuntimeException(getReadingExceptionMessage("byte array"), e); } } @@ -321,7 +314,7 @@ public abstract class AbstractMessageLite< return mergeDelimitedFrom(input, ExtensionRegistryLite.getEmptyRegistry()); } - + @Override @SuppressWarnings("unchecked") // isInstance takes care of this public BuilderType mergeFrom(final MessageLite other) { @@ -329,12 +322,17 @@ public abstract class AbstractMessageLite< throw new IllegalArgumentException( "mergeFrom(MessageLite) can only merge messages of the same type."); } - + return internalMergeFrom((MessageType) other); } - + protected abstract BuilderType internalMergeFrom(MessageType message); + private String getReadingExceptionMessage(String target) { + return "Reading " + getClass().getName() + " from a " + target + + " threw an IOException (should never happen)."; + } + /** * Construct an UninitializedMessageException reporting missing fields in * the given message. |