PageRenderTime 26ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/thirdparty/breakpad/third_party/protobuf/protobuf/java/src/main/java/com/google/protobuf/ExtensionRegistry.java

http://github.com/tomahawk-player/tomahawk
Java | 266 lines | 127 code | 25 blank | 114 comment | 16 complexity | 9694c4b7a8769c82190145277c5cd258 MD5 | raw file
Possible License(s): LGPL-2.1, BSD-3-Clause, GPL-3.0, GPL-2.0
  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2008 Google Inc. All rights reserved.
  3. // http://code.google.com/p/protobuf/
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are
  7. // met:
  8. //
  9. // * Redistributions of source code must retain the above copyright
  10. // notice, this list of conditions and the following disclaimer.
  11. // * Redistributions in binary form must reproduce the above
  12. // copyright notice, this list of conditions and the following disclaimer
  13. // in the documentation and/or other materials provided with the
  14. // distribution.
  15. // * Neither the name of Google Inc. nor the names of its
  16. // contributors may be used to endorse or promote products derived from
  17. // this software without specific prior written permission.
  18. //
  19. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. package com.google.protobuf;
  31. import com.google.protobuf.Descriptors.Descriptor;
  32. import com.google.protobuf.Descriptors.FieldDescriptor;
  33. import java.util.Collections;
  34. import java.util.HashMap;
  35. import java.util.Map;
  36. /**
  37. * A table of known extensions, searchable by name or field number. When
  38. * parsing a protocol message that might have extensions, you must provide
  39. * an {@code ExtensionRegistry} in which you have registered any extensions
  40. * that you want to be able to parse. Otherwise, those extensions will just
  41. * be treated like unknown fields.
  42. *
  43. * <p>For example, if you had the {@code .proto} file:
  44. *
  45. * <pre>
  46. * option java_class = "MyProto";
  47. *
  48. * message Foo {
  49. * extensions 1000 to max;
  50. * }
  51. *
  52. * extend Foo {
  53. * optional int32 bar;
  54. * }
  55. * </pre>
  56. *
  57. * Then you might write code like:
  58. *
  59. * <pre>
  60. * ExtensionRegistry registry = ExtensionRegistry.newInstance();
  61. * registry.add(MyProto.bar);
  62. * MyProto.Foo message = MyProto.Foo.parseFrom(input, registry);
  63. * </pre>
  64. *
  65. * <p>Background:
  66. *
  67. * <p>You might wonder why this is necessary. Two alternatives might come to
  68. * mind. First, you might imagine a system where generated extensions are
  69. * automatically registered when their containing classes are loaded. This
  70. * is a popular technique, but is bad design; among other things, it creates a
  71. * situation where behavior can change depending on what classes happen to be
  72. * loaded. It also introduces a security vulnerability, because an
  73. * unprivileged class could cause its code to be called unexpectedly from a
  74. * privileged class by registering itself as an extension of the right type.
  75. *
  76. * <p>Another option you might consider is lazy parsing: do not parse an
  77. * extension until it is first requested, at which point the caller must
  78. * provide a type to use. This introduces a different set of problems. First,
  79. * it would require a mutex lock any time an extension was accessed, which
  80. * would be slow. Second, corrupt data would not be detected until first
  81. * access, at which point it would be much harder to deal with it. Third, it
  82. * could violate the expectation that message objects are immutable, since the
  83. * type provided could be any arbitrary message class. An unprivileged user
  84. * could take advantage of this to inject a mutable object into a message
  85. * belonging to privileged code and create mischief.
  86. *
  87. * @author kenton@google.com Kenton Varda
  88. */
  89. public final class ExtensionRegistry extends ExtensionRegistryLite {
  90. /** Construct a new, empty instance. */
  91. public static ExtensionRegistry newInstance() {
  92. return new ExtensionRegistry();
  93. }
  94. /** Get the unmodifiable singleton empty instance. */
  95. public static ExtensionRegistry getEmptyRegistry() {
  96. return EMPTY;
  97. }
  98. /** Returns an unmodifiable view of the registry. */
  99. @Override
  100. public ExtensionRegistry getUnmodifiable() {
  101. return new ExtensionRegistry(this);
  102. }
  103. /** A (Descriptor, Message) pair, returned by lookup methods. */
  104. public static final class ExtensionInfo {
  105. /** The extension's descriptor. */
  106. public final FieldDescriptor descriptor;
  107. /**
  108. * A default instance of the extension's type, if it has a message type.
  109. * Otherwise, {@code null}.
  110. */
  111. public final Message defaultInstance;
  112. private ExtensionInfo(final FieldDescriptor descriptor) {
  113. this.descriptor = descriptor;
  114. defaultInstance = null;
  115. }
  116. private ExtensionInfo(final FieldDescriptor descriptor,
  117. final Message defaultInstance) {
  118. this.descriptor = descriptor;
  119. this.defaultInstance = defaultInstance;
  120. }
  121. }
  122. /**
  123. * Find an extension by fully-qualified field name, in the proto namespace.
  124. * I.e. {@code result.descriptor.fullName()} will match {@code fullName} if
  125. * a match is found.
  126. *
  127. * @return Information about the extension if found, or {@code null}
  128. * otherwise.
  129. */
  130. public ExtensionInfo findExtensionByName(final String fullName) {
  131. return extensionsByName.get(fullName);
  132. }
  133. /**
  134. * Find an extension by containing type and field number.
  135. *
  136. * @return Information about the extension if found, or {@code null}
  137. * otherwise.
  138. */
  139. public ExtensionInfo findExtensionByNumber(final Descriptor containingType,
  140. final int fieldNumber) {
  141. return extensionsByNumber.get(
  142. new DescriptorIntPair(containingType, fieldNumber));
  143. }
  144. /** Add an extension from a generated file to the registry. */
  145. public void add(final GeneratedMessage.GeneratedExtension<?, ?> extension) {
  146. if (extension.getDescriptor().getJavaType() ==
  147. FieldDescriptor.JavaType.MESSAGE) {
  148. if (extension.getMessageDefaultInstance() == null) {
  149. throw new IllegalStateException(
  150. "Registered message-type extension had null default instance: " +
  151. extension.getDescriptor().getFullName());
  152. }
  153. add(new ExtensionInfo(extension.getDescriptor(),
  154. extension.getMessageDefaultInstance()));
  155. } else {
  156. add(new ExtensionInfo(extension.getDescriptor(), null));
  157. }
  158. }
  159. /** Add a non-message-type extension to the registry by descriptor. */
  160. public void add(final FieldDescriptor type) {
  161. if (type.getJavaType() == FieldDescriptor.JavaType.MESSAGE) {
  162. throw new IllegalArgumentException(
  163. "ExtensionRegistry.add() must be provided a default instance when " +
  164. "adding an embedded message extension.");
  165. }
  166. add(new ExtensionInfo(type, null));
  167. }
  168. /** Add a message-type extension to the registry by descriptor. */
  169. public void add(final FieldDescriptor type, final Message defaultInstance) {
  170. if (type.getJavaType() != FieldDescriptor.JavaType.MESSAGE) {
  171. throw new IllegalArgumentException(
  172. "ExtensionRegistry.add() provided a default instance for a " +
  173. "non-message extension.");
  174. }
  175. add(new ExtensionInfo(type, defaultInstance));
  176. }
  177. // =================================================================
  178. // Private stuff.
  179. private ExtensionRegistry() {
  180. this.extensionsByName = new HashMap<String, ExtensionInfo>();
  181. this.extensionsByNumber = new HashMap<DescriptorIntPair, ExtensionInfo>();
  182. }
  183. private ExtensionRegistry(ExtensionRegistry other) {
  184. super(other);
  185. this.extensionsByName = Collections.unmodifiableMap(other.extensionsByName);
  186. this.extensionsByNumber =
  187. Collections.unmodifiableMap(other.extensionsByNumber);
  188. }
  189. private final Map<String, ExtensionInfo> extensionsByName;
  190. private final Map<DescriptorIntPair, ExtensionInfo> extensionsByNumber;
  191. private ExtensionRegistry(boolean empty) {
  192. super(ExtensionRegistryLite.getEmptyRegistry());
  193. this.extensionsByName = Collections.<String, ExtensionInfo>emptyMap();
  194. this.extensionsByNumber =
  195. Collections.<DescriptorIntPair, ExtensionInfo>emptyMap();
  196. }
  197. private static final ExtensionRegistry EMPTY = new ExtensionRegistry(true);
  198. private void add(final ExtensionInfo extension) {
  199. if (!extension.descriptor.isExtension()) {
  200. throw new IllegalArgumentException(
  201. "ExtensionRegistry.add() was given a FieldDescriptor for a regular " +
  202. "(non-extension) field.");
  203. }
  204. extensionsByName.put(extension.descriptor.getFullName(), extension);
  205. extensionsByNumber.put(
  206. new DescriptorIntPair(extension.descriptor.getContainingType(),
  207. extension.descriptor.getNumber()),
  208. extension);
  209. final FieldDescriptor field = extension.descriptor;
  210. if (field.getContainingType().getOptions().getMessageSetWireFormat() &&
  211. field.getType() == FieldDescriptor.Type.MESSAGE &&
  212. field.isOptional() &&
  213. field.getExtensionScope() == field.getMessageType()) {
  214. // This is an extension of a MessageSet type defined within the extension
  215. // type's own scope. For backwards-compatibility, allow it to be looked
  216. // up by type name.
  217. extensionsByName.put(field.getMessageType().getFullName(), extension);
  218. }
  219. }
  220. /** A (GenericDescriptor, int) pair, used as a map key. */
  221. private static final class DescriptorIntPair {
  222. private final Descriptor descriptor;
  223. private final int number;
  224. DescriptorIntPair(final Descriptor descriptor, final int number) {
  225. this.descriptor = descriptor;
  226. this.number = number;
  227. }
  228. @Override
  229. public int hashCode() {
  230. return descriptor.hashCode() * ((1 << 16) - 1) + number;
  231. }
  232. @Override
  233. public boolean equals(final Object obj) {
  234. if (!(obj instanceof DescriptorIntPair)) {
  235. return false;
  236. }
  237. final DescriptorIntPair other = (DescriptorIntPair)obj;
  238. return descriptor == other.descriptor && number == other.number;
  239. }
  240. }
  241. }