forked from googleapis/google-http-java-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClassInfo.java
More file actions
241 lines (220 loc) · 7.52 KB
/
ClassInfo.java
File metadata and controls
241 lines (220 loc) · 7.52 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
230
231
232
233
234
235
236
237
238
239
240
241
/*
* Copyright (c) 2010 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/
package com.google.api.client.util;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.IdentityHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.TreeSet;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
/**
* Computes class information to determine data key name/value pairs associated with the class.
*
* <p>Implementation is thread-safe.
*
* @since 1.0
* @author Yaniv Inbar
*/
public final class ClassInfo {
/** Class information cache, with case-sensitive field names. */
private static final ConcurrentMap<Class<?>, ClassInfo> CACHE =
new ConcurrentHashMap<Class<?>, ClassInfo>();
/** Class information cache, with case-insensitive fields names. */
private static final ConcurrentMap<Class<?>, ClassInfo> CACHE_IGNORE_CASE =
new ConcurrentHashMap<Class<?>, ClassInfo>();
/** Class. */
private final Class<?> clazz;
/** Whether field names are case sensitive. */
private final boolean ignoreCase;
/** Map from {@link FieldInfo#getName()} to the field information. */
private final IdentityHashMap<String, FieldInfo> nameToFieldInfoMap =
new IdentityHashMap<String, FieldInfo>();
/**
* Unmodifiable sorted (with any possible {@code null} member first) list (without duplicates) of
* {@link FieldInfo#getName()}.
*/
final List<String> names;
/**
* Returns the class information for the given underlying class.
*
* @param underlyingClass underlying class or {@code null} for {@code null} result
* @return class information or {@code null} for {@code null} input
*/
public static ClassInfo of(Class<?> underlyingClass) {
return of(underlyingClass, false);
}
/**
* Returns the class information for the given underlying class.
*
* @param underlyingClass underlying class or {@code null} for {@code null} result
* @param ignoreCase whether field names are case sensitive
* @return class information or {@code null} for {@code null} input
* @since 1.10
*/
public static ClassInfo of(Class<?> underlyingClass, boolean ignoreCase) {
if (underlyingClass == null) {
return null;
}
final ConcurrentMap<Class<?>, ClassInfo> cache = ignoreCase ? CACHE_IGNORE_CASE : CACHE;
// Logic copied from ConcurrentMap.computeIfAbsent
ClassInfo v, newValue;
return ((v = cache.get(underlyingClass)) == null
&& (newValue = new ClassInfo(underlyingClass, ignoreCase)) != null
&& (v = cache.putIfAbsent(underlyingClass, newValue)) == null)
? newValue
: v;
}
/**
* Returns the underlying class.
*
* @since 1.4
*/
public Class<?> getUnderlyingClass() {
return clazz;
}
/**
* Returns whether field names are case sensitive.
*
* @since 1.10
*/
public final boolean getIgnoreCase() {
return ignoreCase;
}
/**
* Returns whether information for the given {@link FieldInfo#getName()} is available.
*
* @param name {@link FieldInfo#getName()} or {@code null}
* @return true if field info is available.
*/
public boolean hasFieldInfo(String name) {
if (name != null) {
if (ignoreCase) {
name = name.toLowerCase(Locale.US);
}
name = name.intern();
}
return nameToFieldInfoMap.containsKey(name);
}
/**
* Returns the information for the given {@link FieldInfo#getName()}.
*
* @param name {@link FieldInfo#getName()} or {@code null}
* @return field information or {@code null} for none
*/
public FieldInfo getFieldInfo(String name) {
if (name != null) {
if (ignoreCase) {
name = name.toLowerCase(Locale.US);
}
name = name.intern();
}
return nameToFieldInfoMap.get(name);
}
/**
* Returns the field for the given {@link FieldInfo#getName()}.
*
* @param name {@link FieldInfo#getName()} or {@code null}
* @return field or {@code null} for none
*/
public Field getField(String name) {
FieldInfo fieldInfo = getFieldInfo(name);
return fieldInfo == null ? null : fieldInfo.getField();
}
/**
* Returns the underlying class is an enum.
*
* @since 1.4
*/
public boolean isEnum() {
return clazz.isEnum();
}
/**
* Returns an unmodifiable sorted set (with any possible {@code null} member first) of {@link
* FieldInfo#getName() names}.
*/
public Collection<String> getNames() {
return names;
}
private ClassInfo(Class<?> srcClass, boolean ignoreCase) {
clazz = srcClass;
this.ignoreCase = ignoreCase;
Preconditions.checkArgument(
!ignoreCase || !srcClass.isEnum(), "cannot ignore case on an enum: " + srcClass);
// name set has a special comparator to keep null first
TreeSet<String> nameSet =
new TreeSet<String>(
new Comparator<String>() {
public int compare(String s0, String s1) {
return Objects.equal(s0, s1)
? 0
: s0 == null ? -1 : s1 == null ? 1 : s0.compareTo(s1);
}
});
// iterate over declared fields
for (Field field : srcClass.getDeclaredFields()) {
FieldInfo fieldInfo = FieldInfo.of(field);
if (fieldInfo == null) {
continue;
}
String fieldName = fieldInfo.getName();
if (ignoreCase) {
fieldName = fieldName.toLowerCase(Locale.US).intern();
}
FieldInfo conflictingFieldInfo = nameToFieldInfoMap.get(fieldName);
Preconditions.checkArgument(
conflictingFieldInfo == null,
"two fields have the same %sname <%s>: %s and %s",
ignoreCase ? "case-insensitive " : "",
fieldName,
field,
conflictingFieldInfo == null ? null : conflictingFieldInfo.getField());
nameToFieldInfoMap.put(fieldName, fieldInfo);
nameSet.add(fieldName);
}
// inherit from super class and add only fields not already in nameToFieldInfoMap
Class<?> superClass = srcClass.getSuperclass();
if (superClass != null) {
ClassInfo superClassInfo = ClassInfo.of(superClass, ignoreCase);
nameSet.addAll(superClassInfo.names);
for (Map.Entry<String, FieldInfo> e : superClassInfo.nameToFieldInfoMap.entrySet()) {
String name = e.getKey();
if (!nameToFieldInfoMap.containsKey(name)) {
nameToFieldInfoMap.put(name, e.getValue());
}
}
}
names =
nameSet.isEmpty()
? Collections.<String>emptyList()
: Collections.unmodifiableList(new ArrayList<String>(nameSet));
}
/**
* Returns an unmodifiable collection of the {@code FieldInfo}s for this class, without any
* guarantee of order.
*
* <p>If you need sorted order, instead use {@link #getNames()} with {@link
* #getFieldInfo(String)}.
*
* @since 1.16
*/
public Collection<FieldInfo> getFieldInfos() {
return Collections.unmodifiableCollection(nameToFieldInfoMap.values());
}
}