1 /*******************************************************************************
\r
2 * Copyright 2011 See AUTHORS file.
\r
4 * Licensed under the Apache License, Version 2.0 (the "License");
\r
5 * you may not use this file except in compliance with the License.
\r
6 * You may obtain a copy of the License at
\r
8 * http://www.apache.org/licenses/LICENSE-2.0
\r
10 * Unless required by applicable law or agreed to in writing, software
\r
11 * distributed under the License is distributed on an "AS IS" BASIS,
\r
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
13 * See the License for the specific language governing permissions and
\r
14 * limitations under the License.
\r
15 ******************************************************************************/
17 package com.badlogic.gdx.utils.reflect;
19 import java.lang.reflect.Modifier;
20 import java.lang.reflect.ParameterizedType;
21 import java.lang.reflect.Type;
23 /** Provides information about, and access to, a single field of a class or interface.
24 * @author nexsoftware */
25 public final class Field {
27 private final com.badlogic.gwtref.client.Field field;
29 Field(com.badlogic.gwtref.client.Field field) {
33 /** Returns the name of the field. */
34 public String getName() {
35 return field.getName();
38 /** Returns a Class object that identifies the declared type for the field. */
39 public Class getType() {
40 return field.getType().getClassOfType();
43 /** Returns the Class object representing the class or interface that declares the field. */
44 public Class getDeclaringClass() {
45 return field.getEnclosingType().getClassOfType();
48 public boolean isAccessible() {
49 return field.isAccessible();
52 public void setAccessible(boolean accessible) {
53 field.setAccessible(accessible);
56 /** Return true if the field does not include any of the {@code private}, {@code protected}, or {@code public} modifiers. */
57 public boolean isDefaultAccess() {
58 return !isPrivate() && ! isProtected() && ! isPublic();
61 /** Return true if the field includes the {@code final} modifier. */
62 public boolean isFinal() {
63 return field.isFinal();
66 /** Return true if the field includes the {@code private} modifier. */
67 public boolean isPrivate() {
68 return field.isPrivate();
71 /** Return true if the field includes the {@code protected} modifier. */
72 public boolean isProtected() {
73 return field.isProtected();
76 /** Return true if the field includes the {@code public} modifier. */
77 public boolean isPublic() {
78 return field.isPublic();
81 /** Return true if the field includes the {@code static} modifier. */
82 public boolean isStatic() {
83 return field.isStatic();
86 /** Return true if the field includes the {@code transient} modifier. */
87 public boolean isTransient() {
88 return field.isTransient();
91 /** Return true if the field includes the {@code volatile} modifier. */
92 public boolean isVolatile() {
93 return field.isVolatile();
96 /** Return true if the field is a synthetic field. */
97 public boolean isSynthetic() {
98 return field.isSynthetic();
101 /** If the type of the field is parameterized, returns the Class object representing the parameter type at the specified index,
103 public Class getElementType (int index) {
107 /** Returns the value of the field on the supplied object. */
108 public Object get(Object obj) throws ReflectionException {
110 return field.get(obj);
111 } catch (IllegalArgumentException e) {
112 throw new ReflectionException("Object is not an instance of " + getDeclaringClass(), e);
113 } catch (IllegalAccessException e) {
114 throw new ReflectionException("Illegal access to field: " + getName(), e);
118 /** Sets the value of the field on the supplied object. */
119 public void set(Object obj, Object value) throws ReflectionException {
121 field.set(obj, value);
122 } catch (IllegalArgumentException e) {
123 throw new ReflectionException("Argument not valid for field: " + getName(), e);
124 } catch (IllegalAccessException e) {
125 throw new ReflectionException("Illegal access to field: " + getName(), e);