diff options
author | Tom Powell <zifnab@zifnab06.net> | 2015-10-08 21:40:00 -0700 |
---|---|---|
committer | Tom Powell <zifnab@zifnab06.net> | 2015-10-08 21:40:00 -0700 |
commit | 4bc2ec0628f74bc24c3820951f4ca2c91e516f7a (patch) | |
tree | 3b0061fa2a6be32d1fdab9a63495aacf2bdc705c /gson/src/main/java/com/google/gson/FieldAttributes.java | |
parent | ca8ca14e5e1904db81f4f68445b94040293d83e8 (diff) | |
parent | 5d414d68ea79cc01bdd574656099a08228b82aa2 (diff) | |
download | android_external_gson-4bc2ec0628f74bc24c3820951f4ca2c91e516f7a.tar.gz android_external_gson-4bc2ec0628f74bc24c3820951f4ca2c91e516f7a.tar.bz2 android_external_gson-4bc2ec0628f74bc24c3820951f4ca2c91e516f7a.zip |
Merge tag 'gson-2.4' into cm-13.0HEADreplicant-6.0-0004-transitionreplicant-6.0-0004-rc6replicant-6.0-0004-rc5-transitionreplicant-6.0-0004-rc5replicant-6.0-0004-rc4replicant-6.0-0004-rc3replicant-6.0-0004-rc2replicant-6.0-0004-rc1replicant-6.0-0004replicant-6.0-0003replicant-6.0-0002replicant-6.0-0001staging/lineage-15.1stable/cm-13.0-ZNH5Ystable/cm-13.0-ZNH2KBstable/cm-13.0-ZNH2Kstable/cm-13.0-ZNH0Ecm-14.1cm-14.0cm-13.0
[maven-release-plugin] copy for tag gson-2.4
Change-Id: Id89b6397c7e9152eaab6f4753826708330d7373c
Diffstat (limited to 'gson/src/main/java/com/google/gson/FieldAttributes.java')
-rw-r--r-- | gson/src/main/java/com/google/gson/FieldAttributes.java | 157 |
1 files changed, 157 insertions, 0 deletions
diff --git a/gson/src/main/java/com/google/gson/FieldAttributes.java b/gson/src/main/java/com/google/gson/FieldAttributes.java new file mode 100644 index 00000000..cb89ff11 --- /dev/null +++ b/gson/src/main/java/com/google/gson/FieldAttributes.java @@ -0,0 +1,157 @@ +/* + * Copyright (C) 2009 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.google.gson; + +import com.google.gson.internal.$Gson$Preconditions; +import java.lang.annotation.Annotation; +import java.lang.reflect.Field; +import java.lang.reflect.Type; +import java.util.Arrays; +import java.util.Collection; + +/** + * A data object that stores attributes of a field. + * + * <p>This class is immutable; therefore, it can be safely shared across threads. + * + * @author Inderjeet Singh + * @author Joel Leitch + * + * @since 1.4 + */ +public final class FieldAttributes { + private final Field field; + + /** + * Constructs a Field Attributes object from the {@code f}. + * + * @param f the field to pull attributes from + */ + public FieldAttributes(Field f) { + $Gson$Preconditions.checkNotNull(f); + this.field = f; + } + + /** + * @return the declaring class that contains this field + */ + public Class<?> getDeclaringClass() { + return field.getDeclaringClass(); + } + + /** + * @return the name of the field + */ + public String getName() { + return field.getName(); + } + + /** + * <p>For example, assume the following class definition: + * <pre class="code"> + * public class Foo { + * private String bar; + * private List<String> red; + * } + * + * Type listParmeterizedType = new TypeToken<List<String>>() {}.getType(); + * </pre> + * + * <p>This method would return {@code String.class} for the {@code bar} field and + * {@code listParameterizedType} for the {@code red} field. + * + * @return the specific type declared for this field + */ + public Type getDeclaredType() { + return field.getGenericType(); + } + + /** + * Returns the {@code Class} object that was declared for this field. + * + * <p>For example, assume the following class definition: + * <pre class="code"> + * public class Foo { + * private String bar; + * private List<String> red; + * } + * </pre> + * + * <p>This method would return {@code String.class} for the {@code bar} field and + * {@code List.class} for the {@code red} field. + * + * @return the specific class object that was declared for the field + */ + public Class<?> getDeclaredClass() { + return field.getType(); + } + + /** + * Return the {@code T} annotation object from this field if it exist; otherwise returns + * {@code null}. + * + * @param annotation the class of the annotation that will be retrieved + * @return the annotation instance if it is bound to the field; otherwise {@code null} + */ + public <T extends Annotation> T getAnnotation(Class<T> annotation) { + return field.getAnnotation(annotation); + } + + /** + * Return the annotations that are present on this field. + * + * @return an array of all the annotations set on the field + * @since 1.4 + */ + public Collection<Annotation> getAnnotations() { + return Arrays.asList(field.getAnnotations()); + } + + /** + * Returns {@code true} if the field is defined with the {@code modifier}. + * + * <p>This method is meant to be called as: + * <pre class="code"> + * boolean hasPublicModifier = fieldAttribute.hasModifier(java.lang.reflect.Modifier.PUBLIC); + * </pre> + * + * @see java.lang.reflect.Modifier + */ + public boolean hasModifier(int modifier) { + return (field.getModifiers() & modifier) != 0; + } + + /** + * This is exposed internally only for the removing synthetic fields from the JSON output. + * + * @return true if the field is synthetic; otherwise false + * @throws IllegalAccessException + * @throws IllegalArgumentException + */ + Object get(Object instance) throws IllegalAccessException { + return field.get(instance); + } + + /** + * This is exposed internally only for the removing synthetic fields from the JSON output. + * + * @return true if the field is synthetic; otherwise false + */ + boolean isSynthetic() { + return field.isSynthetic(); + } +} |