forked from junkdog/artemis-odb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ClassReflection.java
188 lines (166 loc) · 8.31 KB
/
ClassReflection.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*******************************************************************************
* Copyright 2011 See AUTHORS file.
*
* 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.artemis.utils.reflect;
import com.artemis.gwtref.client.ReflectionCache;
import com.artemis.gwtref.client.Type;
import com.artemis.utils.reflect.ReflectionException;
/** Utilities for Class reflection.
* @author nexsoftware */
public final class ClassReflection {
/** Returns the Class object associated with the class or interface with the supplied string name. */
static public Class forName (String name) throws ReflectionException {
try {
return ReflectionCache.forName(name).getClassOfType();
} catch (ClassNotFoundException e) {
throw new ReflectionException("Class not found: " + name);
}
}
/** Returns the simple name of the underlying class as supplied in the source code. */
static public String getSimpleName (Class c) {
return c.getName();
}
/** Determines if the supplied Object is assignment-compatible with the object represented by supplied Class. */
static public boolean isInstance (Class c, Object obj) {
return isAssignableFrom(c, obj.getClass());
}
/** Determines if the class or interface represented by first Class parameter is either the same as, or is a superclass or
* superinterface of, the class or interface represented by the second Class parameter. */
static public boolean isAssignableFrom (Class c1, Class c2) {
Type c1Type = ReflectionCache.getType(c1);
Type c2Type = ReflectionCache.getType(c2);
return c1Type.isAssignableFrom(c2Type);
}
/** Returns true if the class or interface represented by the supplied Class is a member class. */
static public boolean isMemberClass (Class c) {
return ReflectionCache.getType(c).isMemberClass();
}
/** Returns true if the class or interface represented by the supplied Class is a static class. */
static public boolean isStaticClass (Class c) {
return ReflectionCache.getType(c).isStatic();
}
/** Creates a new instance of the class represented by the supplied Class. */
static public <T> T newInstance (Class<T> c) throws ReflectionException {
try {
return (T)ReflectionCache.getType(c).newInstance();
} catch (NoSuchMethodException e) {
throw new ReflectionException("Could not use default constructor of " + c.getName(), e);
}
}
/** Returns an array of {@link Constructor} containing the public constructors of the class represented by the supplied Class. */
static public Constructor[] getConstructors (Class c) {
com.artemis.gwtref.client.Constructor[] constructors = ReflectionCache.getType(c).getConstructors();
Constructor[] result = new Constructor[constructors.length];
for (int i = 0, j = constructors.length; i < j; i++) {
result[i] = new Constructor(constructors[i]);
}
return result;
}
/** Returns a {@link Constructor} that represents the public constructor for the supplied class which takes the supplied
* parameter types. */
static public Constructor getConstructor (Class c, Class... parameterTypes) throws ReflectionException {
try {
return new Constructor(ReflectionCache.getType(c).getConstructor(parameterTypes));
} catch (SecurityException e) {
throw new ReflectionException("Security violation while getting constructor for class: " + c.getName(), e);
} catch (NoSuchMethodException e) {
throw new ReflectionException("Constructor not found for class: " + c.getName(), e);
}
}
/** Returns a {@link Constructor} that represents the constructor for the supplied class which takes the supplied parameter
* types. */
static public Constructor getDeclaredConstructor (Class c, Class... parameterTypes) throws ReflectionException {
try {
return new Constructor(ReflectionCache.getType(c).getDeclaredConstructor(parameterTypes));
} catch (SecurityException e) {
throw new ReflectionException("Security violation while getting constructor for class: " + c.getName(), e);
} catch (NoSuchMethodException e) {
throw new ReflectionException("Constructor not found for class: " + c.getName(), e);
}
}
/** Returns an array of {@link Method} containing the public member methods of the class represented by the supplied Class. */
static public Method[] getMethods (Class c) {
com.artemis.gwtref.client.Method[] methods = ReflectionCache.getType(c).getMethods();
Method[] result = new Method[methods.length];
for (int i = 0, j = methods.length; i < j; i++) {
result[i] = new Method(methods[i]);
}
return result;
}
/** Returns a {@link Method} that represents the public member method for the supplied class which takes the supplied parameter
* types. */
static public Method getMethod (Class c, String name, Class... parameterTypes) throws ReflectionException {
try {
return new Method(ReflectionCache.getType(c).getMethod(name, parameterTypes));
} catch (SecurityException e) {
throw new ReflectionException("Security violation while getting method: " + name + ", for class: " + c.getName(), e);
} catch (NoSuchMethodException e) {
throw new ReflectionException("Method not found: " + name + ", for class: " + c.getName(), e);
}
}
/** Returns an array of {@link Method} containing the methods declared by the class represented by the supplied Class. */
static public Method[] getDeclaredMethods (Class c) {
com.artemis.gwtref.client.Method[] methods = ReflectionCache.getType(c).getDeclaredMethods();
Method[] result = new Method[methods.length];
for (int i = 0, j = methods.length; i < j; i++) {
result[i] = new Method(methods[i]);
}
return result;
}
/** Returns a {@link Method} that represents the method declared by the supplied class which takes the supplied parameter types. */
static public Method getDeclaredMethod (Class c, String name, Class... parameterTypes) throws ReflectionException {
try {
return new Method(ReflectionCache.getType(c).getMethod(name, parameterTypes));
} catch (SecurityException e) {
throw new ReflectionException("Security violation while getting method: " + name + ", for class: " + c.getName(), e);
} catch (NoSuchMethodException e) {
throw new ReflectionException("Method not found: " + name + ", for class: " + c.getName(), e);
}
}
/** Returns an array of {@link Field} containing the public fields of the class represented by the supplied Class. */
static public Field[] getFields (Class c) {
com.artemis.gwtref.client.Field[] fields = ReflectionCache.getType(c).getFields();
Field[] result = new Field[fields.length];
for (int i = 0, j = fields.length; i < j; i++) {
result[i] = new Field(fields[i]);
}
return result;
}
/** Returns a {@link Field} that represents the specified public member field for the supplied class. */
static public Field getField (Class c, String name) throws ReflectionException {
try {
return new Field(ReflectionCache.getType(c).getField(name));
} catch (SecurityException e) {
throw new ReflectionException("Security violation while getting field: " + name + ", for class: " + c.getName(), e);
}
}
/** Returns an array of {@link Field} objects reflecting all the fields declared by the supplied class. */
static public Field[] getDeclaredFields (Class c) {
com.artemis.gwtref.client.Field[] fields = ReflectionCache.getType(c).getDeclaredFields();
Field[] result = new Field[fields.length];
for (int i = 0, j = fields.length; i < j; i++) {
result[i] = new Field(fields[i]);
}
return result;
}
/** Returns a {@link Field} that represents the specified declared field for the supplied class. */
static public Field getDeclaredField (Class c, String name) throws ReflectionException {
try {
return new Field(ReflectionCache.getType(c).getField(name));
} catch (SecurityException e) {
throw new ReflectionException("Security violation while getting field: " + name + ", for class: " + c.getName(), e);
}
}
}