1 package com.badlogic.gdx.utils.reflect;
3 import java.lang.reflect.Modifier;
4 import java.lang.reflect.ParameterizedType;
5 import java.lang.reflect.Type;
7 /** Provides information about, and access to, a single field of a class or interface.
8 * @author nexsoftware */
9 public final class Field {
11 private final com.badlogic.gwtref.client.Field field;
13 Field(com.badlogic.gwtref.client.Field field) {
17 /** Returns the name of the field. */
18 public String getName() {
19 return field.getName();
22 /** Returns a Class object that identifies the declared type for the field. */
23 public Class getType() {
24 return field.getType().getClassOfType();
27 /** Returns the Class object representing the class or interface that declares the field. */
28 public Class getDeclaringClass() {
29 return field.getEnclosingType().getClassOfType();
32 public boolean isAccessible() {
33 return field.isAccessible();
36 public void setAccessible(boolean accessible) {
37 field.setAccessible(accessible);
40 /** Return true if the field does not include any of the {@code private}, {@code protected}, or {@code public} modifiers. */
41 public boolean isDefaultAccess() {
42 return !isPrivate() && ! isProtected() && ! isPublic();
45 /** Return true if the field includes the {@code final} modifier. */
46 public boolean isFinal() {
47 return field.isFinal();
50 /** Return true if the field includes the {@code private} modifier. */
51 public boolean isPrivate() {
52 return field.isPrivate();
55 /** Return true if the field includes the {@code protected} modifier. */
56 public boolean isProtected() {
57 return field.isProtected();
60 /** Return true if the field includes the {@code public} modifier. */
61 public boolean isPublic() {
62 return field.isPublic();
65 /** Return true if the field includes the {@code static} modifier. */
66 public boolean isStatic() {
67 return field.isStatic();
70 /** Return true if the field includes the {@code transient} modifier. */
71 public boolean isTransient() {
72 return field.isTransient();
75 /** Return true if the field includes the {@code volatile} modifier. */
76 public boolean isVolatile() {
77 return field.isVolatile();
80 /** Return true if the field is a synthetic field. */
81 public boolean isSynthetic() {
82 return field.isSynthetic();
85 /** If the type of the field is parameterized, returns the Class object representing the parameter type, null otherwise. */
86 public Class getElementType() {
90 /** Returns the value of the field on the supplied object. */
91 public Object get(Object obj) throws ReflectionException {
93 return field.get(obj);
94 } catch (IllegalArgumentException e) {
95 throw new ReflectionException("Object is not an instance of " + getDeclaringClass(), e);
96 } catch (IllegalAccessException e) {
97 throw new ReflectionException("Illegal access to field: " + getName(), e);
101 /** Sets the value of the field on the supplied object. */
102 public void set(Object obj, Object value) throws ReflectionException {
104 field.set(obj, value);
105 } catch (IllegalArgumentException e) {
106 throw new ReflectionException("Argument not valid for field: " + getName(), e);
107 } catch (IllegalAccessException e) {
108 throw new ReflectionException("Illegal access to field: " + getName(), e);