/
FieldInfo.java
289 lines (265 loc) · 8.62 KB
/
FieldInfo.java
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/*
* 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 com.google.common.base.Ascii;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.WeakHashMap;
/**
* Parses field information to determine data key name/value pair associated with the field.
*
* <p>Implementation is thread-safe.
*
* @since 1.0
* @author Yaniv Inbar
*/
public class FieldInfo {
/** Cached field information. */
private static final Map<Field, FieldInfo> CACHE = new WeakHashMap<Field, FieldInfo>();
/**
* Returns the field information for the given enum value.
*
* @param enumValue enum value
* @return field information
* @throws IllegalArgumentException if the enum value has no value annotation
* @since 1.4
*/
public static FieldInfo of(Enum<?> enumValue) {
try {
FieldInfo result = FieldInfo.of(enumValue.getClass().getField(enumValue.name()));
Preconditions.checkArgument(
result != null, "enum constant missing @Value or @NullValue annotation: %s", enumValue);
return result;
} catch (NoSuchFieldException e) {
// not possible
throw new RuntimeException(e);
}
}
/**
* Returns the field information for the given field.
*
* @param field field or {@code null} for {@code null} result
* @return field information or {@code null} if the field has no {@link #name} or for {@code null}
* input
*/
public static FieldInfo of(Field field) {
if (field == null) {
return null;
}
synchronized (CACHE) {
FieldInfo fieldInfo = CACHE.get(field);
boolean isEnumContant = field.isEnumConstant();
if (fieldInfo == null && (isEnumContant || !Modifier.isStatic(field.getModifiers()))) {
String fieldName;
if (isEnumContant) {
// check for @Value annotation
Value value = field.getAnnotation(Value.class);
if (value != null) {
fieldName = value.value();
} else {
// check for @NullValue annotation
NullValue nullValue = field.getAnnotation(NullValue.class);
if (nullValue != null) {
fieldName = null;
} else {
// else ignore
return null;
}
}
} else {
// check for @Key annotation
Key key = field.getAnnotation(Key.class);
if (key == null) {
// else ignore
return null;
}
fieldName = key.value();
field.setAccessible(true);
}
if ("##default".equals(fieldName)) {
fieldName = field.getName();
}
fieldInfo = new FieldInfo(field, fieldName);
CACHE.put(field, fieldInfo);
}
return fieldInfo;
}
}
/** Whether the field class is "primitive" as defined by {@link Data#isPrimitive(Type)}. */
private final boolean isPrimitive;
/** Field. */
private final Field field;
private final Method[] setters;
/**
* Data key name associated with the field for a non-enum-constant with a {@link Key} annotation,
* or data key value associated with the enum constant with a {@link Value} annotation or {@code
* null} for an enum constant with a {@link NullValue} annotation.
*
* <p>This string is interned.
*/
private final String name;
FieldInfo(Field field, String name) {
this.field = field;
this.name = name == null ? null : name.intern();
isPrimitive = Data.isPrimitive(getType());
this.setters = settersMethodForField(field);
}
/** Creates list of setter methods for a field only in declaring class. */
private Method[] settersMethodForField(final Field field) {
List<Method> methods = new ArrayList<>();
String fieldSetter = "set" + Ascii.toUpperCase(field.getName().substring(0, 1));
if (field.getName().length() > 1) {
fieldSetter += field.getName().substring(1);
}
for (Method method : field.getDeclaringClass().getDeclaredMethods()) {
if (method.getParameterTypes().length == 1) {
// add case-sensitive matches first in the list
if (method.getName().equals(fieldSetter)) {
methods.add(0, method);
} else if (Ascii.toLowerCase(method.getName()).equals(Ascii.toLowerCase(fieldSetter))) {
methods.add(method);
}
}
}
return methods.toArray(new Method[0]);
}
/**
* Returns the field.
*
* @since 1.4
*/
public Field getField() {
return field;
}
/**
* Returns the data key name associated with the field for a non-enum-constant with a {@link Key}
* annotation, or data key value associated with the enum constant with a {@link Value} annotation
* or {@code null} for an enum constant with a {@link NullValue} annotation.
*
* <p>This string is interned.
*
* @since 1.4
*/
public String getName() {
return name;
}
/**
* Returns the field's type.
*
* @since 1.4
*/
public Class<?> getType() {
return field.getType();
}
/**
* Returns the field's generic type, which is a class, parameterized type, generic array type, or
* type variable, but not a wildcard type.
*
* @since 1.4
*/
public Type getGenericType() {
return field.getGenericType();
}
/**
* Returns whether the field is final.
*
* @since 1.4
*/
public boolean isFinal() {
return Modifier.isFinal(field.getModifiers());
}
/**
* Returns whether the field is primitive as defined by {@link Data#isPrimitive(Type)}.
*
* @since 1.4
*/
public boolean isPrimitive() {
return isPrimitive;
}
/** Returns the value of the field in the given object instance using reflection. */
public Object getValue(Object obj) {
return getFieldValue(field, obj);
}
/**
* Sets this field in the given object to the given value using reflection.
*
* <p>If the field is final, it checks that the value being set is identical to the existing
* value.
*/
public void setValue(Object obj, Object value) {
for (Method method : setters) {
if (value == null || method.getParameterTypes()[0].isAssignableFrom(value.getClass())) {
try {
method.invoke(obj, value);
return;
} catch (IllegalAccessException | InvocationTargetException e) {
// try to set field directly
}
}
}
setFieldValue(field, obj, value);
}
/** Returns the class information of the field's declaring class. */
public ClassInfo getClassInfo() {
return ClassInfo.of(field.getDeclaringClass());
}
@SuppressWarnings("unchecked")
public <T extends Enum<T>> T enumValue() {
return Enum.valueOf((Class<T>) field.getDeclaringClass(), field.getName());
}
/** Returns the value of the given field in the given object using reflection. */
public static Object getFieldValue(Field field, Object obj) {
try {
return field.get(obj);
} catch (IllegalAccessException e) {
throw new IllegalArgumentException(e);
}
}
/**
* Sets the given field in the given object to the given value using reflection.
*
* <p>If the field is final, it checks that the value being set is identical to the existing
* value.
*/
public static void setFieldValue(Field field, Object obj, Object value) {
if (Modifier.isFinal(field.getModifiers())) {
Object finalValue = getFieldValue(field, obj);
if (value == null ? finalValue != null : !value.equals(finalValue)) {
throw new IllegalArgumentException(
"expected final value <"
+ finalValue
+ "> but was <"
+ value
+ "> on "
+ field.getName()
+ " field in "
+ obj.getClass().getName());
}
} else {
try {
field.set(obj, value);
} catch (SecurityException e) {
throw new IllegalArgumentException(e);
} catch (IllegalAccessException e) {
throw new IllegalArgumentException(e);
}
}
}
}