Update 25.x GeneratedMessageV3 and nested classes to inherit from GeneratedMessage that passes 25.x //java/... and //compatability/... bazel build and tests

25.x-compat-upgrade
Sandy Zhang 4 months ago
parent 45b56c137e
commit 3e6be939eb
  1. 15
      java/core/src/main/java/com/google/protobuf/GeneratedMessage.java
  2. 46
      java/core/src/main/java/com/google/protobuf/GeneratedMessageV3.java
  3. 34
      java/core/src/main/java/com/google/protobuf/RepeatedFieldBuilderV3.java
  4. 12
      java/core/src/main/java/com/google/protobuf/SingleFieldBuilderV3.java
  5. 2
      java/core/src/test/java/com/google/protobuf/RepeatedFieldBuilderV3Test.java
  6. 2
      java/core/src/test/java/com/google/protobuf/TestUtil.java

@ -325,17 +325,6 @@ public abstract class GeneratedMessage extends AbstractMessage implements Serial
/** TODO: remove this together with GeneratedMessage.BuilderParent. */
protected abstract Message.Builder newBuilderForType(BuilderParent parent);
@Override
protected Message.Builder newBuilderForType(final AbstractMessage.BuilderParent parent) {
return newBuilderForType(
new BuilderParent() {
@Override
public void markDirty() {
parent.markDirty();
}
});
}
@SuppressWarnings("unchecked")
public abstract static class Builder<BuilderType extends Builder<BuilderType>>
extends AbstractMessage.Builder<BuilderType> {
@ -592,7 +581,7 @@ public abstract class GeneratedMessage extends AbstractMessage implements Serial
}
@Override
public final UnknownFieldSet getUnknownFields() {
public UnknownFieldSet getUnknownFields() {
return unknownFields;
}
@ -1908,7 +1897,7 @@ public abstract class GeneratedMessage extends AbstractMessage implements Serial
* Users should ignore this class. This class provides the implementation with access to the
* fields of a message object using Java reflection.
*/
public static final class FieldAccessorTable {
public static class FieldAccessorTable {
/**
* Construct a FieldAccessorTable for a particular message class. Only one FieldAccessorTable

@ -53,7 +53,7 @@ import java.util.TreeMap;
*
* @author kenton@google.com Kenton Varda
*/
public abstract class GeneratedMessageV3 extends AbstractMessage implements Serializable {
public abstract class GeneratedMessageV3 extends GeneratedMessage implements Serializable {
private static final long serialVersionUID = 1L;
/**
@ -537,27 +537,23 @@ public abstract class GeneratedMessageV3 extends AbstractMessage implements Seri
*
* <p>TODO: Remove at breaking change since b/29368482 was fixed in 2020
*/
protected interface BuilderParent extends AbstractMessage.BuilderParent {}
protected interface BuilderParent extends GeneratedMessage.BuilderParent {}
/** TODO: remove this together with GeneratedMessageV3.BuilderParent. */
// /** TODO: remove this together with GeneratedMessageV3.BuilderParent. */
protected abstract Message.Builder newBuilderForType(BuilderParent parent);
/** TODO: generated class should implement this directly */
@Override
protected Message.Builder newBuilderForType(final AbstractMessage.BuilderParent parent) {
return newBuilderForType(
new BuilderParent() {
@Override
public void markDirty() {
parent.markDirty();
}
});
protected Message.Builder newBuilderForType(GeneratedMessage.BuilderParent parent) {
// if (parent instanceof GeneratedMessageV3.BuilderParent) {
return newBuilderForType((GeneratedMessageV3.BuilderParent) parent);
// }
// throw new UnsupportedOperationException("Should be overridden");
}
/** Builder class for {@link GeneratedMessageV3}. */
@SuppressWarnings("unchecked")
public abstract static class Builder<BuilderT extends Builder<BuilderT>>
extends AbstractMessage.Builder<BuilderT> {
extends GeneratedMessage.Builder<BuilderT> {
private BuilderParent builderParent;
@ -913,18 +909,6 @@ public abstract class GeneratedMessageV3 extends AbstractMessage implements Seri
return meAsParent;
}
/**
* Called when a builder or one of its nested children has changed and any parent should be
* notified of its invalidation.
*/
protected final void onChanged() {
if (isClean && builderParent != null) {
builderParent.markDirty();
// Don't keep dispatching invalidations until build is called again.
isClean = false;
}
}
/**
* Gets the map field with the given field number. This method should be overridden in the
@ -2056,7 +2040,7 @@ public abstract class GeneratedMessageV3 extends AbstractMessage implements Seri
* Users should ignore this class. This class provides the implementation with access to the
* fields of a message object using Java reflection.
*/
public static final class FieldAccessorTable {
public static final class FieldAccessorTable extends GeneratedMessage.FieldAccessorTable {
/**
* Construct a FieldAccessorTable for a particular message class. Only one FieldAccessorTable
@ -2082,6 +2066,7 @@ public abstract class GeneratedMessageV3 extends AbstractMessage implements Seri
* FieldAccessors.
*/
public FieldAccessorTable(final Descriptor descriptor, final String[] camelCaseNames) {
super(descriptor, camelCaseNames);
this.descriptor = descriptor;
this.camelCaseNames = camelCaseNames;
fields = new FieldAccessor[descriptor.getFields().size()];
@ -2096,7 +2081,16 @@ public abstract class GeneratedMessageV3 extends AbstractMessage implements Seri
* @param builderClass The builder type.
* @return this
*/
@Override
@SuppressWarnings("unchecked")
public FieldAccessorTable ensureFieldAccessorsInitialized(
Class<? extends GeneratedMessage> messageClass, Class<? extends GeneratedMessage.Builder> builderClass) {
// if (messageClass.isAssignableFrom(GeneratedMessageV3.class))
return ensureFieldAccessorsInitializedV3((Class<? extends GeneratedMessageV3>) messageClass, (Class<? extends Builder<?>>) builderClass);
}
public FieldAccessorTable ensureFieldAccessorsInitializedV3(
Class<? extends GeneratedMessageV3> messageClass, Class<? extends Builder<?>> builderClass) {
if (initialized) {
return this;

@ -37,13 +37,14 @@ import java.util.RandomAccess;
* @author jonp@google.com (Jon Perlow)
*/
public class RepeatedFieldBuilderV3<
MType extends AbstractMessage,
BType extends AbstractMessage.Builder,
MType extends GeneratedMessageV3,
BType extends GeneratedMessageV3.Builder,
IType extends MessageOrBuilder>
implements AbstractMessage.BuilderParent {
extends RepeatedFieldBuilder<MType, BType, IType>
implements GeneratedMessageV3.BuilderParent {
// Parent to send changes to.
private AbstractMessage.BuilderParent parent;
private GeneratedMessageV3.BuilderParent parent;
// List of messages. Never null. It may be immutable, in which case
// isMessagesListMutable will be false. See note below.
@ -104,12 +105,13 @@ public class RepeatedFieldBuilderV3<
public RepeatedFieldBuilderV3(
List<MType> messages,
boolean isMessagesListMutable,
AbstractMessage.BuilderParent parent,
GeneratedMessageV3.BuilderParent parent,
boolean isClean) {
this.messages = messages;
this.isMessagesListMutable = isMessagesListMutable;
this.parent = parent;
this.isClean = isClean;
super(messages, isMessagesListMutable, parent, isClean);
// this.messages = messages;
// this.isMessagesListMutable = isMessagesListMutable;
// this.parent = parent;
// this.isClean = isClean;
}
public void dispose() {
@ -318,7 +320,7 @@ public class RepeatedFieldBuilderV3<
* @param values the messages to add
* @return the builder
*/
@CanIgnoreReturnValue
@CanIgnoreReturnValue
public RepeatedFieldBuilderV3<MType, BType, IType> addAllMessages(
Iterable<? extends MType> values) {
for (final MType value : values) {
@ -551,8 +553,8 @@ public class RepeatedFieldBuilderV3<
* @param <IType> the common interface for the message and the builder
*/
private static class MessageExternalList<
MType extends AbstractMessage,
BType extends AbstractMessage.Builder,
MType extends GeneratedMessageV3,
BType extends GeneratedMessageV3.Builder,
IType extends MessageOrBuilder>
extends AbstractList<MType> implements List<MType>, RandomAccess {
@ -585,8 +587,8 @@ public class RepeatedFieldBuilderV3<
* @param <IType> the common interface for the message and the builder
*/
private static class BuilderExternalList<
MType extends AbstractMessage,
BType extends AbstractMessage.Builder,
MType extends GeneratedMessageV3,
BType extends GeneratedMessageV3.Builder,
IType extends MessageOrBuilder>
extends AbstractList<BType> implements List<BType>, RandomAccess {
@ -619,8 +621,8 @@ public class RepeatedFieldBuilderV3<
* @param <IType> the common interface for the message and the builder
*/
private static class MessageOrBuilderExternalList<
MType extends AbstractMessage,
BType extends AbstractMessage.Builder,
MType extends GeneratedMessageV3,
BType extends GeneratedMessageV3.Builder,
IType extends MessageOrBuilder>
extends AbstractList<IType> implements List<IType>, RandomAccess {

@ -30,13 +30,14 @@ import static com.google.protobuf.Internal.checkNotNull;
* @author jonp@google.com (Jon Perlow)
*/
public class SingleFieldBuilderV3<
MType extends AbstractMessage,
BType extends AbstractMessage.Builder,
MType extends GeneratedMessageV3,
BType extends GeneratedMessageV3.Builder,
IType extends MessageOrBuilder>
implements AbstractMessage.BuilderParent {
extends SingleFieldBuilder<MType, BType, IType>
implements GeneratedMessageV3.BuilderParent {
// Parent to send changes to.
private AbstractMessage.BuilderParent parent;
private GeneratedMessageV3.BuilderParent parent;
// Invariant: one of builder or message fields must be non-null.
@ -53,7 +54,8 @@ public class SingleFieldBuilderV3<
// to dispatch dirty invalidations. See AbstractMessage.BuilderListener.
private boolean isClean;
public SingleFieldBuilderV3(MType message, AbstractMessage.BuilderParent parent, boolean isClean) {
public SingleFieldBuilderV3(MType message, GeneratedMessageV3.BuilderParent parent, boolean isClean) {
super(message, parent, isClean);
this.message = checkNotNull(message);
this.parent = parent;
this.isClean = isClean;

@ -161,7 +161,7 @@ public class RepeatedFieldBuilderV3Test {
}
private RepeatedFieldBuilderV3<TestAllTypes, TestAllTypes.Builder, TestAllTypesOrBuilder>
newRepeatedFieldBuilderV3(AbstractMessage.BuilderParent parent) {
newRepeatedFieldBuilderV3(GeneratedMessageV3.BuilderParent parent) {
return new RepeatedFieldBuilderV3<TestAllTypes, TestAllTypes.Builder, TestAllTypesOrBuilder>(
Collections.<TestAllTypes>emptyList(), false, parent, false);
}

@ -3871,7 +3871,7 @@ public final class TestUtil {
*
* @author jonp@google.com (Jon Perlow)
*/
public static class MockBuilderParent implements GeneratedMessage.BuilderParent {
public static class MockBuilderParent implements GeneratedMessageV3.BuilderParent {
private int invalidations;

Loading…
Cancel
Save