]>
iEval git - unical.git/blob - FieldAttributes.java
cb89ff110306bfe6b6e1f12a2884cc2f38656ab1
2 * Copyright (C) 2009 Google Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com
.google
.gson
;
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
;
27 * A data object that stores attributes of a field.
29 * <p>This class is immutable; therefore, it can be safely shared across threads.
31 * @author Inderjeet Singh
36 public final class FieldAttributes
{
37 private final Field field
;
40 * Constructs a Field Attributes object from the {@code f}.
42 * @param f the field to pull attributes from
44 public FieldAttributes(Field f
) {
45 $Gson$Preconditions
.checkNotNull(f
);
50 * @return the declaring class that contains this field
52 public Class
<?
> getDeclaringClass() {
53 return field
.getDeclaringClass();
57 * @return the name of the field
59 public String
getName() {
60 return field
.getName();
64 * <p>For example, assume the following class definition:
68 * private List<String> red;
71 * Type listParmeterizedType = new TypeToken<List<String>>() {}.getType();
74 * <p>This method would return {@code String.class} for the {@code bar} field and
75 * {@code listParameterizedType} for the {@code red} field.
77 * @return the specific type declared for this field
79 public Type
getDeclaredType() {
80 return field
.getGenericType();
84 * Returns the {@code Class} object that was declared for this field.
86 * <p>For example, assume the following class definition:
90 * private List<String> red;
94 * <p>This method would return {@code String.class} for the {@code bar} field and
95 * {@code List.class} for the {@code red} field.
97 * @return the specific class object that was declared for the field
99 public Class
<?
> getDeclaredClass() {
100 return field
.getType();
104 * Return the {@code T} annotation object from this field if it exist; otherwise returns
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}
110 public <T
extends Annotation
> T
getAnnotation(Class
<T
> annotation
) {
111 return field
.getAnnotation(annotation
);
115 * Return the annotations that are present on this field.
117 * @return an array of all the annotations set on the field
120 public Collection
<Annotation
> getAnnotations() {
121 return Arrays
.asList(field
.getAnnotations());
125 * Returns {@code true} if the field is defined with the {@code modifier}.
127 * <p>This method is meant to be called as:
129 * boolean hasPublicModifier = fieldAttribute.hasModifier(java.lang.reflect.Modifier.PUBLIC);
132 * @see java.lang.reflect.Modifier
134 public boolean hasModifier(int modifier
) {
135 return (field
.getModifiers() & modifier
) != 0;
139 * This is exposed internally only for the removing synthetic fields from the JSON output.
141 * @return true if the field is synthetic; otherwise false
142 * @throws IllegalAccessException
143 * @throws IllegalArgumentException
145 Object
get(Object instance
) throws IllegalAccessException
{
146 return field
.get(instance
);
150 * This is exposed internally only for the removing synthetic fields from the JSON output.
152 * @return true if the field is synthetic; otherwise false
154 boolean isSynthetic() {
155 return field
.isSynthetic();
This page took 0.05106 seconds and 3 git commands to generate.