import com.badlogic.gwtref.client.ReflectionCache;
+/** Utilities for Array reflection.
+ * @author nexsoftware */
public final class ArrayReflection {
+ /** Creates a new array with the specified component type and length. */
static public Object newInstance (Class c, int size) {
return ReflectionCache.instance.newArray(c, size);
}
+ /** Returns the length of the supplied array. */
static public int getLength (Object array) {
return ReflectionCache.instance.getArrayLength(ReflectionCache.getType(array.getClass()), array);
}
+ /** Returns the value of the indexed component in the supplied array. */
static public Object get (Object array, int index) {
ReflectionCache.instance.getArrayElement(ReflectionCache.getType(array.getClass()), array, index);
return array;
}
+ /** Sets the value of the indexed component in the supplied array to the supplied value. */
static public void set (Object array, int index, Object value) {
ReflectionCache.instance.setArrayElement(ReflectionCache.getType(array.getClass()), array, index, value);
}
import com.badlogic.gwtref.client.ReflectionCache;
import com.badlogic.gwtref.client.Type;
+/** 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 {
- return ReflectionCache.instance.forName(name).getClassOfType();
+ Type type = ReflectionCache.instance.forName(name);
+ if (type != null) {
+ return type.getClassOfType();
+ }
+ 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);
+ return isAssignableFrom(c, obj.getClass());
}
- static public boolean isAssignableFrom (Class c, Object obj) {
- Type cType = ReflectionCache.getType(c);
- Type objType = ReflectionCache.getType(obj.getClass());
- return cType.isAssignableFrom(objType);
+ /** 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 Object newInstance (Class c) throws ReflectionException {
return ReflectionCache.getType(c).newInstance();
}
+ /** Returns an array of {@link Constructor} containing the public constructors of the class represented by the supplied Class. */
static public Constructor[] getConstructors (Class c) {
throw new GdxRuntimeException("Not implemented.");
}
+ /** 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 {
throw new GdxRuntimeException("Not implemented.");
}
+ /** 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 {
throw new GdxRuntimeException("Not implemented.");
}
+ /** 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.badlogic.gwtref.client.Method[] methods = ReflectionCache.getType(c).getMethods();
Method[] result = new Method[methods.length];
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("", e); // TODO: Real Message
+ throw new ReflectionException("Security violation while getting method: " + name + ", for class: " + c.getName(), e);
} catch (NoSuchMethodException e) {
- throw new ReflectionException("", e); // TODO: Real Message
+ 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.badlogic.gwtref.client.Method[] methods = ReflectionCache.getType(c).getDeclaredMethods();
Method[] result = new Method[methods.length];
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("", e); // TODO: Real Message
+ throw new ReflectionException("Security violation while getting method: " + name + ", for class: " + c.getName(), e);
} catch (NoSuchMethodException e) {
- throw new ReflectionException("", e); // TODO: Real Message
+ 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.badlogic.gwtref.client.Field[] fields = ReflectionCache.getType(c).getFields();
Field[] result = new Field[fields.length];
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("", e); // TODO: Real Message
+ throw new ReflectionException("Security violation while getting field: " + name + ", for class: " + c.getName(), e);
}
}
+ /** Returns a {@link Field} that represents the specified public member field for the supplied class. */
static public Field[] getDeclaredFields (Class c) {
com.badlogic.gwtref.client.Field[] fields = ReflectionCache.getType(c).getDeclaredFields();
Field[] result = new Field[fields.length];
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("", e); // TODO: Real Message
+ throw new ReflectionException("Security violation while getting field: " + name + ", for class: " + c.getName(), e);
}
}
-
- static public String getSimpleName(Class c) {
- return c.getName();
- }
}
\ No newline at end of file
import java.lang.reflect.InvocationTargetException;
+/** Provides information about, and access to, a single constructor for a Class.
+ * @author nexsoftware */
public final class Constructor {
private final com.badlogic.gwtref.client.Constructor constructor;
this.constructor = constructor;
}
+ /** Returns an array of Class objects that represent the formal parameter types, in declaration order, of the constructor. */
public Class[] getParameterTypes() {
return null;
}
+ /** Returns the Class object representing the class or interface that declares the constructor. */
public Class getDeclaringClass() {
return constructor.getEnclosingType().getClassOfType();
}
constructor.setAccessible(accessible);
}
+ /** Uses the constructor to create and initialize a new instance of the constructor's declaring class, with the supplied initialization parameters. */
public Object newInstance(Object... args) throws ReflectionException {
try {
return constructor.newInstance();
} catch (IllegalArgumentException e) {
- throw new ReflectionException("", e); // TODO: Real Message
+ throw new ReflectionException("Illegal argument(s) supplied to constructor for class: " + getDeclaringClass().getName(), e);
}
}
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
+/** Provides information about, and access to, a single field of a class or interface.
+ * @author nexsoftware */
public final class Field {
private final com.badlogic.gwtref.client.Field field;
this.field = field;
}
+ /** Returns the name of the field. */
public String getName() {
return field.getName();
}
+ /** Returns a Class object that identifies the declared type for the field. */
public Class getType() {
return field.getType().getClassOfType();
}
+ /** Returns the Class object representing the class or interface that declares the field. */
public Class getDeclaringClass() {
return field.getEnclosingType().getClassOfType();
}
field.setAccessible(accessible);
}
+ /** Return true if the field does not include any of the {@code private}, {@code protected}, or {@code public} modifiers. */
public boolean isDefaultAccess() {
return !isPrivate() && ! isProtected() && ! isPublic();
}
+ /** Return true if the field includes the {@code final} modifier. */
public boolean isFinal() {
return field.isFinal();
}
+ /** Return true if the field includes the {@code private} modifier. */
public boolean isPrivate() {
return field.isPrivate();
}
+ /** Return true if the field includes the {@code protected} modifier. */
public boolean isProtected() {
return field.isProtected();
}
+ /** Return true if the field includes the {@code public} modifier. */
public boolean isPublic() {
return field.isPublic();
}
+ /** Return true if the field includes the {@code static} modifier. */
public boolean isStatic() {
return field.isStatic();
}
+ /** Return true if the field includes the {@code transient} modifier. */
public boolean isTransient() {
return field.isTransient();
}
+ /** Return true if the field includes the {@code volatile} modifier. */
public boolean isVolatile() {
return field.isVolatile();
}
+ /** Return true if the field is a synthetic field. */
public boolean isSynthetic() {
return field.isSynthetic();
}
+ /** If the type of the field is parameterized, returns the Class object representing the parameter type, null otherwise. */
public Class getElementType() {
return null;
}
+ /** Returns the value of the field on the supplied object. */
public Object get(Object obj) throws ReflectionException {
try {
return field.get(obj);
} catch (IllegalArgumentException e) {
- throw new ReflectionException("", e); // TODO: Real Message
+ throw new ReflectionException("Object is not an instance of " + getDeclaringClass(), e);
} catch (IllegalAccessException e) {
- throw new ReflectionException("", e); // TODO: Real Message
+ throw new ReflectionException("Illegal access to field: " + getName(), e);
}
}
+ /** Sets the value of the field on the supplied object. */
public void set(Object obj, Object value) throws ReflectionException {
try {
field.set(obj, value);
} catch (IllegalArgumentException e) {
- throw new ReflectionException("", e); // TODO: Real Message
+ throw new ReflectionException("Argument not valid for field: " + getName(), e);
} catch (IllegalAccessException e) {
- throw new ReflectionException("", e); // TODO: Real Message
+ throw new ReflectionException("Illegal access to field: " + getName(), e);
}
}
import com.badlogic.gwtref.client.Parameter;
+/** Provides information about, and access to, a single method on a class or interface.
+ * @author nexsoftware */
public final class Method {
private final com.badlogic.gwtref.client.Method method;
this.method = method;
}
+ /** Returns the name of the method. */
public String getName() {
return method.getName();
}
+ /** Returns a Class object that represents the formal return type of the method. */
public Class getReturnType() {
return method.getReturnType();
}
+ /** Returns an array of Class objects that represent the formal parameter types, in declaration order, of the method. */
public Class[] getParameterTypes() {
Parameter[] parameters = method.getParameters();
Class[] parameterTypes = new Class[parameters.length];
return parameterTypes;
}
+ /** Returns the Class object representing the class or interface that declares the method. */
public Class getDeclaringClass() {
return method.getEnclosingType();
}
public void setAccessible(boolean accessible) {
method.setAccessible(accessible);
}
-
+
+ /** Return true if the method includes the {@code abstract} modifier. */
public boolean isAbstract() {
return method.isAbstract();
}
+ /** Return true if the method does not include any of the {@code private}, {@code protected}, or {@code public} modifiers. */
public boolean isDefaultAccess() {
return !isPrivate() && ! isProtected() && ! isPublic();
}
+ /** Return true if the method includes the {@code final} modifier. */
public boolean isFinal() {
return method.isFinal();
}
+ /** Return true if the method includes the {@code private} modifier. */
public boolean isPrivate() {
return method.isPrivate();
}
+ /** Return true if the method includes the {@code protected} modifier. */
public boolean isProtected() {
return method.isProtected();
}
+ /** Return true if the method includes the {@code public} modifier. */
public boolean isPublic() {
return method.isPublic();
}
+ /** Return true if the method includes the {@code native} modifier. */
public boolean isNative() {
return method.isNative();
}
- public boolean isVarArgs() {
- return method.isVarArgs();
- }
-
+ /** Return true if the method includes the {@code static} modifier. */
public boolean isStatic() {
return method.isStatic();
}
+ /** Return true if the method takes a variable number of arguments. */
+ public boolean isVarArgs() {
+ return method.isVarArgs();
+ }
+
+ /** Invokes the underlying method on the supplied object with the supplied parameters. */
public Object invoke(Object obj, Object... args) throws ReflectionException {
try {
return method.invoke(obj, args);
} catch (IllegalArgumentException e) {
- throw new ReflectionException("", e); // TODO: Real Message
+ throw new ReflectionException("Illegal argument(s) supplied to method: " + getName(), e);
}
}
try {
return constructor.newInstance(args);
} catch (IllegalArgumentException e) {
- throw new ReflectionException("Illegal argument(s) supplied to constructor for class: " + constructor.getDeclaringClass().getName(), e);
+ throw new ReflectionException("Illegal argument(s) supplied to constructor for class: " + getDeclaringClass().getName(), e);
} catch (InstantiationException e) {
- throw new ReflectionException("Could not instantiate instance of class: " + constructor.getDeclaringClass().getName(), e);
+ throw new ReflectionException("Could not instantiate instance of class: " + getDeclaringClass().getName(), e);
} catch (IllegalAccessException e) {
- throw new ReflectionException("Could not instantiate instance of class: " + constructor.getDeclaringClass().getName(), e);
+ throw new ReflectionException("Could not instantiate instance of class: " + getDeclaringClass().getName(), e);
} catch (InvocationTargetException e) {
- throw new ReflectionException("Exception occurred in constructor for class: " + constructor.getDeclaringClass().getName(), e);
+ throw new ReflectionException("Exception occurred in constructor for class: " + getDeclaringClass().getName(), e);
}
}
method.setAccessible(accessible);
}
- /** Return true if the method includes the abstract modifier. */
+ /** Return true if the method includes the {@code abstract} modifier. */
public boolean isAbstract () {
return Modifier.isAbstract(method.getModifiers());
}
- /** Return true if the method does not include any of the private, protected, or public modifiers. */
+ /** Return true if the method does not include any of the {@code private}, {@code protected}, or {@code public} modifiers. */
public boolean isDefaultAccess () {
return !isPrivate() && !isProtected() && !isPublic();
}
- /** Return true if the method includes the final modifier. */
+ /** Return true if the method includes the {@code final} modifier. */
public boolean isFinal () {
return Modifier.isFinal(method.getModifiers());
}
- /** Return true if the method includes the private modifier. */
+ /** Return true if the method includes the {@code private} modifier. */
public boolean isPrivate () {
return Modifier.isPrivate(method.getModifiers());
}
- /** Return true if the method includes the protected modifier. */
+ /** Return true if the method includes the {@code protected} modifier. */
public boolean isProtected () {
return Modifier.isProtected(method.getModifiers());
}
- /** Return true if the method includes the public modifier. */
+ /** Return true if the method includes the {@code public} modifier. */
public boolean isPublic () {
return Modifier.isPublic(method.getModifiers());
}
- /** Return true if the method includes the native modifier. */
+ /** Return true if the method includes the {@code native} modifier. */
public boolean isNative () {
return Modifier.isNative(method.getModifiers());
}
- /** Return true if the method includes the static modifier. */
+ /** Return true if the method includes the {@code static} modifier. */
public boolean isStatic () {
return Modifier.isStatic(method.getModifiers());
}