| Index: third_party/protobuf/java/core/src/main/java/com/google/protobuf/ExtensionRegistryLite.java
|
| diff --git a/third_party/protobuf/java/core/src/main/java/com/google/protobuf/ExtensionRegistryLite.java b/third_party/protobuf/java/core/src/main/java/com/google/protobuf/ExtensionRegistryLite.java
|
| index 65cf73857642534c0f368229919c6e7fb5ff1656..f3d48d3af34e7e3eeb71601bfeca4c2561038a9a 100644
|
| --- a/third_party/protobuf/java/core/src/main/java/com/google/protobuf/ExtensionRegistryLite.java
|
| +++ b/third_party/protobuf/java/core/src/main/java/com/google/protobuf/ExtensionRegistryLite.java
|
| @@ -79,6 +79,22 @@ public class ExtensionRegistryLite {
|
| // applications. Need to support this feature on smaller granularity.
|
| private static volatile boolean eagerlyParseMessageSets = false;
|
|
|
| + // Visible for testing.
|
| + static final String EXTENSION_CLASS_NAME = "com.google.protobuf.Extension";
|
| +
|
| + /* @Nullable */
|
| + static Class<?> resolveExtensionClass() {
|
| + try {
|
| + return Class.forName(EXTENSION_CLASS_NAME);
|
| + } catch (ClassNotFoundException e) {
|
| + // See comment in ExtensionRegistryFactory on the potential expense of this.
|
| + return null;
|
| + }
|
| + }
|
| +
|
| + /* @Nullable */
|
| + private static final Class<?> extensionClass = resolveExtensionClass();
|
| +
|
| public static boolean isEagerlyParseMessageSets() {
|
| return eagerlyParseMessageSets;
|
| }
|
| @@ -87,16 +103,25 @@ public class ExtensionRegistryLite {
|
| eagerlyParseMessageSets = isEagerlyParse;
|
| }
|
|
|
| - /** Construct a new, empty instance. */
|
| + /**
|
| + * Construct a new, empty instance.
|
| + *
|
| + * <p>This may be an {@code ExtensionRegistry} if the full (non-Lite) proto libraries are
|
| + * available.
|
| + */
|
| public static ExtensionRegistryLite newInstance() {
|
| - return new ExtensionRegistryLite();
|
| + return ExtensionRegistryFactory.create();
|
| }
|
|
|
| - /** Get the unmodifiable singleton empty instance. */
|
| + /**
|
| + * Get the unmodifiable singleton empty instance of either ExtensionRegistryLite or
|
| + * {@code ExtensionRegistry} (if the full (non-Lite) proto libraries are available).
|
| + */
|
| public static ExtensionRegistryLite getEmptyRegistry() {
|
| - return EMPTY;
|
| + return ExtensionRegistryFactory.createEmpty();
|
| }
|
|
|
| +
|
| /** Returns an unmodifiable view of the registry. */
|
| public ExtensionRegistryLite getUnmodifiable() {
|
| return new ExtensionRegistryLite(this);
|
| @@ -128,6 +153,23 @@ public class ExtensionRegistryLite {
|
| extension);
|
| }
|
|
|
| + /**
|
| + * Add an extension from a lite generated file to the registry only if it is
|
| + * a non-lite extension i.e. {@link GeneratedMessageLite.GeneratedExtension}. */
|
| + public final void add(ExtensionLite<?, ?> extension) {
|
| + if (GeneratedMessageLite.GeneratedExtension.class.isAssignableFrom(extension.getClass())) {
|
| + add((GeneratedMessageLite.GeneratedExtension<?, ?>) extension);
|
| + }
|
| + if (ExtensionRegistryFactory.isFullRegistry(this)) {
|
| + try {
|
| + this.getClass().getMethod("add", extensionClass).invoke(this, extension);
|
| + } catch (Exception e) {
|
| + throw new IllegalArgumentException(
|
| + String.format("Could not invoke ExtensionRegistry#add for %s", extension), e);
|
| + }
|
| + }
|
| + }
|
| +
|
| // =================================================================
|
| // Private stuff.
|
|
|
| @@ -139,9 +181,11 @@ public class ExtensionRegistryLite {
|
| new HashMap<ObjectIntPair,
|
| GeneratedMessageLite.GeneratedExtension<?, ?>>();
|
| }
|
| + static final ExtensionRegistryLite EMPTY_REGISTRY_LITE =
|
| + new ExtensionRegistryLite(true);
|
|
|
| ExtensionRegistryLite(ExtensionRegistryLite other) {
|
| - if (other == EMPTY) {
|
| + if (other == EMPTY_REGISTRY_LITE) {
|
| this.extensionsByNumber = Collections.emptyMap();
|
| } else {
|
| this.extensionsByNumber =
|
| @@ -153,11 +197,9 @@ public class ExtensionRegistryLite {
|
| GeneratedMessageLite.GeneratedExtension<?, ?>>
|
| extensionsByNumber;
|
|
|
| - private ExtensionRegistryLite(boolean empty) {
|
| + ExtensionRegistryLite(boolean empty) {
|
| this.extensionsByNumber = Collections.emptyMap();
|
| }
|
| - private static final ExtensionRegistryLite EMPTY =
|
| - new ExtensionRegistryLite(true);
|
|
|
| /** A (Object, int) pair, used as a map key. */
|
| private static final class ObjectIntPair {
|
|
|