-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExtensionRegistryLite.java
More file actions
229 lines (200 loc) · 8.43 KB
/
ExtensionRegistryLite.java
File metadata and controls
229 lines (200 loc) · 8.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
// Protocol Buffers - Google's data interchange format
// Copyright 2008 Google Inc. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd
package com.google.protobuf;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
/**
* Equivalent to {@link ExtensionRegistry} but supports only "lite" types.
*
* <p>If all of your types are lite types, then you only need to use {@code ExtensionRegistryLite}.
* Similarly, if all your types are regular types, then you only need {@link ExtensionRegistry}.
* Typically it does not make sense to mix the two, since if you have any regular types in your
* program, you then require the full runtime and lose all the benefits of the lite runtime, so you
* might as well make all your types be regular types. However, in some cases (e.g. when depending
* on multiple third-party libraries where one uses lite types and one uses regular), you may find
* yourself wanting to mix the two. In this case things get more complicated.
*
* <p>There are three factors to consider: Whether the type being extended is lite, whether the
* embedded type (in the case of a message-typed extension) is lite, and whether the extension
* itself is lite. Since all three are declared in different files, they could all be different.
* Here are all the combinations and which type of registry to use:
*
* <pre>
* Extended type Inner type Extension Use registry
* =======================================================================
* lite lite lite ExtensionRegistryLite
* lite regular lite ExtensionRegistry
* regular regular regular ExtensionRegistry
* all other combinations not supported
* </pre>
*
* <p>Note that just as regular types are not allowed to contain lite-type fields, they are also not
* allowed to contain lite-type extensions. This is because regular types must be fully accessible
* via reflection, which in turn means that all the inner messages must also support reflection. On
* the other hand, since regular types implement the entire lite interface, there is no problem with
* embedding regular types inside lite types.
*
* @author kenton@google.com Kenton Varda
*/
public class ExtensionRegistryLite {
// Set true to enable lazy parsing feature for MessageSet.
//
// TODO: Now we use a global flag to control whether enable lazy
// parsing feature for MessageSet, which may be too crude for some
// applications. Need to support this feature on smaller granularity.
private static volatile boolean eagerlyParseMessageSets = false;
// short circuit the ExtensionRegistryFactory via assumevalues trickery
@SuppressWarnings("JavaOptionalSuggestions")
private static boolean doFullRuntimeInheritanceCheck = true;
// Visible for testing.
static final String EXTENSION_CLASS_NAME = "com.google.protobuf.Extension";
private static class ExtensionClassHolder {
static final Class<?> INSTANCE = resolveExtensionClass();
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;
}
}
}
private static final Class<?> GENERATED_EXTENSION = getGeneratedExtensionClass();
private static Class<?> getGeneratedExtensionClass() {
try {
Class<?> generatedMessageLiteClass = Class.forName("com.google.protobuf.GeneratedMessageLite");
return Arrays.stream(generatedMessageLiteClass.getClasses())
.filter(innerClass -> innerClass.getName().equals("com.google.protobuf.GeneratedMessageLite$GeneratedExtension"))
.findFirst()
.get();
} catch (Throwable e) {
return null;
}
}
public static boolean isEagerlyParseMessageSets() {
return eagerlyParseMessageSets;
}
public static void setEagerlyParseMessageSets(boolean isEagerlyParse) {
eagerlyParseMessageSets = isEagerlyParse;
}
/**
* 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 doFullRuntimeInheritanceCheck
? ExtensionRegistryFactory.create()
: new ExtensionRegistryLite();
}
private static volatile ExtensionRegistryLite emptyRegistry;
/**
* 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() {
if (!doFullRuntimeInheritanceCheck) {
return EMPTY_REGISTRY_LITE;
}
ExtensionRegistryLite result = emptyRegistry;
if (result == null) {
synchronized (ExtensionRegistryLite.class) {
result = emptyRegistry;
if (result == null) {
result = emptyRegistry = ExtensionRegistryFactory.createEmpty();
}
}
}
return result;
}
/** Returns an unmodifiable view of the registry. */
public ExtensionRegistryLite getUnmodifiable() {
return new ExtensionRegistryLite(this);
}
/**
* Find an extension by containing type and field number.
*
* @return Information about the extension if found, or {@code null} otherwise.
*/
@SuppressWarnings("unchecked")
public <ContainingType extends MessageLite>
ExtensionLite<ContainingType, ?> findLiteExtensionByNumber(
final ContainingType containingTypeDefaultInstance, final int fieldNumber) {
return (ExtensionLite<ContainingType, ?>)
extensionsByNumber.get(new ObjectIntPair(containingTypeDefaultInstance, fieldNumber));
}
/** Add an extension from a lite generated file to the registry. */
// public final void add(final ExtensionLite<?, ?> extension) {
// extensionsByNumber.put(
// new ObjectIntPair(extension, extension.getNumber()),
// 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 (GENERATED_EXTENSION.isAssignableFrom(extension.getClass())) {
extensionsByNumber.put(
new ObjectIntPair(extension, extension.getNumber()),
extension);
}
if (doFullRuntimeInheritanceCheck && ExtensionRegistryFactory.isFullRegistry(this)) {
try {
this.getClass().getMethod("add", ExtensionClassHolder.INSTANCE).invoke(this, extension);
} catch (Exception e) {
throw new IllegalArgumentException(
String.format("Could not invoke ExtensionRegistry#add for %s", extension), e);
}
}
}
// =================================================================
// Private stuff.
// Constructors are package-private so that ExtensionRegistry can subclass
// this.
ExtensionRegistryLite() {
this.extensionsByNumber =
new HashMap<ObjectIntPair, ExtensionLite<?, ?>>();
}
static final ExtensionRegistryLite EMPTY_REGISTRY_LITE = new ExtensionRegistryLite(true);
ExtensionRegistryLite(ExtensionRegistryLite other) {
if (other == EMPTY_REGISTRY_LITE) {
this.extensionsByNumber = Collections.emptyMap();
} else {
this.extensionsByNumber = Collections.unmodifiableMap(other.extensionsByNumber);
}
}
private final Map<ObjectIntPair, ExtensionLite<?, ?>>
extensionsByNumber;
ExtensionRegistryLite(boolean empty) {
this.extensionsByNumber = Collections.emptyMap();
}
/** A (Object, int) pair, used as a map key. */
private static final class ObjectIntPair {
private final Object object;
private final int number;
ObjectIntPair(final Object object, final int number) {
this.object = object;
this.number = number;
}
@Override
public int hashCode() {
return System.identityHashCode(object) * ((1 << 16) - 1) + number;
}
@Override
public boolean equals(final Object obj) {
if (!(obj instanceof ObjectIntPair)) {
return false;
}
final ObjectIntPair other = (ObjectIntPair) obj;
return object == other.object && number == other.number;
}
}
}