Top Description Inners Fields Constructors Methods
com.google.protobuf

public Class ExtensionRegistry

extends ExtensionRegistryLite
Class Inheritance
Imports
com.google.protobuf.Descriptors.Descriptor, .Descriptors.FieldDescriptor, java.util.Collection, .Collections, .HashMap, .HashSet, .Map, .Set

A table of known extensions, searchable by name or field number. When parsing a protocol message that might have extensions, you must provide an ExtensionRegistry in which you have registered any extensions that you want to be able to parse. Otherwise, those extensions will just be treated like unknown fields.

For example, if you had the .proto file:

option java_class = "MyProto";

message Foo {
  extensions 1000 to max;
}

extend Foo {
  optional int32 bar;
}
Then you might write code like:
ExtensionRegistry registry = ExtensionRegistry.newInstance();
registry.add(MyProto.bar);
MyProto.Foo message = MyProto.Foo.parseFrom(input, registry);

Background:

You might wonder why this is necessary. Two alternatives might come to mind. First, you might imagine a system where generated extensions are automatically registered when their containing classes are loaded. This is a popular technique, but is bad design; among other things, it creates a situation where behavior can change depending on what classes happen to be loaded. It also introduces a security vulnerability, because an unprivileged class could cause its code to be called unexpectedly from a privileged class by registering itself as an extension of the right type.

Another option you might consider is lazy parsing: do not parse an extension until it is first requested, at which point the caller must provide a type to use. This introduces a different set of problems. First, it would require a mutex lock any time an extension was accessed, which would be slow. Second, corrupt data would not be detected until first access, at which point it would be much harder to deal with it. Third, it could violate the expectation that message objects are immutable, since the type provided could be any arbitrary message class. An unprivileged user could take advantage of this to inject a mutable object into a message belonging to privileged code and create mischief.

Author
[email protected] Kenton Varda

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
ExtensionRegistry.DescriptorIntPair

A (GenericDescriptor, int) pair, used as a map key.

public static class
ExtensionRegistry.ExtensionInfo

A (Descriptor, Message) pair, returned by lookup methods.

Field Summary

Modifier and TypeField and Description
pack-priv static final ExtensionRegistry
private final Map<String, ExtensionRegistry.ExtensionInfo>
private final Map<ExtensionRegistry.DescriptorIntPair, ExtensionRegistry.ExtensionInfo>
private final Map<String, ExtensionRegistry.ExtensionInfo>
private final Map<ExtensionRegistry.DescriptorIntPair, ExtensionRegistry.ExtensionInfo>
Inherited from com.google.protobuf.ExtensionRegistryLite:
EMPTY_REGISTRY_LITEEXTENSION_CLASS_NAME

Constructor Summary

AccessConstructor and Description
private
private
pack-priv
ExtensionRegistry(boolean empty)

Method Summary

Modifier and TypeMethod and Description
public void
add(final Extension<?, ?> extension)

Add an extension from a generated file to the registry.

public void
add(final GeneratedMessage.GeneratedExtension<?, ?> extension)

Add an extension from a generated file to the registry.

public void
add(final Descriptors.FieldDescriptor type)

Add a non-message-type extension to the registry by descriptor.

public void
add(final Descriptors.FieldDescriptor type, final Message defaultInstance)

Add a message-type extension to the registry by descriptor.

private void
add(final ExtensionRegistry.ExtensionInfo extension, final Extension.ExtensionType extensionType)

public ExtensionRegistry.ExtensionInfo
findExtensionByName(final String fullName)

Deprecated
Deprecated.
public ExtensionRegistry.ExtensionInfo
findExtensionByNumber(final Descriptors.Descriptor containingType, final int fieldNumber)

Deprecated
Deprecated.
public ExtensionRegistry.ExtensionInfo

Returns:

Information about the extension if found, or null otherwise.
findImmutableExtensionByName
(final String fullName)

Find an extension for immutable APIs by fully-qualified field name, in the proto namespace.

public ExtensionRegistry.ExtensionInfo

Returns:

Information about the extension if found, or null otherwise.
findImmutableExtensionByNumber
(final Descriptors.Descriptor containingType, final int fieldNumber)

Find an extension by containing type and field number for immutable APIs.

public ExtensionRegistry.ExtensionInfo

Returns:

Information about the extension if found, or null otherwise.
findMutableExtensionByName
(final String fullName)

Find an extension for mutable APIs by fully-qualified field name, in the proto namespace.

public ExtensionRegistry.ExtensionInfo

Returns:

Information about the extension if found, or null otherwise.
findMutableExtensionByNumber
(final Descriptors.Descriptor containingType, final int fieldNumber)

Find an extension by containing type and field number for mutable APIs.

public Set<ExtensionRegistry.ExtensionInfo>

Returns:

Information about the extensions found, or null if there are none.
getAllImmutableExtensionsByExtendedType
(final String fullName)

Find all extensions for immutable APIs by fully-qualified name of extended class.

public Set<ExtensionRegistry.ExtensionInfo>

Returns:

Information about the extensions found, or null if there are none.
getAllMutableExtensionsByExtendedType
(final String fullName)

Find all extensions for mutable APIs by fully-qualified name of extended class.

public static ExtensionRegistry
getEmptyRegistry()

Hides com.google.protobuf.ExtensionRegistryLite.getEmptyRegistry.

Get the unmodifiable singleton empty instance.
public ExtensionRegistry
getUnmodifiable()

Overrides com.google.protobuf.ExtensionRegistryLite.getUnmodifiable.

Returns an unmodifiable view of the registry.
pack-priv static ExtensionRegistry.ExtensionInfo
newExtensionInfo(final Extension<?, ?> extension)

public static ExtensionRegistry
newInstance()

Hides com.google.protobuf.ExtensionRegistryLite.newInstance.

Construct a new, empty instance.
Inherited from com.google.protobuf.ExtensionRegistryLite:
addaddfindLiteExtensionByNumberisEagerlyParseMessageSetsresolveExtensionClasssetEagerlyParseMessageSets

Field Detail

EMPTY_REGISTRYback to summary
pack-priv static final ExtensionRegistry EMPTY_REGISTRY
immutableExtensionsByNameback to summary
private final Map<String, ExtensionRegistry.ExtensionInfo> immutableExtensionsByName
immutableExtensionsByNumberback to summary
private final Map<ExtensionRegistry.DescriptorIntPair, ExtensionRegistry.ExtensionInfo> immutableExtensionsByNumber
mutableExtensionsByNameback to summary
private final Map<String, ExtensionRegistry.ExtensionInfo> mutableExtensionsByName
mutableExtensionsByNumberback to summary
private final Map<ExtensionRegistry.DescriptorIntPair, ExtensionRegistry.ExtensionInfo> mutableExtensionsByNumber

Constructor Detail

ExtensionRegistryback to summary
private ExtensionRegistry()
ExtensionRegistryback to summary
private ExtensionRegistry(ExtensionRegistry other)
ExtensionRegistryback to summary
pack-priv ExtensionRegistry(boolean empty)

Method Detail

addback to summary
public void add(final Extension<?, ?> extension)

Add an extension from a generated file to the registry.

addback to summary
public void add(final GeneratedMessage.GeneratedExtension<?, ?> extension)

Add an extension from a generated file to the registry.

addback to summary
public void add(final Descriptors.FieldDescriptor type)

Add a non-message-type extension to the registry by descriptor.

addback to summary
public void add(final Descriptors.FieldDescriptor type, final Message defaultInstance)

Add a message-type extension to the registry by descriptor.

addback to summary
private void add(final ExtensionRegistry.ExtensionInfo extension, final Extension.ExtensionType extensionType)
findExtensionByNameback to summary
public ExtensionRegistry.ExtensionInfo findExtensionByName(final String fullName)

Deprecated

Deprecated. Use findImmutableExtensionByName(String) instead.

Annotations
@Deprecated
findExtensionByNumberback to summary
public ExtensionRegistry.ExtensionInfo findExtensionByNumber(final Descriptors.Descriptor containingType, final int fieldNumber)

Deprecated

Deprecated. Use findImmutableExtensionByNumber(Descriptors.Descriptor, int)

Annotations
@Deprecated
findImmutableExtensionByNameback to summary
public ExtensionRegistry.ExtensionInfo findImmutableExtensionByName(final String fullName)

Find an extension for immutable APIs by fully-qualified field name, in the proto namespace. i.e. result.descriptor.fullName() will match fullName if a match is found.

Returns:ExtensionRegistry.ExtensionInfo

Information about the extension if found, or null otherwise.

findImmutableExtensionByNumberback to summary
public ExtensionRegistry.ExtensionInfo findImmutableExtensionByNumber(final Descriptors.Descriptor containingType, final int fieldNumber)

Find an extension by containing type and field number for immutable APIs.

Returns:ExtensionRegistry.ExtensionInfo

Information about the extension if found, or null otherwise.

findMutableExtensionByNameback to summary
public ExtensionRegistry.ExtensionInfo findMutableExtensionByName(final String fullName)

Find an extension for mutable APIs by fully-qualified field name, in the proto namespace. i.e. result.descriptor.fullName() will match fullName if a match is found.

Returns:ExtensionRegistry.ExtensionInfo

Information about the extension if found, or null otherwise.

findMutableExtensionByNumberback to summary
public ExtensionRegistry.ExtensionInfo findMutableExtensionByNumber(final Descriptors.Descriptor containingType, final int fieldNumber)

Find an extension by containing type and field number for mutable APIs.

Returns:ExtensionRegistry.ExtensionInfo

Information about the extension if found, or null otherwise.

getAllImmutableExtensionsByExtendedTypeback to summary
public Set<ExtensionRegistry.ExtensionInfo> getAllImmutableExtensionsByExtendedType(final String fullName)

Find all extensions for immutable APIs by fully-qualified name of extended class. Note that this method is more computationally expensive than getting a single extension by name or number.

Returns:Set<ExtensionRegistry.ExtensionInfo>

Information about the extensions found, or null if there are none.

getAllMutableExtensionsByExtendedTypeback to summary
public Set<ExtensionRegistry.ExtensionInfo> getAllMutableExtensionsByExtendedType(final String fullName)

Find all extensions for mutable APIs by fully-qualified name of extended class. Note that this method is more computationally expensive than getting a single extension by name or number.

Returns:Set<ExtensionRegistry.ExtensionInfo>

Information about the extensions found, or null if there are none.

getEmptyRegistryback to summary
public static ExtensionRegistry getEmptyRegistry()

Hides com.google.protobuf.ExtensionRegistryLite.getEmptyRegistry.

Get the unmodifiable singleton empty instance.

getUnmodifiableback to summary
public ExtensionRegistry getUnmodifiable()

Overrides com.google.protobuf.ExtensionRegistryLite.getUnmodifiable.

Returns an unmodifiable view of the registry.

Annotations
@Override
newExtensionInfoback to summary
pack-priv static ExtensionRegistry.ExtensionInfo newExtensionInfo(final Extension<?, ?> extension)
newInstanceback to summary
public static ExtensionRegistry newInstance()

Hides com.google.protobuf.ExtensionRegistryLite.newInstance.

Construct a new, empty instance.

com.google.protobuf back to summary

private final Class ExtensionRegistry.DescriptorIntPair

extends Object
Class Inheritance

A (GenericDescriptor, int) pair, used as a map key.

Field Summary

Modifier and TypeField and Description
private final Descriptors.Descriptor
private final int

Constructor Summary

AccessConstructor and Description
pack-priv
DescriptorIntPair(final Descriptors.Descriptor descriptor, final int number)

Method Summary

Modifier and TypeMethod and Description
public boolean
equals(final Object
the reference object with which to compare.
obj
)

Overrides java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.
public int
hashCode()

Overrides java.lang.Object.hashCode.

Returns a hash code value for the object.
Inherited from java.lang.Object:
clonefinalizegetClassnotifynotifyAlltoStringwaitwaitwait

Field Detail

descriptorback to summary
private final Descriptors.Descriptor descriptor
numberback to summary
private final int number

Constructor Detail

DescriptorIntPairback to summary
pack-priv DescriptorIntPair(final Descriptors.Descriptor descriptor, final int number)

Method Detail

equalsback to summary
public boolean equals(final Object obj)

Overrides java.lang.Object.equals.

Doc from java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.

The equals method implements an equivalence relation on non-null object references:

  • It is reflexive: for any non-null reference value x, x.equals(x) should return true.
  • It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
  • It is transitive: for any non-null reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.
  • It is consistent: for any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified.
  • For any non-null reference value x, x.equals(null) should return false.

An equivalence relation partitions the elements it operates on into equivalence classes; all the members of an equivalence class are equal to each other. Members of an equivalence class are substitutable for each other, at least for some purposes.

Parameters
obj:Object

the reference object with which to compare.

Returns:boolean

true if this object is the same as the obj argument; false otherwise.

Annotations
@Override
hashCodeback to summary
public int hashCode()

Overrides java.lang.Object.hashCode.

Doc from java.lang.Object.hashCode.

Returns a hash code value for the object. This method is supported for the benefit of hash tables such as those provided by java.util.HashMap.

The general contract of hashCode is:

  • Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
  • If two objects are equal according to the equals method, then calling the hashCode method on each of the two objects must produce the same integer result.
  • It is not required that if two objects are unequal according to the equals method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.
Returns:int

a hash code value for this object.

Annotations
@Override
com.google.protobuf back to summary

public final Class ExtensionRegistry.ExtensionInfo

extends Object
Class Inheritance

A (Descriptor, Message) pair, returned by lookup methods.

Field Summary

Modifier and TypeField and Description
public final Message
defaultInstance

A default instance of the extension's type, if it has a message type.

public final Descriptors.FieldDescriptor
descriptor

The extension's descriptor.

Constructor Summary

AccessConstructor and Description
private
private
ExtensionInfo(final Descriptors.FieldDescriptor descriptor, final Message defaultInstance)

Method Summary

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

defaultInstanceback to summary
public final Message defaultInstance

A default instance of the extension's type, if it has a message type. Otherwise, null.

descriptorback to summary
public final Descriptors.FieldDescriptor descriptor

The extension's descriptor.

Constructor Detail

ExtensionInfoback to summary
private ExtensionInfo(final Descriptors.FieldDescriptor descriptor)
ExtensionInfoback to summary
private ExtensionInfo(final Descriptors.FieldDescriptor descriptor, final Message defaultInstance)