• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2009 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.google.gson;
18 
19 import java.lang.annotation.Annotation;
20 import java.lang.reflect.Field;
21 import java.lang.reflect.Type;
22 import java.util.Arrays;
23 import java.util.Collection;
24 import java.util.Objects;
25 
26 /**
27  * A data object that stores attributes of a field.
28  *
29  * <p>This class is immutable; therefore, it can be safely shared across threads.
30  *
31  * @author Inderjeet Singh
32  * @author Joel Leitch
33  *
34  * @since 1.4
35  */
36 public final class FieldAttributes {
37   private final Field field;
38 
39   /**
40    * Constructs a Field Attributes object from the {@code f}.
41    *
42    * @param f the field to pull attributes from
43    */
FieldAttributes(Field f)44   public FieldAttributes(Field f) {
45     this.field = Objects.requireNonNull(f);
46   }
47 
48   /**
49    * @return the declaring class that contains this field
50    */
getDeclaringClass()51   public Class<?> getDeclaringClass() {
52     return field.getDeclaringClass();
53   }
54 
55   /**
56    * @return the name of the field
57    */
getName()58   public String getName() {
59     return field.getName();
60   }
61 
62   /**
63    * <p>For example, assume the following class definition:
64    * <pre class="code">
65    * public class Foo {
66    *   private String bar;
67    *   private List&lt;String&gt; red;
68    * }
69    *
70    * Type listParameterizedType = new TypeToken&lt;List&lt;String&gt;&gt;() {}.getType();
71    * </pre>
72    *
73    * <p>This method would return {@code String.class} for the {@code bar} field and
74    * {@code listParameterizedType} for the {@code red} field.
75    *
76    * @return the specific type declared for this field
77    */
getDeclaredType()78   public Type getDeclaredType() {
79     return field.getGenericType();
80   }
81 
82   /**
83    * Returns the {@code Class} object that was declared for this field.
84    *
85    * <p>For example, assume the following class definition:
86    * <pre class="code">
87    * public class Foo {
88    *   private String bar;
89    *   private List&lt;String&gt; red;
90    * }
91    * </pre>
92    *
93    * <p>This method would return {@code String.class} for the {@code bar} field and
94    * {@code List.class} for the {@code red} field.
95    *
96    * @return the specific class object that was declared for the field
97    */
getDeclaredClass()98   public Class<?> getDeclaredClass() {
99     return field.getType();
100   }
101 
102   /**
103    * Return the {@code T} annotation object from this field if it exist; otherwise returns
104    * {@code null}.
105    *
106    * @param annotation the class of the annotation that will be retrieved
107    * @return the annotation instance if it is bound to the field; otherwise {@code null}
108    */
getAnnotation(Class<T> annotation)109   public <T extends Annotation> T getAnnotation(Class<T> annotation) {
110     return field.getAnnotation(annotation);
111   }
112 
113   /**
114    * Return the annotations that are present on this field.
115    *
116    * @return an array of all the annotations set on the field
117    * @since 1.4
118    */
getAnnotations()119   public Collection<Annotation> getAnnotations() {
120     return Arrays.asList(field.getAnnotations());
121   }
122 
123   /**
124    * Returns {@code true} if the field is defined with the {@code modifier}.
125    *
126    * <p>This method is meant to be called as:
127    * <pre class="code">
128    * boolean hasPublicModifier = fieldAttribute.hasModifier(java.lang.reflect.Modifier.PUBLIC);
129    * </pre>
130    *
131    * @see java.lang.reflect.Modifier
132    */
hasModifier(int modifier)133   public boolean hasModifier(int modifier) {
134     return (field.getModifiers() & modifier) != 0;
135   }
136 
137   @Override
toString()138   public String toString() {
139     return field.toString();
140   }
141 }
142