diff --git a/java/core/src/main/java/com/google/protobuf/GeneratedMessageV3.java b/java/core/src/main/java/com/google/protobuf/GeneratedMessageV3.java index 57483b66af8c..3683fa7a9b20 100644 --- a/java/core/src/main/java/com/google/protobuf/GeneratedMessageV3.java +++ b/java/core/src/main/java/com/google/protobuf/GeneratedMessageV3.java @@ -728,7 +728,7 @@ public Object getField(final FieldDescriptor field) { if (field.isRepeated()) { // The underlying list object is still modifiable at this point. // Make sure not to expose the modifiable list to the caller. - return Collections.unmodifiableList((List) object); + return Collections.unmodifiableList((List) object); } else { return object; } @@ -953,7 +953,7 @@ protected MapField internalGetMutableMapField(int fieldNumber) { // Extensions-related stuff /** Extends {@link MessageOrBuilder} with extension-related functions. */ - public interface ExtendableMessageOrBuilder + public interface ExtendableMessageOrBuilder> extends MessageOrBuilder { // Re-define for return type covariance. @Override @@ -1055,7 +1055,7 @@ T getExtension( * *

See also {@link ExtendableBuilder}. */ - public abstract static class ExtendableMessage + public abstract static class ExtendableMessage> extends GeneratedMessageV3 implements ExtendableMessageOrBuilder { private static final long serialVersionUID = 1L; @@ -1085,7 +1085,6 @@ private void verifyExtensionContainingType(final Extension extensio /** Check if a singular extension is present. */ @Override - @SuppressWarnings("unchecked") public final boolean hasExtension(final ExtensionLite extensionLite) { Extension extension = checkNotLite(extensionLite); @@ -1095,7 +1094,6 @@ public final boolean hasExtension(final ExtensionLite extension /** Get the number of elements in a repeated extension. */ @Override - @SuppressWarnings("unchecked") public final int getExtensionCount(final ExtensionLite> extensionLite) { Extension> extension = checkNotLite(extensionLite); @@ -1452,7 +1450,7 @@ private void verifyContainingType(final FieldDescriptor field) { */ @SuppressWarnings("unchecked") public abstract static class ExtendableBuilder< - MessageT extends ExtendableMessage, + MessageT extends ExtendableMessage, BuilderT extends ExtendableBuilder> extends Builder implements ExtendableMessageOrBuilder { @@ -1588,8 +1586,8 @@ public final BuilderT addExtension( } /** Clear an extension. */ - public final BuilderT clearExtension(final ExtensionLite extensionLite) { - Extension extension = checkNotLite(extensionLite); + public final BuilderT clearExtension(final ExtensionLite extensionLite) { + Extension extension = checkNotLite(extensionLite); verifyExtensionContainingType(extension); ensureExtensionsIsMutable(); @@ -1722,16 +1720,16 @@ public BuilderT addExtension( *

TODO(b/258340024): handled by ExtensionLite version */ public final BuilderT clearExtension( - final Extension extension) { - return clearExtension((ExtensionLite) extension); + final Extension extension) { + return clearExtension((ExtensionLite) extension); } /** - * Clear an extension. + * Clears an extension. *

TODO(b/258340024): handled by ExtensionLite version */ public BuilderT clearExtension( - final GeneratedExtension extension) { - return clearExtension((ExtensionLite) extension); + final GeneratedExtension extension) { + return clearExtension((ExtensionLite) extension); } /** Called by subclasses to check if all extensions are initialized. */ @@ -1940,7 +1938,7 @@ public Message.Builder newBuilderForField(final FieldDescriptor field) { } } - protected final void mergeExtensionFields(final ExtendableMessage other) { + protected final void mergeExtensionFields(final ExtendableMessage other) { if (other.extensions != null) { ensureExtensionsIsMutable(); extensions.mergeFrom(other.extensions); @@ -1982,9 +1980,8 @@ static interface ExtensionDescriptorRetriever { // ================================================================= /** Calls Class.getMethod and throws a RuntimeException if it fails. */ - @SuppressWarnings("unchecked") private static Method getMethodOrDie( - final Class clazz, final String name, final Class... params) { + final Class clazz, final String name, final Class... params) { try { return clazz.getMethod(name, params); } catch (NoSuchMethodException e) { @@ -2052,7 +2049,7 @@ public FieldAccessorTable( final Descriptor descriptor, final String[] camelCaseNames, final Class messageClass, - final Class builderClass) { + final Class> builderClass) { this(descriptor, camelCaseNames); ensureFieldAccessorsInitialized(messageClass, builderClass); } @@ -2077,7 +2074,7 @@ public FieldAccessorTable(final Descriptor descriptor, final String[] camelCaseN * @return this */ public FieldAccessorTable ensureFieldAccessorsInitialized( - Class messageClass, Class builderClass) { + Class messageClass, Class> builderClass) { if (initialized) { return this; } @@ -2192,35 +2189,35 @@ private OneofAccessor getOneof(final OneofDescriptor oneof) { private interface FieldAccessor { Object get(GeneratedMessageV3 message); - Object get(GeneratedMessageV3.Builder builder); + Object get(GeneratedMessageV3.Builder builder); Object getRaw(GeneratedMessageV3 message); - Object getRaw(GeneratedMessageV3.Builder builder); + Object getRaw(GeneratedMessageV3.Builder builder); - void set(Builder builder, Object value); + void set(Builder builder, Object value); Object getRepeated(GeneratedMessageV3 message, int index); - Object getRepeated(GeneratedMessageV3.Builder builder, int index); + Object getRepeated(GeneratedMessageV3.Builder builder, int index); Object getRepeatedRaw(GeneratedMessageV3 message, int index); - Object getRepeatedRaw(GeneratedMessageV3.Builder builder, int index); + Object getRepeatedRaw(GeneratedMessageV3.Builder builder, int index); - void setRepeated(Builder builder, int index, Object value); + void setRepeated(Builder builder, int index, Object value); - void addRepeated(Builder builder, Object value); + void addRepeated(Builder builder, Object value); boolean has(GeneratedMessageV3 message); - boolean has(GeneratedMessageV3.Builder builder); + boolean has(GeneratedMessageV3.Builder builder); int getRepeatedCount(GeneratedMessageV3 message); - int getRepeatedCount(GeneratedMessageV3.Builder builder); + int getRepeatedCount(GeneratedMessageV3.Builder builder); - void clear(Builder builder); + void clear(Builder builder); Message.Builder newBuilder(); @@ -2236,7 +2233,7 @@ private static class OneofAccessor { final int oneofIndex, final String camelCaseName, final Class messageClass, - final Class builderClass) { + final Class> builderClass) { this.descriptor = descriptor; OneofDescriptor oneofDescriptor = descriptor.getOneofs().get(oneofIndex); if (oneofDescriptor.isSynthetic()) { @@ -2265,7 +2262,7 @@ public boolean has(final GeneratedMessageV3 message) { } } - public boolean has(GeneratedMessageV3.Builder builder) { + public boolean has(GeneratedMessageV3.Builder builder) { if (fieldDescriptor != null) { return builder.hasField(fieldDescriptor); } else { @@ -2285,7 +2282,7 @@ public FieldDescriptor get(final GeneratedMessageV3 message) { return null; } - public FieldDescriptor get(GeneratedMessageV3.Builder builder) { + public FieldDescriptor get(GeneratedMessageV3.Builder builder) { if (fieldDescriptor != null) { return builder.hasField(fieldDescriptor) ? fieldDescriptor : null; } else { @@ -2298,7 +2295,7 @@ public FieldDescriptor get(GeneratedMessageV3.Builder builder) { return null; } - public void clear(final Builder builder) { + public void clear(final Builder builder) { // TODO(b/230609037): remove the unused variable Object unused = invokeOrDie(clearMethod, builder); } @@ -2339,7 +2336,7 @@ private static final class ReflectionInvoker implements MethodInvoker { final FieldDescriptor descriptor, final String camelCaseName, final Class messageClass, - final Class builderClass, + final Class> builderClass, final String containingOneofCamelCaseName, boolean isOneofField, boolean hasHasMethod) { @@ -2408,7 +2405,7 @@ public void clear(final GeneratedMessageV3.Builder builder) { final FieldDescriptor descriptor, final String camelCaseName, final Class messageClass, - final Class builderClass, + final Class> builderClass, final String containingOneofCamelCaseName) { isOneofField = descriptor.getContainingOneof() != null @@ -2450,7 +2447,7 @@ public Object get(final GeneratedMessageV3 message) { } @Override - public Object get(GeneratedMessageV3.Builder builder) { + public Object get(GeneratedMessageV3.Builder builder) { return invoker.get(builder); } @@ -2460,12 +2457,12 @@ public Object getRaw(final GeneratedMessageV3 message) { } @Override - public Object getRaw(GeneratedMessageV3.Builder builder) { + public Object getRaw(GeneratedMessageV3.Builder builder) { return get(builder); } @Override - public void set(final Builder builder, final Object value) { + public void set(final Builder builder, final Object value) { invoker.set(builder, value); } @@ -2475,29 +2472,29 @@ public Object getRepeated(final GeneratedMessageV3 message, final int index) { } @Override - public Object getRepeatedRaw(final GeneratedMessageV3 message, final int index) { - throw new UnsupportedOperationException( - "getRepeatedFieldRaw() called on a singular field."); + public Object getRepeated(GeneratedMessageV3.Builder builder, int index) { + throw new UnsupportedOperationException("getRepeatedField() called on a singular field."); } @Override - public Object getRepeated(GeneratedMessageV3.Builder builder, int index) { - throw new UnsupportedOperationException("getRepeatedField() called on a singular field."); + public Object getRepeatedRaw(final GeneratedMessageV3 message, final int index) { + throw new UnsupportedOperationException( + "getRepeatedFieldRaw() called on a singular field."); } @Override - public Object getRepeatedRaw(GeneratedMessageV3.Builder builder, int index) { + public Object getRepeatedRaw(GeneratedMessageV3.Builder builder, int index) { throw new UnsupportedOperationException( "getRepeatedFieldRaw() called on a singular field."); } @Override - public void setRepeated(final Builder builder, final int index, final Object value) { + public void setRepeated(final Builder builder, final int index, final Object value) { throw new UnsupportedOperationException("setRepeatedField() called on a singular field."); } @Override - public void addRepeated(final Builder builder, final Object value) { + public void addRepeated(final Builder builder, final Object value) { throw new UnsupportedOperationException("addRepeatedField() called on a singular field."); } @@ -2513,7 +2510,7 @@ public boolean has(final GeneratedMessageV3 message) { } @Override - public boolean has(GeneratedMessageV3.Builder builder) { + public boolean has(GeneratedMessageV3.Builder builder) { if (!hasHasMethod) { if (isOneofField) { return invoker.getOneofFieldNumber(builder) == field.getNumber(); @@ -2530,13 +2527,13 @@ public int getRepeatedCount(final GeneratedMessageV3 message) { } @Override - public int getRepeatedCount(GeneratedMessageV3.Builder builder) { + public int getRepeatedCount(GeneratedMessageV3.Builder builder) { throw new UnsupportedOperationException( "getRepeatedFieldSize() called on a singular field."); } @Override - public void clear(final Builder builder) { + public void clear(final Builder builder) { invoker.clear(builder); } @@ -2595,7 +2592,7 @@ private static final class ReflectionInvoker implements MethodInvoker { final FieldDescriptor descriptor, final String camelCaseName, final Class messageClass, - final Class builderClass) { + final Class> builderClass) { getMethod = getMethodOrDie(messageClass, "get" + camelCaseName + "List"); getMethodBuilder = getMethodOrDie(builderClass, "get" + camelCaseName + "List"); getRepeatedMethod = getMethodOrDie(messageClass, "get" + camelCaseName, Integer.TYPE); @@ -2660,14 +2657,14 @@ public void clear(final GeneratedMessageV3.Builder builder) { } } - protected final Class type; + protected final Class type; protected final MethodInvoker invoker; RepeatedFieldAccessor( final FieldDescriptor descriptor, final String camelCaseName, final Class messageClass, - final Class builderClass) { + final Class> builderClass) { ReflectionInvoker reflectionInvoker = new ReflectionInvoker(descriptor, camelCaseName, messageClass, builderClass); type = reflectionInvoker.getRepeatedMethod.getReturnType(); @@ -2684,7 +2681,7 @@ public Object get(final GeneratedMessageV3 message) { } @Override - public Object get(GeneratedMessageV3.Builder builder) { + public Object get(GeneratedMessageV3.Builder builder) { return invoker.get(builder); } @@ -2694,12 +2691,12 @@ public Object getRaw(final GeneratedMessageV3 message) { } @Override - public Object getRaw(GeneratedMessageV3.Builder builder) { + public Object getRaw(GeneratedMessageV3.Builder builder) { return get(builder); } @Override - public void set(final Builder builder, final Object value) { + public void set(final Builder builder, final Object value) { // Add all the elements individually. This serves two purposes: // 1) Verifies that each element has the correct type. // 2) Insures that the caller cannot modify the list later on and @@ -2716,7 +2713,7 @@ public Object getRepeated(final GeneratedMessageV3 message, final int index) { } @Override - public Object getRepeated(GeneratedMessageV3.Builder builder, int index) { + public Object getRepeated(GeneratedMessageV3.Builder builder, int index) { return invoker.getRepeated(builder, index); } @@ -2726,17 +2723,17 @@ public Object getRepeatedRaw(GeneratedMessageV3 message, int index) { } @Override - public Object getRepeatedRaw(GeneratedMessageV3.Builder builder, int index) { + public Object getRepeatedRaw(GeneratedMessageV3.Builder builder, int index) { return getRepeated(builder, index); } @Override - public void setRepeated(final Builder builder, final int index, final Object value) { + public void setRepeated(final Builder builder, final int index, final Object value) { invoker.setRepeated(builder, index, value); } @Override - public void addRepeated(final Builder builder, final Object value) { + public void addRepeated(final Builder builder, final Object value) { invoker.addRepeated(builder, value); } @@ -2746,7 +2743,7 @@ public boolean has(final GeneratedMessageV3 message) { } @Override - public boolean has(GeneratedMessageV3.Builder builder) { + public boolean has(GeneratedMessageV3.Builder builder) { throw new UnsupportedOperationException("hasField() called on a repeated field."); } @@ -2756,12 +2753,12 @@ public int getRepeatedCount(final GeneratedMessageV3 message) { } @Override - public int getRepeatedCount(GeneratedMessageV3.Builder builder) { + public int getRepeatedCount(GeneratedMessageV3.Builder builder) { return invoker.getRepeatedCount(builder); } @Override - public void clear(final Builder builder) { + public void clear(final Builder builder) { invoker.clear(builder); } @@ -2800,11 +2797,11 @@ private static class MapFieldAccessor implements FieldAccessor { return (MapField) message.internalGetMapField(field.getNumber()); } - private MapField getMapField(GeneratedMessageV3.Builder builder) { + private MapField getMapField(GeneratedMessageV3.Builder builder) { return (MapField) builder.internalGetMapField(field.getNumber()); } - private MapField getMutableMapField(GeneratedMessageV3.Builder builder) { + private MapField getMutableMapField(GeneratedMessageV3.Builder builder) { return (MapField) builder.internalGetMutableMapField(field.getNumber()); } @@ -2823,9 +2820,8 @@ private Message coerceType(Message value) { } @Override - @SuppressWarnings("unchecked") public Object get(GeneratedMessageV3 message) { - List result = new ArrayList<>(); + List result = new ArrayList<>(); for (int i = 0; i < getRepeatedCount(message); i++) { result.add(getRepeated(message, i)); } @@ -2833,9 +2829,8 @@ public Object get(GeneratedMessageV3 message) { } @Override - @SuppressWarnings("unchecked") - public Object get(Builder builder) { - List result = new ArrayList<>(); + public Object get(Builder builder) { + List result = new ArrayList<>(); for (int i = 0; i < getRepeatedCount(builder); i++) { result.add(getRepeated(builder, i)); } @@ -2848,14 +2843,14 @@ public Object getRaw(GeneratedMessageV3 message) { } @Override - public Object getRaw(GeneratedMessageV3.Builder builder) { + public Object getRaw(GeneratedMessageV3.Builder builder) { return get(builder); } @Override - public void set(Builder builder, Object value) { + public void set(Builder builder, Object value) { clear(builder); - for (Object entry : (List) value) { + for (Object entry : (List) value) { addRepeated(builder, entry); } } @@ -2866,7 +2861,7 @@ public Object getRepeated(GeneratedMessageV3 message, int index) { } @Override - public Object getRepeated(Builder builder, int index) { + public Object getRepeated(Builder builder, int index) { return getMapField(builder).getList().get(index); } @@ -2876,17 +2871,17 @@ public Object getRepeatedRaw(GeneratedMessageV3 message, int index) { } @Override - public Object getRepeatedRaw(Builder builder, int index) { + public Object getRepeatedRaw(Builder builder, int index) { return getRepeated(builder, index); } @Override - public void setRepeated(Builder builder, int index, Object value) { + public void setRepeated(Builder builder, int index, Object value) { getMutableMapField(builder).getMutableList().set(index, coerceType((Message) value)); } @Override - public void addRepeated(Builder builder, Object value) { + public void addRepeated(Builder builder, Object value) { getMutableMapField(builder).getMutableList().add(coerceType((Message) value)); } @@ -2896,7 +2891,7 @@ public boolean has(GeneratedMessageV3 message) { } @Override - public boolean has(Builder builder) { + public boolean has(Builder builder) { throw new UnsupportedOperationException("hasField() is not supported for repeated fields."); } @@ -2906,12 +2901,12 @@ public int getRepeatedCount(GeneratedMessageV3 message) { } @Override - public int getRepeatedCount(Builder builder) { + public int getRepeatedCount(Builder builder) { return getMapField(builder).getList().size(); } @Override - public void clear(Builder builder) { + public void clear(Builder builder) { getMutableMapField(builder).getMutableList().clear(); } @@ -2938,7 +2933,7 @@ private static final class SingularEnumFieldAccessor extends SingularFieldAccess final FieldDescriptor descriptor, final String camelCaseName, final Class messageClass, - final Class builderClass, + final Class> builderClass, final String containingOneofCamelCaseName) { super(descriptor, camelCaseName, messageClass, builderClass, containingOneofCamelCaseName); @@ -2975,7 +2970,7 @@ public Object get(final GeneratedMessageV3 message) { } @Override - public Object get(final GeneratedMessageV3.Builder builder) { + public Object get(final GeneratedMessageV3.Builder builder) { if (supportUnknownEnumValue) { int value = (Integer) invokeOrDie(getValueMethodBuilder, builder); return enumDescriptor.findValueByNumberCreatingIfUnknown(value); @@ -2984,7 +2979,7 @@ public Object get(final GeneratedMessageV3.Builder builder) { } @Override - public void set(final Builder builder, final Object value) { + public void set(final Builder builder, final Object value) { if (supportUnknownEnumValue) { // TODO(b/230609037): remove the unused variable Object unused = @@ -3000,7 +2995,7 @@ private static final class RepeatedEnumFieldAccessor extends RepeatedFieldAccess final FieldDescriptor descriptor, final String camelCaseName, final Class messageClass, - final Class builderClass) { + final Class> builderClass) { super(descriptor, camelCaseName, messageClass, builderClass); enumDescriptor = descriptor.getEnumType(); @@ -3044,7 +3039,7 @@ public Object get(final GeneratedMessageV3 message) { } @Override - public Object get(final GeneratedMessageV3.Builder builder) { + public Object get(final GeneratedMessageV3.Builder builder) { final List newList = new ArrayList<>(); final int size = getRepeatedCount(builder); for (int i = 0; i < size; i++) { @@ -3063,7 +3058,7 @@ public Object getRepeated(final GeneratedMessageV3 message, final int index) { } @Override - public Object getRepeated(final GeneratedMessageV3.Builder builder, final int index) { + public Object getRepeated(final GeneratedMessageV3.Builder builder, final int index) { if (supportUnknownEnumValue) { int value = (Integer) invokeOrDie(getRepeatedValueMethodBuilder, builder, index); return enumDescriptor.findValueByNumberCreatingIfUnknown(value); @@ -3072,7 +3067,7 @@ public Object getRepeated(final GeneratedMessageV3.Builder builder, final int in } @Override - public void setRepeated(final Builder builder, final int index, final Object value) { + public void setRepeated(final Builder builder, final int index, final Object value) { if (supportUnknownEnumValue) { // TODO(b/230609037): remove the unused variable Object unused = @@ -3087,7 +3082,7 @@ public void setRepeated(final Builder builder, final int index, final Object val } @Override - public void addRepeated(final Builder builder, final Object value) { + public void addRepeated(final Builder builder, final Object value) { if (supportUnknownEnumValue) { // TODO(b/230609037): remove the unused variable Object unused = @@ -3116,7 +3111,7 @@ private static final class SingularStringFieldAccessor extends SingularFieldAcce final FieldDescriptor descriptor, final String camelCaseName, final Class messageClass, - final Class builderClass, + final Class> builderClass, final String containingOneofCamelCaseName) { super(descriptor, camelCaseName, messageClass, builderClass, containingOneofCamelCaseName); getBytesMethod = getMethodOrDie(messageClass, "get" + camelCaseName + "Bytes"); @@ -3135,12 +3130,12 @@ public Object getRaw(final GeneratedMessageV3 message) { } @Override - public Object getRaw(GeneratedMessageV3.Builder builder) { + public Object getRaw(GeneratedMessageV3.Builder builder) { return invokeOrDie(getBytesMethodBuilder, builder); } @Override - public void set(GeneratedMessageV3.Builder builder, Object value) { + public void set(GeneratedMessageV3.Builder builder, Object value) { if (value instanceof ByteString) { // TODO(b/230609037): remove the unused variable Object unused = invokeOrDie(setBytesMethodBuilder, builder, value); @@ -3157,7 +3152,7 @@ private static final class SingularMessageFieldAccessor extends SingularFieldAcc final FieldDescriptor descriptor, final String camelCaseName, final Class messageClass, - final Class builderClass, + final Class> builderClass, final String containingOneofCamelCaseName) { super(descriptor, camelCaseName, messageClass, builderClass, containingOneofCamelCaseName); @@ -3183,7 +3178,7 @@ private Object coerceType(final Object value) { } @Override - public void set(final Builder builder, final Object value) { + public void set(final Builder builder, final Object value) { super.set(builder, coerceType(value)); } @@ -3203,7 +3198,7 @@ private static final class RepeatedMessageFieldAccessor extends RepeatedFieldAcc final FieldDescriptor descriptor, final String camelCaseName, final Class messageClass, - final Class builderClass) { + final Class> builderClass) { super(descriptor, camelCaseName, messageClass, builderClass); newBuilderMethod = getMethodOrDie(type, "newBuilder"); @@ -3229,12 +3224,12 @@ private Object coerceType(final Object value) { } @Override - public void setRepeated(final Builder builder, final int index, final Object value) { + public void setRepeated(final Builder builder, final int index, final Object value) { super.setRepeated(builder, index, coerceType(value)); } @Override - public void addRepeated(final Builder builder, final Object value) { + public void addRepeated(final Builder builder, final Object value) { super.addRepeated(builder, coerceType(value)); }