Skip to content

Commit 974107f

Browse files
mhansencopybara-github
authored andcommitted
Remove calls to registerSchemaOverride in TestSchemas and TestSchemasLite
I believe these are now vestigial and no-op. If we don't register these up-front, they'll get registered later in Protobuf.schemaFor. TestSchemas, TestSchemasLite, and the replacement Protobuf.schemaFor all use `new ManifestSchemaFactory()` to create the schema. So the behaviour should be the same. The tests pass without these. PiperOrigin-RevId: 814081139
1 parent bc0b635 commit 974107f

File tree

8 files changed

+11
-162
lines changed

8 files changed

+11
-162
lines changed

java/core/src/main/java/com/google/protobuf/Protobuf.java

Lines changed: 8 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -13,54 +13,40 @@
1313
import java.util.concurrent.ConcurrentHashMap;
1414
import java.util.concurrent.ConcurrentMap;
1515

16-
/**
17-
* Main runtime interface for protobuf. Applications should interact with this interface (rather
18-
* than directly accessing internal APIs) in order to perform operations on protobuf messages.
19-
*/
2016
@ExperimentalApi
2117
@CheckReturnValue
2218
final class Protobuf {
2319
private static final Protobuf INSTANCE = new Protobuf();
2420

2521
private final SchemaFactory schemaFactory;
2622

27-
// TODO: Consider using ClassValue instead.
23+
// TODO: b/341207042 - Consider using ClassValue instead.
2824
private final ConcurrentMap<Class<?>, Schema<?>> schemaCache =
2925
new ConcurrentHashMap<Class<?>, Schema<?>>();
3026

3127
/** Gets the singleton instance of the Protobuf runtime. */
32-
public static Protobuf getInstance() {
28+
static Protobuf getInstance() {
3329
return INSTANCE;
3430
}
3531

3632
/** Writes the given message to the target {@link Writer}. */
37-
public <T> void writeTo(T message, Writer writer) throws IOException {
33+
<T> void writeTo(T message, Writer writer) throws IOException {
3834
schemaFor(message).writeTo(message, writer);
3935
}
4036

4137
/** Reads fields from the given {@link Reader} and merges them into the message. */
42-
public <T> void mergeFrom(T message, Reader reader) throws IOException {
43-
mergeFrom(message, reader, ExtensionRegistryLite.getEmptyRegistry());
44-
}
45-
46-
/** Reads fields from the given {@link Reader} and merges them into the message. */
47-
public <T> void mergeFrom(T message, Reader reader, ExtensionRegistryLite extensionRegistry)
38+
<T> void mergeFrom(T message, Reader reader, ExtensionRegistryLite extensionRegistry)
4839
throws IOException {
4940
schemaFor(message).mergeFrom(message, reader, extensionRegistry);
5041
}
5142

52-
/** Marks repeated/map/extension/unknown fields as immutable. */
53-
public <T> void makeImmutable(T message) {
54-
schemaFor(message).makeImmutable(message);
55-
}
56-
5743
/** Checks if all required fields are set. */
5844
<T> boolean isInitialized(T message) {
5945
return schemaFor(message).isInitialized(message);
6046
}
6147

6248
/** Gets the schema for the given message type. */
63-
public <T> Schema<T> schemaFor(Class<T> messageType) {
49+
<T> Schema<T> schemaFor(Class<T> messageType) {
6450
checkNotNull(messageType, "messageType");
6551
@SuppressWarnings("unchecked")
6652
Schema<T> schema = (Schema<T>) schemaCache.get(messageType);
@@ -78,7 +64,7 @@ public <T> Schema<T> schemaFor(Class<T> messageType) {
7864

7965
/** Gets the schema for the given message. */
8066
@SuppressWarnings("unchecked")
81-
public <T> Schema<T> schemaFor(T message) {
67+
<T> Schema<T> schemaFor(T message) {
8268
return schemaFor((Class<T>) message.getClass());
8369
}
8470

@@ -90,7 +76,7 @@ public <T> Schema<T> schemaFor(T message) {
9076
* @return the previously registered schema, or {@code null} if the given schema was successfully
9177
* registered.
9278
*/
93-
public Schema<?> registerSchema(Class<?> messageType, Schema<?> schema) {
79+
private Schema<?> registerSchema(Class<?> messageType, Schema<?> schema) {
9480
checkNotNull(messageType, "messageType");
9581
checkNotNull(schema, "schema");
9682
return schemaCache.putIfAbsent(messageType, schema);
@@ -106,7 +92,7 @@ public Schema<?> registerSchema(Class<?> messageType, Schema<?> schema) {
10692
* previously.
10793
*/
10894
@CanIgnoreReturnValue
109-
public Schema<?> registerSchemaOverride(Class<?> messageType, Schema<?> schema) {
95+
Schema<?> registerSchemaOverride(Class<?> messageType, Schema<?> schema) {
11096
checkNotNull(messageType, "messageType");
11197
checkNotNull(schema, "schema");
11298
return schemaCache.put(messageType, schema);
@@ -115,14 +101,4 @@ public Schema<?> registerSchemaOverride(Class<?> messageType, Schema<?> schema)
115101
private Protobuf() {
116102
schemaFactory = new ManifestSchemaFactory();
117103
}
118-
119-
int getTotalSchemaSize() {
120-
int result = 0;
121-
for (Schema<?> schema : schemaCache.values()) {
122-
if (schema instanceof MessageSchema) {
123-
result += ((MessageSchema) schema).getSchemaSize();
124-
}
125-
}
126-
return result;
127-
}
128104
}

java/core/src/test/java/com/google/protobuf/BinaryProtocolTest.java

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,12 @@
1111

1212
import com.google.protobuf.testing.Proto2Testing.Proto2Message;
1313
import com.google.protobuf.testing.Proto3Testing.Proto3Message;
14-
import org.junit.Before;
1514
import org.junit.Test;
1615
import org.junit.runner.RunWith;
1716
import org.junit.runners.JUnit4;
1817

1918
@RunWith(JUnit4.class)
2019
public final class BinaryProtocolTest {
21-
@Before
22-
public void setup() {
23-
TestSchemas.registerGenericProto2Schemas();
24-
25-
Protobuf.getInstance()
26-
.registerSchemaOverride(Proto3Message.class, TestSchemas.genericProto3Schema);
27-
}
2820

2921
@Test
3022
public void proto3Roundtrip() throws Exception {

java/core/src/test/java/com/google/protobuf/CodedAdapterTest.java

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,12 @@
1212
import com.google.protobuf.testing.Proto2Testing.Proto2Message;
1313
import com.google.protobuf.testing.Proto3Testing.Proto3Message;
1414
import java.io.IOException;
15-
import org.junit.Before;
1615
import org.junit.Test;
1716
import org.junit.runner.RunWith;
1817
import org.junit.runners.JUnit4;
1918

2019
@RunWith(JUnit4.class)
2120
public final class CodedAdapterTest {
22-
@Before
23-
public void setup() {
24-
TestSchemas.registerGenericProto2Schemas();
25-
26-
Protobuf.getInstance()
27-
.registerSchemaOverride(Proto3Message.class, TestSchemas.genericProto3Schema);
28-
}
2921

3022
@Test
3123
public void proto3Roundtrip() throws Exception {

java/core/src/test/java/com/google/protobuf/Proto2LiteSchemaTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,5 @@ protected Schema<Proto2MessageLite> schema() {
2121

2222
@Override
2323
protected void registerSchemas() {
24-
TestSchemasLite.registerGenericProto2LiteSchemas();
2524
}
2625
}

java/core/src/test/java/com/google/protobuf/Proto3LiteSchemaTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,5 @@ protected Schema<Proto3MessageLite> schema() {
2121

2222
@Override
2323
protected void registerSchemas() {
24-
TestSchemasLite.registerGenericProto3LiteSchemas();
2524
}
2625
}

java/core/src/test/java/com/google/protobuf/Proto3SchemaTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
public class Proto3SchemaTest extends AbstractProto3SchemaTest {
1616
@Override
1717
protected void registerSchemas() {
18-
TestSchemas.registerGenericProto3Schemas();
1918
}
2019

2120
@Override

java/core/src/test/java/com/google/protobuf/TestSchemas.java

Lines changed: 0 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,8 @@
77

88
package com.google.protobuf;
99

10-
import com.google.protobuf.testing.Proto2Testing;
11-
import com.google.protobuf.testing.Proto2Testing.Proto2Empty;
1210
import com.google.protobuf.testing.Proto2Testing.Proto2Message;
13-
import com.google.protobuf.testing.Proto2Testing.Proto2MessageWithExtensions;
14-
import com.google.protobuf.testing.Proto2Testing.Proto2MessageWithMaps;
15-
import com.google.protobuf.testing.Proto3Testing.Proto3Empty;
1611
import com.google.protobuf.testing.Proto3Testing.Proto3Message;
17-
import com.google.protobuf.testing.Proto3Testing.Proto3MessageWithMaps;
1812

1913
/** Schemas to support testing. */
2014
public class TestSchemas {
@@ -27,49 +21,4 @@ private TestSchemas() {
2721
public static final Schema<Proto3Message> genericProto3Schema =
2822
new ManifestSchemaFactory().createSchema(Proto3Message.class);
2923

30-
public static void registerGenericProto2Schemas() {
31-
registerProto2Schemas();
32-
}
33-
34-
public static void registerGenericProto3Schemas() {
35-
registerProto3Schemas();
36-
}
37-
38-
private static void registerProto2Schemas() {
39-
Protobuf protobuf = Protobuf.getInstance();
40-
ManifestSchemaFactory factory = new ManifestSchemaFactory();
41-
protobuf.registerSchemaOverride(Proto2Message.class, factory.createSchema(Proto2Message.class));
42-
protobuf.registerSchemaOverride(
43-
Proto2Message.FieldGroup49.class, factory.createSchema(Proto2Message.FieldGroup49.class));
44-
protobuf.registerSchemaOverride(
45-
Proto2Message.FieldGroupList51.class,
46-
factory.createSchema(Proto2Message.FieldGroupList51.class));
47-
protobuf.registerSchemaOverride(
48-
Proto2Message.FieldGroup69.class, factory.createSchema(Proto2Message.FieldGroup69.class));
49-
protobuf.registerSchemaOverride(
50-
Proto2Message.RequiredNestedMessage.class,
51-
factory.createSchema(Proto2Message.RequiredNestedMessage.class));
52-
protobuf.registerSchemaOverride(
53-
Proto2Message.FieldRequiredGroup88.class,
54-
factory.createSchema(Proto2Message.FieldRequiredGroup88.class));
55-
protobuf.registerSchemaOverride(Proto2Empty.class, factory.createSchema(Proto2Empty.class));
56-
protobuf.registerSchemaOverride(
57-
Proto2MessageWithExtensions.class, factory.createSchema(Proto2MessageWithExtensions.class));
58-
protobuf.registerSchemaOverride(
59-
Proto2Testing.FieldGroup49.class, factory.createSchema(Proto2Testing.FieldGroup49.class));
60-
protobuf.registerSchemaOverride(
61-
Proto2Testing.FieldGroupList51.class,
62-
factory.createSchema(Proto2Testing.FieldGroupList51.class));
63-
protobuf.registerSchemaOverride(
64-
Proto2MessageWithMaps.class, factory.createSchema(Proto2MessageWithMaps.class));
65-
}
66-
67-
private static void registerProto3Schemas() {
68-
Protobuf protobuf = Protobuf.getInstance();
69-
ManifestSchemaFactory factory = new ManifestSchemaFactory();
70-
protobuf.registerSchemaOverride(Proto3Message.class, factory.createSchema(Proto3Message.class));
71-
protobuf.registerSchemaOverride(Proto3Empty.class, factory.createSchema(Proto3Empty.class));
72-
protobuf.registerSchemaOverride(
73-
Proto3MessageWithMaps.class, factory.createSchema(Proto3MessageWithMaps.class));
74-
}
7524
}

java/core/src/test/java/com/google/protobuf/TestSchemasLite.java

Lines changed: 3 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -7,74 +7,17 @@
77

88
package com.google.protobuf;
99

10-
import com.google.protobuf.testing.Proto2TestingLite;
11-
import com.google.protobuf.testing.Proto2TestingLite.Proto2EmptyLite;
1210
import com.google.protobuf.testing.Proto2TestingLite.Proto2MessageLite;
13-
import com.google.protobuf.testing.Proto2TestingLite.Proto2MessageLiteWithExtensions;
14-
import com.google.protobuf.testing.Proto2TestingLite.Proto2MessageLiteWithMaps;
15-
import com.google.protobuf.testing.Proto3TestingLite.Proto3EmptyLite;
1611
import com.google.protobuf.testing.Proto3TestingLite.Proto3MessageLite;
17-
import com.google.protobuf.testing.Proto3TestingLite.Proto3MessageLiteWithMaps;
1812

1913
/** Schemas to support testing. */
2014
public final class TestSchemasLite {
2115

16+
private TestSchemasLite() {
17+
}
18+
2219
public static final Schema<Proto2MessageLite> genericProto2LiteSchema =
2320
new ManifestSchemaFactory().createSchema(Proto2MessageLite.class);
2421
public static final Schema<Proto3MessageLite> genericProto3LiteSchema =
2522
new ManifestSchemaFactory().createSchema(Proto3MessageLite.class);
26-
27-
public static void registerGenericProto2LiteSchemas() {
28-
registerProto2LiteSchemas();
29-
}
30-
31-
public static void registerGenericProto3LiteSchemas() {
32-
registerProto3LiteSchemas();
33-
}
34-
35-
private static void registerProto2LiteSchemas() {
36-
Protobuf protobuf = Protobuf.getInstance();
37-
ManifestSchemaFactory factory = new ManifestSchemaFactory();
38-
protobuf.registerSchemaOverride(
39-
Proto2MessageLite.class, factory.createSchema(Proto2MessageLite.class));
40-
protobuf.registerSchemaOverride(
41-
Proto2MessageLite.FieldGroup49.class,
42-
factory.createSchema(Proto2MessageLite.FieldGroup49.class));
43-
protobuf.registerSchemaOverride(
44-
Proto2MessageLite.FieldGroupList51.class,
45-
factory.createSchema(Proto2MessageLite.FieldGroupList51.class));
46-
protobuf.registerSchemaOverride(
47-
Proto2MessageLite.FieldGroup69.class,
48-
factory.createSchema(Proto2MessageLite.FieldGroup69.class));
49-
protobuf.registerSchemaOverride(
50-
Proto2MessageLite.RequiredNestedMessage.class,
51-
factory.createSchema(Proto2MessageLite.RequiredNestedMessage.class));
52-
protobuf.registerSchemaOverride(
53-
Proto2MessageLite.FieldRequiredGroup88.class,
54-
factory.createSchema(Proto2MessageLite.FieldRequiredGroup88.class));
55-
protobuf.registerSchemaOverride(
56-
Proto2EmptyLite.class, factory.createSchema(Proto2EmptyLite.class));
57-
protobuf.registerSchemaOverride(
58-
Proto2MessageLiteWithExtensions.class,
59-
factory.createSchema(Proto2MessageLiteWithExtensions.class));
60-
protobuf.registerSchemaOverride(
61-
Proto2TestingLite.FieldGroup49.class,
62-
factory.createSchema(Proto2TestingLite.FieldGroup49.class));
63-
protobuf.registerSchemaOverride(
64-
Proto2TestingLite.FieldGroupList51.class,
65-
factory.createSchema(Proto2TestingLite.FieldGroupList51.class));
66-
protobuf.registerSchemaOverride(
67-
Proto2MessageLiteWithMaps.class, factory.createSchema(Proto2MessageLiteWithMaps.class));
68-
}
69-
70-
private static void registerProto3LiteSchemas() {
71-
Protobuf protobuf = Protobuf.getInstance();
72-
ManifestSchemaFactory factory = new ManifestSchemaFactory();
73-
protobuf.registerSchemaOverride(
74-
Proto3MessageLite.class, factory.createSchema(Proto3MessageLite.class));
75-
protobuf.registerSchemaOverride(
76-
Proto3EmptyLite.class, factory.createSchema(Proto3EmptyLite.class));
77-
protobuf.registerSchemaOverride(
78-
Proto3MessageLiteWithMaps.class, factory.createSchema(Proto3MessageLiteWithMaps.class));
79-
}
8023
}

0 commit comments

Comments
 (0)