OSDN Git Service

0542f9b50f382c791d2d43b9401a299e263cbb8b
[mikumikustudio/libgdx-mikumikustudio.git] / backends / gdx-backends-gwt / src / com / badlogic / gdx / backends / gwt / emu / com / badlogic / gdx / utils / reflect / Field.java
1 /*******************************************************************************\r
2  * Copyright 2011 See AUTHORS file.\r
3  * \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
7  * \r
8  *   http://www.apache.org/licenses/LICENSE-2.0\r
9  * \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  ******************************************************************************/
16
17 package com.badlogic.gdx.utils.reflect;
18
19 import java.lang.reflect.Modifier;
20 import java.lang.reflect.ParameterizedType;
21 import java.lang.reflect.Type;
22
23 /** Provides information about, and access to, a single field of a class or interface.
24  * @author nexsoftware */
25 public final class Field {
26         
27         private final com.badlogic.gwtref.client.Field field;
28         
29         Field(com.badlogic.gwtref.client.Field field) {
30                 this.field = field;
31         }
32         
33         /** Returns the name of the field. */
34         public String getName() {
35                 return field.getName();
36         }
37         
38         /** Returns a Class object that identifies the declared type for the field. */
39         public Class getType() {
40                 return field.getType().getClassOfType();
41         }
42         
43         /** Returns the Class object representing the class or interface that declares the field. */
44         public Class getDeclaringClass() {
45                 return field.getEnclosingType().getClassOfType();
46         }
47         
48         public boolean isAccessible() {
49                 return field.isAccessible();            
50         }
51         
52         public void setAccessible(boolean accessible) {
53                 field.setAccessible(accessible);
54         }
55
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();
59         }
60         
61         /** Return true if the field includes the {@code final} modifier. */
62         public boolean isFinal() {
63                 return field.isFinal();
64         }
65         
66         /** Return true if the field includes the {@code private} modifier. */
67         public boolean isPrivate() {
68                 return field.isPrivate();
69         }
70         
71         /** Return true if the field includes the {@code protected} modifier. */
72         public boolean isProtected() {
73                 return field.isProtected();
74         }
75         
76         /** Return true if the field includes the {@code public} modifier. */
77         public boolean isPublic() {
78                 return field.isPublic();
79         }
80         
81         /** Return true if the field includes the {@code static} modifier. */
82         public boolean isStatic() {
83                 return field.isStatic();
84         }
85
86         /** Return true if the field includes the {@code transient} modifier. */
87         public boolean isTransient() {
88                 return field.isTransient();
89         }
90         
91         /** Return true if the field includes the {@code volatile} modifier. */
92         public boolean isVolatile() {
93                 return field.isVolatile();
94         }
95
96         /** Return true if the field is a synthetic field. */
97         public boolean isSynthetic() {
98                 return field.isSynthetic();
99         }
100         
101         /** If the type of the field is parameterized, returns the Class object representing the parameter type at the specified index,
102          * null otherwise. */
103         public Class getElementType (int index) {
104                 return null;
105         }
106         
107         /** Returns the value of the field on the supplied object. */
108         public Object get(Object obj) throws ReflectionException {
109                 try {
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);
115                 }       
116         }
117         
118         /** Sets the value of the field on the supplied object. */
119         public void set(Object obj, Object value) throws ReflectionException {
120                 try {
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);
126                 }
127         }
128         
129 }