2009-10-07 11:23:14 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2009 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.gson;
|
|
|
|
|
2011-09-11 09:04:56 +02:00
|
|
|
import com.google.gson.internal.Pair;
|
2009-10-07 11:23:14 +02:00
|
|
|
import java.lang.annotation.Annotation;
|
|
|
|
import java.lang.reflect.Field;
|
|
|
|
import java.lang.reflect.Type;
|
2010-01-09 23:43:27 +01:00
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.Collection;
|
|
|
|
import java.util.Collections;
|
2009-10-07 11:23:14 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A data object that stores attributes of a field.
|
|
|
|
*
|
|
|
|
* <p>This class is immutable; therefore, it can be safely shared across threads.
|
|
|
|
*
|
|
|
|
* @author Inderjeet Singh
|
|
|
|
* @author Joel Leitch
|
|
|
|
*
|
|
|
|
* @since 1.4
|
|
|
|
*/
|
2009-10-09 05:11:16 +02:00
|
|
|
public final class FieldAttributes {
|
2010-05-19 22:47:27 +02:00
|
|
|
private static final String MAX_CACHE_PROPERTY_NAME =
|
|
|
|
"com.google.gson.annotation_cache_size_hint";
|
2010-11-01 23:31:48 +01:00
|
|
|
|
2011-11-21 07:23:42 +01:00
|
|
|
private static final LruCache<Pair<Class<?>, String>, Collection<Annotation>> ANNOTATION_CACHE
|
|
|
|
= new LruCache<Pair<Class<?>,String>, Collection<Annotation>>(getMaxCacheSize());
|
2010-01-10 01:32:21 +01:00
|
|
|
|
2010-11-01 23:31:48 +01:00
|
|
|
private final Class<?> declaringClazz;
|
2009-10-07 11:23:14 +02:00
|
|
|
private final Field field;
|
2009-12-04 00:13:39 +01:00
|
|
|
private final Class<?> declaredType;
|
|
|
|
private final boolean isSynthetic;
|
|
|
|
private final int modifiers;
|
2010-01-09 23:43:27 +01:00
|
|
|
private final String name;
|
2009-10-07 11:23:14 +02:00
|
|
|
|
2009-12-03 23:17:39 +01:00
|
|
|
// Fields used for lazy initialization
|
|
|
|
private Type genericType;
|
2010-01-09 23:43:27 +01:00
|
|
|
private Collection<Annotation> annotations;
|
2009-12-03 23:17:39 +01:00
|
|
|
|
2009-10-07 11:23:14 +02:00
|
|
|
/**
|
2009-10-09 05:11:16 +02:00
|
|
|
* Constructs a Field Attributes object from the {@code f}.
|
|
|
|
*
|
|
|
|
* @param f the field to pull attributes from
|
2009-10-07 11:23:14 +02:00
|
|
|
*/
|
2011-11-21 06:42:30 +01:00
|
|
|
public FieldAttributes(Field f) {
|
|
|
|
this.declaringClazz = f.getDeclaringClass();
|
2011-03-16 08:23:44 +01:00
|
|
|
this.name = f.getName();
|
|
|
|
this.declaredType = f.getType();
|
|
|
|
this.isSynthetic = f.isSynthetic();
|
|
|
|
this.modifiers = f.getModifiers();
|
|
|
|
this.field = f;
|
2009-10-07 11:23:14 +02:00
|
|
|
}
|
|
|
|
|
2010-05-19 22:47:27 +02:00
|
|
|
private static int getMaxCacheSize() {
|
|
|
|
final int defaultMaxCacheSize = 2000;
|
|
|
|
try {
|
|
|
|
String propertyValue = System.getProperty(
|
|
|
|
MAX_CACHE_PROPERTY_NAME, String.valueOf(defaultMaxCacheSize));
|
|
|
|
return Integer.parseInt(propertyValue);
|
|
|
|
} catch (NumberFormatException e) {
|
|
|
|
return defaultMaxCacheSize;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-01 23:31:48 +01:00
|
|
|
/**
|
|
|
|
* @return the declaring class that contains this field
|
|
|
|
*/
|
|
|
|
public Class<?> getDeclaringClass() {
|
|
|
|
return declaringClazz;
|
|
|
|
}
|
|
|
|
|
2009-10-07 11:23:14 +02:00
|
|
|
/**
|
|
|
|
* @return the name of the field
|
|
|
|
*/
|
|
|
|
public String getName() {
|
2009-12-03 23:17:39 +01:00
|
|
|
return name;
|
2009-10-07 11:23:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* <p>For example, assume the following class definition:
|
|
|
|
* <pre class="code">
|
|
|
|
* public class Foo {
|
|
|
|
* private String bar;
|
|
|
|
* private List<String> red;
|
|
|
|
* }
|
|
|
|
*
|
2011-06-03 21:01:08 +02:00
|
|
|
* Type listParmeterizedType = new TypeToken<List<String>>() {}.getType();
|
2009-10-07 11:23:14 +02:00
|
|
|
* </pre>
|
|
|
|
*
|
|
|
|
* <p>This method would return {@code String.class} for the {@code bar} field and
|
|
|
|
* {@code listParameterizedType} for the {@code red} field.
|
|
|
|
*
|
|
|
|
* @return the specific type declared for this field
|
|
|
|
*/
|
|
|
|
public Type getDeclaredType() {
|
2009-12-03 23:17:39 +01:00
|
|
|
if (genericType == null) {
|
|
|
|
genericType = field.getGenericType();
|
|
|
|
}
|
|
|
|
return genericType;
|
2009-10-07 11:23:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-10-01 04:04:48 +02:00
|
|
|
* Returns the {@code Class} object that was declared for this field.
|
2009-10-07 11:23:14 +02:00
|
|
|
*
|
|
|
|
* <p>For example, assume the following class definition:
|
|
|
|
* <pre class="code">
|
|
|
|
* public class Foo {
|
|
|
|
* private String bar;
|
|
|
|
* private List<String> red;
|
|
|
|
* }
|
|
|
|
* </pre>
|
|
|
|
*
|
|
|
|
* <p>This method would return {@code String.class} for the {@code bar} field and
|
|
|
|
* {@code List.class} for the {@code red} field.
|
|
|
|
*
|
|
|
|
* @return the specific class object that was declared for the field
|
|
|
|
*/
|
|
|
|
public Class<?> getDeclaredClass() {
|
2009-12-03 23:17:39 +01:00
|
|
|
return declaredType;
|
2009-10-07 11:23:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-10-09 05:03:04 +02:00
|
|
|
* Return the {@code T} annotation object from this field if it exist; otherwise returns
|
2009-10-07 11:23:14 +02:00
|
|
|
* {@code null}.
|
|
|
|
*
|
|
|
|
* @param annotation the class of the annotation that will be retrieved
|
|
|
|
* @return the annotation instance if it is bound to the field; otherwise {@code null}
|
|
|
|
*/
|
|
|
|
public <T extends Annotation> T getAnnotation(Class<T> annotation) {
|
2010-01-09 23:43:27 +01:00
|
|
|
return getAnnotationFromArray(getAnnotations(), annotation);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the annotations that are present on this field.
|
|
|
|
*
|
|
|
|
* @return an array of all the annotations set on the field
|
2010-11-01 23:31:48 +01:00
|
|
|
* @since 1.4
|
2010-01-09 23:43:27 +01:00
|
|
|
*/
|
|
|
|
public Collection<Annotation> getAnnotations() {
|
2009-12-03 23:28:58 +01:00
|
|
|
if (annotations == null) {
|
2011-03-30 15:59:06 +02:00
|
|
|
Pair<Class<?>, String> key = new Pair<Class<?>, String>(declaringClazz, name);
|
2011-11-21 07:23:42 +01:00
|
|
|
Collection<Annotation> cachedValue = ANNOTATION_CACHE.get(key);
|
2011-06-07 03:28:30 +02:00
|
|
|
if (cachedValue == null) {
|
|
|
|
cachedValue = Collections.unmodifiableCollection(
|
2010-01-10 01:32:21 +01:00
|
|
|
Arrays.asList(field.getAnnotations()));
|
2011-11-21 07:23:42 +01:00
|
|
|
ANNOTATION_CACHE.put(key, cachedValue);
|
2010-01-10 01:32:21 +01:00
|
|
|
}
|
2011-06-07 03:28:30 +02:00
|
|
|
annotations = cachedValue;
|
2009-12-03 23:28:58 +01:00
|
|
|
}
|
2010-01-09 23:43:27 +01:00
|
|
|
return annotations;
|
2009-10-07 11:23:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns {@code true} if the field is defined with the {@code modifier}.
|
|
|
|
*
|
|
|
|
* <p>This method is meant to be called as:
|
|
|
|
* <pre class="code">
|
|
|
|
* boolean hasPublicModifier = fieldAttribute.hasModifier(java.lang.reflect.Modifier.PUBLIC);
|
|
|
|
* </pre>
|
|
|
|
*
|
|
|
|
* @see java.lang.reflect.Modifier
|
|
|
|
*/
|
|
|
|
public boolean hasModifier(int modifier) {
|
2009-12-03 23:17:39 +01:00
|
|
|
return (modifiers & modifier) != 0;
|
2009-10-07 11:23:14 +02:00
|
|
|
}
|
|
|
|
|
2010-01-09 23:43:27 +01:00
|
|
|
/**
|
|
|
|
* This is exposed internally only for the removing synthetic fields from the JSON output.
|
|
|
|
*
|
|
|
|
* @return true if the field is synthetic; otherwise false
|
2010-11-01 23:31:48 +01:00
|
|
|
* @throws IllegalAccessException
|
|
|
|
* @throws IllegalArgumentException
|
2010-01-09 23:43:27 +01:00
|
|
|
*/
|
|
|
|
Object get(Object instance) throws IllegalAccessException {
|
|
|
|
return field.get(instance);
|
|
|
|
}
|
2010-11-01 23:31:48 +01:00
|
|
|
|
2009-10-07 11:23:14 +02:00
|
|
|
/**
|
2009-10-08 22:51:40 +02:00
|
|
|
* This is exposed internally only for the removing synthetic fields from the JSON output.
|
|
|
|
*
|
|
|
|
* @return true if the field is synthetic; otherwise false
|
2009-10-07 11:23:14 +02:00
|
|
|
*/
|
|
|
|
boolean isSynthetic() {
|
2009-12-03 23:17:39 +01:00
|
|
|
return isSynthetic;
|
2009-10-07 11:23:14 +02:00
|
|
|
}
|
2010-11-01 23:31:48 +01:00
|
|
|
|
2009-12-03 23:28:58 +01:00
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
private static <T extends Annotation> T getAnnotationFromArray(
|
2010-01-09 23:43:27 +01:00
|
|
|
Collection<Annotation> annotations, Class<T> annotation) {
|
2009-12-03 23:28:58 +01:00
|
|
|
for (Annotation a : annotations) {
|
|
|
|
if (a.annotationType() == annotation) {
|
|
|
|
return (T) a;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2009-10-07 11:23:14 +02:00
|
|
|
}
|