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
Loading