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 =
new LruCache<Pair<Class<?>,String>, Collection<Annotation>>(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
*/
@ -146,7 +153,7 @@ public final class FieldAttributes {
*/
public Collection<Annotation> getAnnotations() {
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);
if (annotations == null) {
annotations = Collections.unmodifiableCollection(

View File

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