Package com.google.gson
Class FieldAttributes
- java.lang.Object
-
- com.google.gson.FieldAttributes
-
public final class FieldAttributes extends java.lang.Object
A data object that stores attributes of a field.This class is immutable; therefore, it can be safely shared across threads.
- Since:
- 1.4
-
-
Field Summary
Fields Modifier and Type Field Description private java.lang.reflect.Field
field
-
Constructor Summary
Constructors Constructor Description FieldAttributes(java.lang.reflect.Field f)
Constructs a Field Attributes object from thef
.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description (package private) java.lang.Object
get(java.lang.Object instance)
Returns the value of the field represented by thisField
, on the specified object.<T extends java.lang.annotation.Annotation>
TgetAnnotation(java.lang.Class<T> annotation)
Return theT
annotation object from this field if it exist; otherwise returnsnull
.java.util.Collection<java.lang.annotation.Annotation>
getAnnotations()
Return the annotations that are present on this field.java.lang.Class<?>
getDeclaredClass()
Returns theClass
object that was declared for this field.java.lang.reflect.Type
getDeclaredType()
For example, assume the following class definition:java.lang.Class<?>
getDeclaringClass()
java.lang.String
getName()
boolean
hasModifier(int modifier)
Returnstrue
if the field is defined with themodifier
.(package private) boolean
isSynthetic()
This is exposed internally only for the removing synthetic fields from the JSON output.
-
-
-
Method Detail
-
getDeclaringClass
public java.lang.Class<?> getDeclaringClass()
- Returns:
- the declaring class that contains this field
-
getName
public java.lang.String getName()
- Returns:
- the name of the field
-
getDeclaredType
public java.lang.reflect.Type getDeclaredType()
For example, assume the following class definition:
public class Foo { private String bar; private List<String> red; } Type listParameterizedType = new TypeToken<List<String>>() {}.getType();
This method would return
String.class
for thebar
field andlistParameterizedType
for thered
field.- Returns:
- the specific type declared for this field
-
getDeclaredClass
public java.lang.Class<?> getDeclaredClass()
Returns theClass
object that was declared for this field.For example, assume the following class definition:
public class Foo { private String bar; private List<String> red; }
This method would return
String.class
for thebar
field andList.class
for thered
field.- Returns:
- the specific class object that was declared for the field
-
getAnnotation
public <T extends java.lang.annotation.Annotation> T getAnnotation(java.lang.Class<T> annotation)
Return theT
annotation object from this field if it exist; otherwise returnsnull
.- Parameters:
annotation
- the class of the annotation that will be retrieved- Returns:
- the annotation instance if it is bound to the field; otherwise
null
-
getAnnotations
public java.util.Collection<java.lang.annotation.Annotation> getAnnotations()
Return the annotations that are present on this field.- Returns:
- an array of all the annotations set on the field
- Since:
- 1.4
-
hasModifier
public boolean hasModifier(int modifier)
Returnstrue
if the field is defined with themodifier
.This method is meant to be called as:
boolean hasPublicModifier = fieldAttribute.hasModifier(java.lang.reflect.Modifier.PUBLIC);
- See Also:
Modifier
-
get
java.lang.Object get(java.lang.Object instance) throws java.lang.IllegalAccessException
Returns the value of the field represented by thisField
, on the specified object. The value is automatically wrapped in an object if it has a primitive type.- Returns:
- the value of the represented field in object
obj
; primitive values are wrapped in an appropriate object before being returned - Throws:
java.lang.IllegalAccessException
java.lang.IllegalArgumentException
-
isSynthetic
boolean isSynthetic()
This is exposed internally only for the removing synthetic fields from the JSON output.- Returns:
- true if the field is synthetic; otherwise false
-
-