ExtensionRegistryLite.java
Go to the documentation of this file.
1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc. All rights reserved.
3 // https://developers.google.com/protocol-buffers/
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 
31 package com.google.protobuf;
32 
33 import java.util.Collections;
34 import java.util.HashMap;
35 import java.util.Map;
36 
70 public class ExtensionRegistryLite {
71 
72  // Set true to enable lazy parsing feature for MessageSet.
73  //
74  // TODO(xiangl): Now we use a global flag to control whether enable lazy
75  // parsing feature for MessageSet, which may be too crude for some
76  // applications. Need to support this feature on smaller granularity.
77  private static volatile boolean eagerlyParseMessageSets = false;
78 
79  // short circuit the ExtensionRegistryFactory via assumevalues trickery
80  @SuppressWarnings("JavaOptionalSuggestions")
81  private static boolean doFullRuntimeInheritanceCheck = true;
82 
83  // Visible for testing.
84  static final String EXTENSION_CLASS_NAME = "com.google.protobuf.Extension";
85 
86  /* @Nullable */
87  static Class<?> resolveExtensionClass() {
88  try {
89  return Class.forName(EXTENSION_CLASS_NAME);
90  } catch (ClassNotFoundException e) {
91  // See comment in ExtensionRegistryFactory on the potential expense of this.
92  return null;
93  }
94  }
95 
96  /* @Nullable */
97  private static final Class<?> extensionClass = resolveExtensionClass();
98 
99  public static boolean isEagerlyParseMessageSets() {
101  }
102 
103  public static void setEagerlyParseMessageSets(boolean isEagerlyParse) {
104  eagerlyParseMessageSets = isEagerlyParse;
105  }
106 
115  ? ExtensionRegistryFactory.create()
116  : new ExtensionRegistryLite();
117  }
118 
119  private static volatile ExtensionRegistryLite emptyRegistry;
120 
127  if (result == null) {
128  synchronized (ExtensionRegistryLite.class) {
129  result = emptyRegistry;
130  if (result == null) {
131  result =
132  emptyRegistry =
134  ? ExtensionRegistryFactory.createEmpty()
135  : EMPTY_REGISTRY_LITE;
136  }
137  }
138  }
139  return result;
140  }
141 
142 
145  return new ExtensionRegistryLite(this);
146  }
147 
153  @SuppressWarnings("unchecked")
154  public <ContainingType extends MessageLite>
155  GeneratedMessageLite.GeneratedExtension<ContainingType, ?> findLiteExtensionByNumber(
156  final ContainingType containingTypeDefaultInstance, final int fieldNumber) {
157  return (GeneratedMessageLite.GeneratedExtension<ContainingType, ?>)
158  extensionsByNumber.get(new ObjectIntPair(containingTypeDefaultInstance, fieldNumber));
159  }
160 
162  public final void add(final GeneratedMessageLite.GeneratedExtension<?, ?> extension) {
163  extensionsByNumber.put(
164  new ObjectIntPair(extension.getContainingTypeDefaultInstance(), extension.getNumber()),
165  extension);
166  }
167 
172  public final void add(ExtensionLite<?, ?> extension) {
173  if (GeneratedMessageLite.GeneratedExtension.class.isAssignableFrom(extension.getClass())) {
175  }
176  if (doFullRuntimeInheritanceCheck && ExtensionRegistryFactory.isFullRegistry(this)) {
177  try {
178  this.getClass().getMethod("add", extensionClass).invoke(this, extension);
179  } catch (Exception e) {
180  throw new IllegalArgumentException(
181  String.format("Could not invoke ExtensionRegistry#add for %s", extension), e);
182  }
183  }
184  }
185 
186  // =================================================================
187  // Private stuff.
188 
189  // Constructors are package-private so that ExtensionRegistry can subclass
190  // this.
191 
193  this.extensionsByNumber =
194  new HashMap<ObjectIntPair, GeneratedMessageLite.GeneratedExtension<?, ?>>();
195  }
196 
197  static final ExtensionRegistryLite EMPTY_REGISTRY_LITE = new ExtensionRegistryLite(true);
198 
199  ExtensionRegistryLite(ExtensionRegistryLite other) {
200  if (other == EMPTY_REGISTRY_LITE) {
201  this.extensionsByNumber = Collections.emptyMap();
202  } else {
203  this.extensionsByNumber = Collections.unmodifiableMap(other.extensionsByNumber);
204  }
205  }
206 
207  private final Map<ObjectIntPair, GeneratedMessageLite.GeneratedExtension<?, ?>>
209 
210  ExtensionRegistryLite(boolean empty) {
211  this.extensionsByNumber = Collections.emptyMap();
212  }
213 
215  private static final class ObjectIntPair {
216  private final Object object;
217  private final int number;
218 
219  ObjectIntPair(final Object object, final int number) {
220  this.object = object;
221  this.number = number;
222  }
223 
224  @Override
225  public int hashCode() {
226  return System.identityHashCode(object) * ((1 << 16) - 1) + number;
227  }
228 
229  @Override
230  public boolean equals(final Object obj) {
231  if (!(obj instanceof ObjectIntPair)) {
232  return false;
233  }
234  final ObjectIntPair other = (ObjectIntPair) obj;
235  return object == other.object && number == other.number;
236  }
237  }
238 }
Map
Definition: ruby/ext/google/protobuf_c/protobuf.h:442
google::protobuf::extension
const Descriptor::ReservedRange const EnumValueDescriptor const MethodDescriptor extension
Definition: src/google/protobuf/descriptor.h:2001
com.google.protobuf.GeneratedMessageLite
Definition: GeneratedMessageLite.java:60
com.google.protobuf.ExtensionRegistryLite.isEagerlyParseMessageSets
static boolean isEagerlyParseMessageSets()
Definition: ExtensionRegistryLite.java:99
com.google.protobuf.ExtensionRegistryLite.add
final void add(final GeneratedMessageLite.GeneratedExtension<?, ?> extension)
Definition: ExtensionRegistryLite.java:162
com.google.protobuf.ExtensionRegistryLite.add
final void add(ExtensionLite<?, ?> extension)
Definition: ExtensionRegistryLite.java:172
com.google.protobuf.ExtensionRegistryLite.ObjectIntPair
Definition: ExtensionRegistryLite.java:215
com.google.protobuf.ExtensionRegistryLite.newInstance
static ExtensionRegistryLite newInstance()
Definition: ExtensionRegistryLite.java:113
com.google.protobuf.GeneratedMessage.GeneratedExtension
Definition: GeneratedMessage.java:1757
obj
GLsizei GLsizei GLuint * obj
Definition: glcorearb.h:3066
com.google.protobuf.ExtensionRegistryLite.ObjectIntPair.equals
boolean equals(final Object obj)
Definition: ExtensionRegistryLite.java:230
com.google.protobuf.ExtensionRegistryLite
Definition: ExtensionRegistryLite.java:70
java
com.google.protobuf.ExtensionRegistryLite.extensionsByNumber
final Map< ObjectIntPair, GeneratedMessageLite.GeneratedExtension<?, ?> > extensionsByNumber
Definition: ExtensionRegistryLite.java:208
com.google.protobuf.ExtensionRegistryLite.getUnmodifiable
ExtensionRegistryLite getUnmodifiable()
Definition: ExtensionRegistryLite.java:144
com.google.protobuf.ExtensionRegistryLite.ObjectIntPair.hashCode
int hashCode()
Definition: ExtensionRegistryLite.java:225
com.google.protobuf.ExtensionRegistryLite.ObjectIntPair.object
final Object object
Definition: ExtensionRegistryLite.java:216
com.google.protobuf.ExtensionRegistryLite.doFullRuntimeInheritanceCheck
static boolean doFullRuntimeInheritanceCheck
Definition: ExtensionRegistryLite.java:81
com.google.protobuf.ExtensionRegistryLite.setEagerlyParseMessageSets
static void setEagerlyParseMessageSets(boolean isEagerlyParse)
Definition: ExtensionRegistryLite.java:103
com
com.google.protobuf.ExtensionRegistryLite.ObjectIntPair.number
final int number
Definition: ExtensionRegistryLite.java:217
com.google.protobuf.ExtensionLite
Definition: ExtensionLite.java:39
com.google.protobuf.Extension
Definition: Extension.java:39
com.google.protobuf.GeneratedMessageLite.GeneratedExtension
Definition: GeneratedMessageLite.java:1181
true
#define true
Definition: cJSON.c:65
com.google.protobuf.ExtensionRegistryLite.emptyRegistry
static volatile ExtensionRegistryLite emptyRegistry
Definition: ExtensionRegistryLite.java:119
com.google.protobuf.ExtensionRegistryLite.extensionClass
static final Class<?> extensionClass
Definition: ExtensionRegistryLite.java:97
com.google.protobuf.ExtensionRegistryLite.eagerlyParseMessageSets
static volatile boolean eagerlyParseMessageSets
Definition: ExtensionRegistryLite.java:77
com.google.protobuf.ExtensionRegistryLite.getEmptyRegistry
static ExtensionRegistryLite getEmptyRegistry()
Definition: ExtensionRegistryLite.java:125
google
Definition: data_proto2_to_proto3_util.h:11
com.google.protobuf.MessageLite
Definition: MessageLite.java:65


libaditof
Author(s):
autogenerated on Wed May 21 2025 02:06:51