Exposing the declaring class for the field wrapped by FieldAttributes.

This commit is contained in:
Joel Leitch 2010-11-01 22:31:48 +00:00
parent 7127be7965
commit d0977c2e3a
2 changed files with 29 additions and 18 deletions

View File

@ -36,11 +36,11 @@ import java.util.Collections;
public final class FieldAttributes { public final class FieldAttributes {
private static final String MAX_CACHE_PROPERTY_NAME = private static final String MAX_CACHE_PROPERTY_NAME =
"com.google.gson.annotation_cache_size_hint"; "com.google.gson.annotation_cache_size_hint";
private static final Cache<Pair<Class<?>, String>, Collection<Annotation>> ANNOTATION_CACHE = private static final Cache<Pair<Class<?>, String>, Collection<Annotation>> ANNOTATION_CACHE =
new LruCache<Pair<Class<?>,String>, Collection<Annotation>>(getMaxCacheSize()); new LruCache<Pair<Class<?>,String>, Collection<Annotation>>(getMaxCacheSize());
private final Class<?> parentClazz; private final Class<?> declaringClazz;
private final Field field; private final Field field;
private final Class<?> declaredType; private final Class<?> declaredType;
private final boolean isSynthetic; private final boolean isSynthetic;
@ -56,9 +56,9 @@ public final class FieldAttributes {
* *
* @param f the field to pull attributes from * @param f the field to pull attributes from
*/ */
FieldAttributes(final Class<?> parentClazz, final Field f) { FieldAttributes(final Class<?> declaringClazz, final Field f) {
Preconditions.checkNotNull(parentClazz); Preconditions.checkNotNull(declaringClazz);
this.parentClazz = parentClazz; this.declaringClazz = declaringClazz;
name = f.getName(); name = f.getName();
declaredType = f.getType(); declaredType = f.getType();
isSynthetic = f.isSynthetic(); isSynthetic = f.isSynthetic();
@ -77,6 +77,13 @@ public final class FieldAttributes {
} }
} }
/**
* @return the declaring class that contains this field
*/
public Class<?> getDeclaringClass() {
return declaringClazz;
}
/** /**
* @return the name of the field * @return the name of the field
*/ */
@ -142,11 +149,11 @@ public final class FieldAttributes {
* Return the annotations that are present on this field. * Return the annotations that are present on this field.
* *
* @return an array of all the annotations set on the field * @return an array of all the annotations set on the field
* @since 1.4 * @since 1.4
*/ */
public Collection<Annotation> getAnnotations() { public Collection<Annotation> getAnnotations() {
if (annotations == null) { if (annotations == null) {
Pair<Class<?>, String> key = new Pair<Class<?>, String>(parentClazz, name); Pair<Class<?>, String> key = new Pair<Class<?>, String>(declaringClazz, name);
annotations = ANNOTATION_CACHE.getElement(key); annotations = ANNOTATION_CACHE.getElement(key);
if (annotations == null) { if (annotations == null) {
annotations = Collections.unmodifiableCollection( annotations = Collections.unmodifiableCollection(
@ -175,24 +182,24 @@ public final class FieldAttributes {
* This is exposed internally only for the removing synthetic fields from the JSON output. * This is exposed internally only for the removing synthetic fields from the JSON output.
* *
* @return true if the field is synthetic; otherwise false * @return true if the field is synthetic; otherwise false
* @throws IllegalAccessException * @throws IllegalAccessException
* @throws IllegalArgumentException * @throws IllegalArgumentException
*/ */
void set(Object instance, Object value) throws IllegalAccessException { void set(Object instance, Object value) throws IllegalAccessException {
field.set(instance, value); field.set(instance, value);
} }
/** /**
* This is exposed internally only for the removing synthetic fields from the JSON output. * This is exposed internally only for the removing synthetic fields from the JSON output.
* *
* @return true if the field is synthetic; otherwise false * @return true if the field is synthetic; otherwise false
* @throws IllegalAccessException * @throws IllegalAccessException
* @throws IllegalArgumentException * @throws IllegalArgumentException
*/ */
Object get(Object instance) throws IllegalAccessException { Object get(Object instance) throws IllegalAccessException {
return field.get(instance); return field.get(instance);
} }
/** /**
* This is exposed internally only for the removing synthetic fields from the JSON output. * This is exposed internally only for the removing synthetic fields from the JSON output.
* *
@ -201,7 +208,7 @@ public final class FieldAttributes {
boolean isSynthetic() { boolean isSynthetic() {
return isSynthetic; return isSynthetic;
} }
/** /**
* @deprecated remove this when {@link FieldNamingStrategy} is deleted. * @deprecated remove this when {@link FieldNamingStrategy} is deleted.
*/ */

View File

@ -16,13 +16,13 @@
package com.google.gson; package com.google.gson;
import java.lang.reflect.Modifier; import com.google.gson.reflect.TypeToken;
import java.lang.reflect.Type;
import java.util.List;
import junit.framework.TestCase; import junit.framework.TestCase;
import com.google.gson.reflect.TypeToken; import java.lang.reflect.Modifier;
import java.lang.reflect.Type;
import java.util.List;
/** /**
* Unit tests for the {@link FieldAttributes} class. * Unit tests for the {@link FieldAttributes} class.
@ -46,6 +46,10 @@ public class FieldAttributesTest extends TestCase {
} catch (NullPointerException expected) { } } catch (NullPointerException expected) { }
} }
public void testDeclaringClass() throws Exception {
assertEquals(Foo.class, fieldAttributes.getDeclaringClass());
}
public void testModifiers() throws Exception { public void testModifiers() throws Exception {
assertFalse(fieldAttributes.hasModifier(Modifier.STATIC)); assertFalse(fieldAttributes.hasModifier(Modifier.STATIC));
assertFalse(fieldAttributes.hasModifier(Modifier.FINAL)); assertFalse(fieldAttributes.hasModifier(Modifier.FINAL));