]>
Commit | Line | Data |
---|---|---|
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 com.google.gson.internal.$Gson$Preconditions; | |
20 | import java.lang.annotation.Annotation; | |
21 | import java.lang.reflect.Field; | |
22 | import java.lang.reflect.Type; | |
23 | import java.util.Arrays; | |
24 | import java.util.Collection; | |
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 | */ | |
44 | public FieldAttributes(Field f) { | |
45 | $Gson$Preconditions.checkNotNull(f); | |
46 | this.field = f; | |
47 | } | |
48 | ||
49 | /** | |
50 | * @return the declaring class that contains this field | |
51 | */ | |
52 | public Class<?> getDeclaringClass() { | |
53 | return field.getDeclaringClass(); | |
54 | } | |
55 | ||
56 | /** | |
57 | * @return the name of the field | |
58 | */ | |
59 | public String getName() { | |
60 | return field.getName(); | |
61 | } | |
62 | ||
63 | /** | |
64 | * <p>For example, assume the following class definition: | |
65 | * <pre class="code"> | |
66 | * public class Foo { | |
67 | * private String bar; | |
68 | * private List<String> red; | |
69 | * } | |
70 | * | |
71 | * Type listParmeterizedType = new TypeToken<List<String>>() {}.getType(); | |
72 | * </pre> | |
73 | * | |
74 | * <p>This method would return {@code String.class} for the {@code bar} field and | |
75 | * {@code listParameterizedType} for the {@code red} field. | |
76 | * | |
77 | * @return the specific type declared for this field | |
78 | */ | |
79 | public Type getDeclaredType() { | |
80 | return field.getGenericType(); | |
81 | } | |
82 | ||
83 | /** | |
84 | * Returns the {@code Class} object that was declared for this field. | |
85 | * | |
86 | * <p>For example, assume the following class definition: | |
87 | * <pre class="code"> | |
88 | * public class Foo { | |
89 | * private String bar; | |
90 | * private List<String> red; | |
91 | * } | |
92 | * </pre> | |
93 | * | |
94 | * <p>This method would return {@code String.class} for the {@code bar} field and | |
95 | * {@code List.class} for the {@code red} field. | |
96 | * | |
97 | * @return the specific class object that was declared for the field | |
98 | */ | |
99 | public Class<?> getDeclaredClass() { | |
100 | return field.getType(); | |
101 | } | |
102 | ||
103 | /** | |
104 | * Return the {@code T} annotation object from this field if it exist; otherwise returns | |
105 | * {@code null}. | |
106 | * | |
107 | * @param annotation the class of the annotation that will be retrieved | |
108 | * @return the annotation instance if it is bound to the field; otherwise {@code null} | |
109 | */ | |
110 | public <T extends Annotation> T getAnnotation(Class<T> annotation) { | |
111 | return field.getAnnotation(annotation); | |
112 | } | |
113 | ||
114 | /** | |
115 | * Return the annotations that are present on this field. | |
116 | * | |
117 | * @return an array of all the annotations set on the field | |
118 | * @since 1.4 | |
119 | */ | |
120 | public Collection<Annotation> getAnnotations() { | |
121 | return Arrays.asList(field.getAnnotations()); | |
122 | } | |
123 | ||
124 | /** | |
125 | * Returns {@code true} if the field is defined with the {@code modifier}. | |
126 | * | |
127 | * <p>This method is meant to be called as: | |
128 | * <pre class="code"> | |
129 | * boolean hasPublicModifier = fieldAttribute.hasModifier(java.lang.reflect.Modifier.PUBLIC); | |
130 | * </pre> | |
131 | * | |
132 | * @see java.lang.reflect.Modifier | |
133 | */ | |
134 | public boolean hasModifier(int modifier) { | |
135 | return (field.getModifiers() & modifier) != 0; | |
136 | } | |
137 | ||
138 | /** | |
139 | * This is exposed internally only for the removing synthetic fields from the JSON output. | |
140 | * | |
141 | * @return true if the field is synthetic; otherwise false | |
142 | * @throws IllegalAccessException | |
143 | * @throws IllegalArgumentException | |
144 | */ | |
145 | Object get(Object instance) throws IllegalAccessException { | |
146 | return field.get(instance); | |
147 | } | |
148 | ||
149 | /** | |
150 | * This is exposed internally only for the removing synthetic fields from the JSON output. | |
151 | * | |
152 | * @return true if the field is synthetic; otherwise false | |
153 | */ | |
154 | boolean isSynthetic() { | |
155 | return field.isSynthetic(); | |
156 | } | |
157 | } |