From d0977c2e3af152901b7b921a31ab6597b0884a4a Mon Sep 17 00:00:00 2001 From: Joel Leitch Date: Mon, 1 Nov 2010 22:31:48 +0000 Subject: [PATCH] Exposing the declaring class for the field wrapped by FieldAttributes. --- .../java/com/google/gson/FieldAttributes.java | 35 +++++++++++-------- .../com/google/gson/FieldAttributesTest.java | 12 ++++--- 2 files changed, 29 insertions(+), 18 deletions(-) diff --git a/gson/src/main/java/com/google/gson/FieldAttributes.java b/gson/src/main/java/com/google/gson/FieldAttributes.java index c278288f..ffa34eb0 100644 --- a/gson/src/main/java/com/google/gson/FieldAttributes.java +++ b/gson/src/main/java/com/google/gson/FieldAttributes.java @@ -36,11 +36,11 @@ import java.util.Collections; public final class FieldAttributes { private static final String MAX_CACHE_PROPERTY_NAME = "com.google.gson.annotation_cache_size_hint"; - + private static final Cache, String>, Collection> ANNOTATION_CACHE = new LruCache,String>, Collection>(getMaxCacheSize()); - private final Class parentClazz; + private final Class declaringClazz; private final Field field; private final Class declaredType; private final boolean isSynthetic; @@ -56,9 +56,9 @@ public final class FieldAttributes { * * @param f the field to pull attributes from */ - FieldAttributes(final Class parentClazz, final Field f) { - Preconditions.checkNotNull(parentClazz); - this.parentClazz = parentClazz; + FieldAttributes(final Class declaringClazz, final Field f) { + Preconditions.checkNotNull(declaringClazz); + this.declaringClazz = declaringClazz; name = f.getName(); declaredType = f.getType(); 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 */ @@ -142,11 +149,11 @@ public final class FieldAttributes { * Return the annotations that are present on this field. * * @return an array of all the annotations set on the field - * @since 1.4 + * @since 1.4 */ public Collection getAnnotations() { if (annotations == null) { - Pair, String> key = new Pair, String>(parentClazz, name); + Pair, String> key = new Pair, String>(declaringClazz, name); annotations = ANNOTATION_CACHE.getElement(key); if (annotations == null) { 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. * * @return true if the field is synthetic; otherwise false - * @throws IllegalAccessException - * @throws IllegalArgumentException + * @throws IllegalAccessException + * @throws IllegalArgumentException */ void set(Object instance, Object value) throws IllegalAccessException { field.set(instance, value); } - + /** * This is exposed internally only for the removing synthetic fields from the JSON output. * * @return true if the field is synthetic; otherwise false - * @throws IllegalAccessException - * @throws IllegalArgumentException + * @throws IllegalAccessException + * @throws IllegalArgumentException */ Object get(Object instance) throws IllegalAccessException { return field.get(instance); } - + /** * This is exposed internally only for the removing synthetic fields from the JSON output. * @@ -201,7 +208,7 @@ public final class FieldAttributes { boolean isSynthetic() { return isSynthetic; } - + /** * @deprecated remove this when {@link FieldNamingStrategy} is deleted. */ diff --git a/gson/src/test/java/com/google/gson/FieldAttributesTest.java b/gson/src/test/java/com/google/gson/FieldAttributesTest.java index 77cabef8..af03f314 100644 --- a/gson/src/test/java/com/google/gson/FieldAttributesTest.java +++ b/gson/src/test/java/com/google/gson/FieldAttributesTest.java @@ -16,13 +16,13 @@ package com.google.gson; -import java.lang.reflect.Modifier; -import java.lang.reflect.Type; -import java.util.List; +import com.google.gson.reflect.TypeToken; 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. @@ -46,6 +46,10 @@ public class FieldAttributesTest extends TestCase { } catch (NullPointerException expected) { } } + public void testDeclaringClass() throws Exception { + assertEquals(Foo.class, fieldAttributes.getDeclaringClass()); + } + public void testModifiers() throws Exception { assertFalse(fieldAttributes.hasModifier(Modifier.STATIC)); assertFalse(fieldAttributes.hasModifier(Modifier.FINAL));