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

@ -40,7 +40,7 @@ public final class FieldAttributes {
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
*/ */
@ -146,7 +153,7 @@ public final class FieldAttributes {
*/ */
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(

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));