Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 8 additions & 32 deletions java/core/src/main/java/com/google/protobuf/Protobuf.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,54 +13,40 @@
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

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

private final SchemaFactory schemaFactory;

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

/** Gets the singleton instance of the Protobuf runtime. */
public static Protobuf getInstance() {
static Protobuf getInstance() {
return INSTANCE;
}

/** Writes the given message to the target {@link Writer}. */
public <T> void writeTo(T message, Writer writer) throws IOException {
<T> void writeTo(T message, Writer writer) throws IOException {
schemaFor(message).writeTo(message, writer);
}

/** Reads fields from the given {@link Reader} and merges them into the message. */
public <T> void mergeFrom(T message, Reader reader) throws IOException {
mergeFrom(message, reader, ExtensionRegistryLite.getEmptyRegistry());
}

/** Reads fields from the given {@link Reader} and merges them into the message. */
public <T> void mergeFrom(T message, Reader reader, ExtensionRegistryLite extensionRegistry)
<T> void mergeFrom(T message, Reader reader, ExtensionRegistryLite extensionRegistry)
throws IOException {
schemaFor(message).mergeFrom(message, reader, extensionRegistry);
}

/** Marks repeated/map/extension/unknown fields as immutable. */
public <T> void makeImmutable(T message) {
schemaFor(message).makeImmutable(message);
}

/** Checks if all required fields are set. */
<T> boolean isInitialized(T message) {
return schemaFor(message).isInitialized(message);
}

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

/** Gets the schema for the given message. */
@SuppressWarnings("unchecked")
public <T> Schema<T> schemaFor(T message) {
<T> Schema<T> schemaFor(T message) {
return schemaFor((Class<T>) message.getClass());
}

Expand All @@ -90,7 +76,7 @@ public <T> Schema<T> schemaFor(T message) {
* @return the previously registered schema, or {@code null} if the given schema was successfully
* registered.
*/
public Schema<?> registerSchema(Class<?> messageType, Schema<?> schema) {
private Schema<?> registerSchema(Class<?> messageType, Schema<?> schema) {
checkNotNull(messageType, "messageType");
checkNotNull(schema, "schema");
return schemaCache.putIfAbsent(messageType, schema);
Expand All @@ -106,7 +92,7 @@ public Schema<?> registerSchema(Class<?> messageType, Schema<?> schema) {
* previously.
*/
@CanIgnoreReturnValue
public Schema<?> registerSchemaOverride(Class<?> messageType, Schema<?> schema) {
Schema<?> registerSchemaOverride(Class<?> messageType, Schema<?> schema) {
checkNotNull(messageType, "messageType");
checkNotNull(schema, "schema");
return schemaCache.put(messageType, schema);
Expand All @@ -115,14 +101,4 @@ public Schema<?> registerSchemaOverride(Class<?> messageType, Schema<?> schema)
private Protobuf() {
schemaFactory = new ManifestSchemaFactory();
}

int getTotalSchemaSize() {
int result = 0;
for (Schema<?> schema : schemaCache.values()) {
if (schema instanceof MessageSchema) {
result += ((MessageSchema) schema).getSchemaSize();
}
}
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,12 @@

import com.google.protobuf.testing.Proto2Testing.Proto2Message;
import com.google.protobuf.testing.Proto3Testing.Proto3Message;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

@RunWith(JUnit4.class)
public final class BinaryProtocolTest {
@Before
public void setup() {
TestSchemas.registerGenericProto2Schemas();

Protobuf.getInstance()
.registerSchemaOverride(Proto3Message.class, TestSchemas.genericProto3Schema);
}

@Test
public void proto3Roundtrip() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,12 @@
import com.google.protobuf.testing.Proto2Testing.Proto2Message;
import com.google.protobuf.testing.Proto3Testing.Proto3Message;
import java.io.IOException;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

@RunWith(JUnit4.class)
public final class CodedAdapterTest {
@Before
public void setup() {
TestSchemas.registerGenericProto2Schemas();

Protobuf.getInstance()
.registerSchemaOverride(Proto3Message.class, TestSchemas.genericProto3Schema);
}

@Test
public void proto3Roundtrip() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ public class Proto2ExtensionLookupSchemaTest {

@Before
public void setup() {
TestSchemas.registerGenericProto2Schemas();

data = new Proto2MessageFactory(10, 20, 1, 1).newMessage().toByteArray();
extensionRegistry = ExtensionRegistry.newInstance();
Proto2Testing.registerAllExtensions(extensionRegistry);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,5 @@ protected Schema<Proto2MessageLite> schema() {

@Override
protected void registerSchemas() {
TestSchemasLite.registerGenericProto2LiteSchemas();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,5 @@ protected Schema<Proto2Message> schema() {

@Override
protected void registerSchemas() {
TestSchemas.registerGenericProto2Schemas();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,5 @@ protected Schema<Proto3MessageLite> schema() {

@Override
protected void registerSchemas() {
TestSchemasLite.registerGenericProto3LiteSchemas();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
public class Proto3SchemaTest extends AbstractProto3SchemaTest {
@Override
protected void registerSchemas() {
TestSchemas.registerGenericProto3Schemas();
}

@Override
Expand Down
51 changes: 0 additions & 51 deletions java/core/src/test/java/com/google/protobuf/TestSchemas.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,8 @@

package com.google.protobuf;

import com.google.protobuf.testing.Proto2Testing;
import com.google.protobuf.testing.Proto2Testing.Proto2Empty;
import com.google.protobuf.testing.Proto2Testing.Proto2Message;
import com.google.protobuf.testing.Proto2Testing.Proto2MessageWithExtensions;
import com.google.protobuf.testing.Proto2Testing.Proto2MessageWithMaps;
import com.google.protobuf.testing.Proto3Testing.Proto3Empty;
import com.google.protobuf.testing.Proto3Testing.Proto3Message;
import com.google.protobuf.testing.Proto3Testing.Proto3MessageWithMaps;

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

public static void registerGenericProto2Schemas() {
registerProto2Schemas();
}

public static void registerGenericProto3Schemas() {
registerProto3Schemas();
}

private static void registerProto2Schemas() {
Protobuf protobuf = Protobuf.getInstance();
ManifestSchemaFactory factory = new ManifestSchemaFactory();
protobuf.registerSchemaOverride(Proto2Message.class, factory.createSchema(Proto2Message.class));
protobuf.registerSchemaOverride(
Proto2Message.FieldGroup49.class, factory.createSchema(Proto2Message.FieldGroup49.class));
protobuf.registerSchemaOverride(
Proto2Message.FieldGroupList51.class,
factory.createSchema(Proto2Message.FieldGroupList51.class));
protobuf.registerSchemaOverride(
Proto2Message.FieldGroup69.class, factory.createSchema(Proto2Message.FieldGroup69.class));
protobuf.registerSchemaOverride(
Proto2Message.RequiredNestedMessage.class,
factory.createSchema(Proto2Message.RequiredNestedMessage.class));
protobuf.registerSchemaOverride(
Proto2Message.FieldRequiredGroup88.class,
factory.createSchema(Proto2Message.FieldRequiredGroup88.class));
protobuf.registerSchemaOverride(Proto2Empty.class, factory.createSchema(Proto2Empty.class));
protobuf.registerSchemaOverride(
Proto2MessageWithExtensions.class, factory.createSchema(Proto2MessageWithExtensions.class));
protobuf.registerSchemaOverride(
Proto2Testing.FieldGroup49.class, factory.createSchema(Proto2Testing.FieldGroup49.class));
protobuf.registerSchemaOverride(
Proto2Testing.FieldGroupList51.class,
factory.createSchema(Proto2Testing.FieldGroupList51.class));
protobuf.registerSchemaOverride(
Proto2MessageWithMaps.class, factory.createSchema(Proto2MessageWithMaps.class));
}

private static void registerProto3Schemas() {
Protobuf protobuf = Protobuf.getInstance();
ManifestSchemaFactory factory = new ManifestSchemaFactory();
protobuf.registerSchemaOverride(Proto3Message.class, factory.createSchema(Proto3Message.class));
protobuf.registerSchemaOverride(Proto3Empty.class, factory.createSchema(Proto3Empty.class));
protobuf.registerSchemaOverride(
Proto3MessageWithMaps.class, factory.createSchema(Proto3MessageWithMaps.class));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,74 +7,16 @@

package com.google.protobuf;

import com.google.protobuf.testing.Proto2TestingLite;
import com.google.protobuf.testing.Proto2TestingLite.Proto2EmptyLite;
import com.google.protobuf.testing.Proto2TestingLite.Proto2MessageLite;
import com.google.protobuf.testing.Proto2TestingLite.Proto2MessageLiteWithExtensions;
import com.google.protobuf.testing.Proto2TestingLite.Proto2MessageLiteWithMaps;
import com.google.protobuf.testing.Proto3TestingLite.Proto3EmptyLite;
import com.google.protobuf.testing.Proto3TestingLite.Proto3MessageLite;
import com.google.protobuf.testing.Proto3TestingLite.Proto3MessageLiteWithMaps;

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

private TestSchemasLite() {}

public static final Schema<Proto2MessageLite> genericProto2LiteSchema =
new ManifestSchemaFactory().createSchema(Proto2MessageLite.class);
public static final Schema<Proto3MessageLite> genericProto3LiteSchema =
new ManifestSchemaFactory().createSchema(Proto3MessageLite.class);

public static void registerGenericProto2LiteSchemas() {
registerProto2LiteSchemas();
}

public static void registerGenericProto3LiteSchemas() {
registerProto3LiteSchemas();
}

private static void registerProto2LiteSchemas() {
Protobuf protobuf = Protobuf.getInstance();
ManifestSchemaFactory factory = new ManifestSchemaFactory();
protobuf.registerSchemaOverride(
Proto2MessageLite.class, factory.createSchema(Proto2MessageLite.class));
protobuf.registerSchemaOverride(
Proto2MessageLite.FieldGroup49.class,
factory.createSchema(Proto2MessageLite.FieldGroup49.class));
protobuf.registerSchemaOverride(
Proto2MessageLite.FieldGroupList51.class,
factory.createSchema(Proto2MessageLite.FieldGroupList51.class));
protobuf.registerSchemaOverride(
Proto2MessageLite.FieldGroup69.class,
factory.createSchema(Proto2MessageLite.FieldGroup69.class));
protobuf.registerSchemaOverride(
Proto2MessageLite.RequiredNestedMessage.class,
factory.createSchema(Proto2MessageLite.RequiredNestedMessage.class));
protobuf.registerSchemaOverride(
Proto2MessageLite.FieldRequiredGroup88.class,
factory.createSchema(Proto2MessageLite.FieldRequiredGroup88.class));
protobuf.registerSchemaOverride(
Proto2EmptyLite.class, factory.createSchema(Proto2EmptyLite.class));
protobuf.registerSchemaOverride(
Proto2MessageLiteWithExtensions.class,
factory.createSchema(Proto2MessageLiteWithExtensions.class));
protobuf.registerSchemaOverride(
Proto2TestingLite.FieldGroup49.class,
factory.createSchema(Proto2TestingLite.FieldGroup49.class));
protobuf.registerSchemaOverride(
Proto2TestingLite.FieldGroupList51.class,
factory.createSchema(Proto2TestingLite.FieldGroupList51.class));
protobuf.registerSchemaOverride(
Proto2MessageLiteWithMaps.class, factory.createSchema(Proto2MessageLiteWithMaps.class));
}

private static void registerProto3LiteSchemas() {
Protobuf protobuf = Protobuf.getInstance();
ManifestSchemaFactory factory = new ManifestSchemaFactory();
protobuf.registerSchemaOverride(
Proto3MessageLite.class, factory.createSchema(Proto3MessageLite.class));
protobuf.registerSchemaOverride(
Proto3EmptyLite.class, factory.createSchema(Proto3EmptyLite.class));
protobuf.registerSchemaOverride(
Proto3MessageLiteWithMaps.class, factory.createSchema(Proto3MessageLiteWithMaps.class));
}
}
Loading