OSDN Git Service

Add initial reflection wrapper api.
authorJustin Shapcott <support@mobidevelop.com>
Mon, 20 May 2013 14:06:35 +0000 (07:06 -0700)
committerJustin Shapcott <support@mobidevelop.com>
Mon, 20 May 2013 14:06:35 +0000 (07:06 -0700)
gdx/src/com/badlogic/gdx/maps/MapLayers.java
gdx/src/com/badlogic/gdx/maps/MapObjects.java
gdx/src/com/badlogic/gdx/utils/reflect/Constructor.java [new file with mode: 0644]
gdx/src/com/badlogic/gdx/utils/reflect/Field.java [new file with mode: 0644]
gdx/src/com/badlogic/gdx/utils/reflect/Method.java [new file with mode: 0644]
gdx/src/com/badlogic/gdx/utils/reflect/Reflection.java [new file with mode: 0644]
gdx/src/com/badlogic/gdx/utils/reflect/ReflectionException.java [new file with mode: 0644]
gdx/src/com/badlogic/gdx/utils/reflect/Test.java [new file with mode: 0644]

index 850bbc1..e975fd2 100644 (file)
@@ -3,6 +3,7 @@ package com.badlogic.gdx.maps;
 import java.util.Iterator;
 
 import com.badlogic.gdx.utils.Array;
+import com.badlogic.gdx.utils.reflect.Reflection;
 
 /**
  * Ordered list of {@link MapLayer} instances owned by a {@link Map}
@@ -74,7 +75,7 @@ public class MapLayers implements Iterable<MapLayer> {
        public <T extends MapLayer> Array<T> getByType(Class<T> type, Array<T> fill) {
                fill.clear();
                for (MapLayer layer : layers) {
-                       if (type.isInstance(layer)) {
+                       if (Reflection.isInstance(type, layer)) {
                                fill.add((T) layer);
                        }
                }
index 2099ec0..3c42e40 100644 (file)
@@ -3,6 +3,7 @@ package com.badlogic.gdx.maps;
 import java.util.Iterator;
 
 import com.badlogic.gdx.utils.Array;
+import com.badlogic.gdx.utils.reflect.Reflection;
 
 /**
  * @brief Collection of MapObject instances
@@ -83,7 +84,7 @@ public class MapObjects implements Iterable<MapObject> {
        public <T extends MapObject> Array<T> getByType(Class<T> type, Array<T> fill) {
                fill.clear();
                for (MapObject object : objects) {
-                       if (type.isInstance(object)) {
+                       if (Reflection.isInstance(type, object)) {
                                fill.add((T) object);
                        }
                }
diff --git a/gdx/src/com/badlogic/gdx/utils/reflect/Constructor.java b/gdx/src/com/badlogic/gdx/utils/reflect/Constructor.java
new file mode 100644 (file)
index 0000000..468c74a
--- /dev/null
@@ -0,0 +1,39 @@
+package com.badlogic.gdx.utils.reflect;
+
+import java.lang.reflect.InvocationTargetException;
+
+public final class Constructor {
+       
+       private final java.lang.reflect.Constructor constructor;
+       
+       Constructor(java.lang.reflect.Constructor constructor) {
+               this.constructor = constructor;
+       }
+
+       public Class getDeclaringClass() {
+               return constructor.getDeclaringClass();
+       }
+       
+       public boolean isAccessible() {
+               return constructor.isAccessible();              
+       }
+       
+       public void setAccessible(boolean accessible) {
+               constructor.setAccessible(accessible);
+       }
+       
+       public Object newInstance(Object... args) throws ReflectionException {
+               try {
+                       return constructor.newInstance(args);
+               } catch (IllegalArgumentException e) {
+                       throw new ReflectionException("", e); // TODO: Real Message
+               } catch (InstantiationException e) {
+                       throw new ReflectionException("", e); // TODO: Real Message
+               } catch (IllegalAccessException e) {
+                       throw new ReflectionException("", e); // TODO: Real Message
+               } catch (InvocationTargetException e) {
+                       throw new ReflectionException("", e); // TODO: Real Message
+               }               
+       }
+       
+}
diff --git a/gdx/src/com/badlogic/gdx/utils/reflect/Field.java b/gdx/src/com/badlogic/gdx/utils/reflect/Field.java
new file mode 100644 (file)
index 0000000..499af84
--- /dev/null
@@ -0,0 +1,81 @@
+package com.badlogic.gdx.utils.reflect;
+
+import java.lang.reflect.Modifier;
+
+public final class Field {
+       
+       private final java.lang.reflect.Field field;
+       
+       Field(java.lang.reflect.Field field) {
+               this.field = field;
+       }
+       
+       public Class getDeclaringClass() {
+               return field.getDeclaringClass();
+       }
+       
+       public boolean isAccessible() {
+               return field.isAccessible();            
+       }
+       
+       public void setAccessible(boolean accessible) {
+               field.setAccessible(accessible);
+       }
+
+       public boolean isDefaultAccess() {
+               return !isPrivate() && ! isProtected() && ! isPublic();
+       }
+       
+       public boolean isFinal() {
+               return Modifier.isFinal(field.getModifiers());
+       }
+       
+       public boolean isPrivate() {
+               return Modifier.isPrivate(field.getModifiers());
+       }
+       
+       public boolean isProtected() {
+               return Modifier.isProtected(field.getModifiers());
+       }
+       
+       public boolean isPublic() {
+               return Modifier.isPublic(field.getModifiers());
+       }
+       
+       public boolean isStatic() {
+               return Modifier.isStatic(field.getModifiers());
+       }
+
+       public boolean isTransient() {
+               return Modifier.isTransient(field.getModifiers());
+       }
+       
+       public boolean isVolatile() {
+               return Modifier.isVolatile(field.getModifiers());
+       }
+
+       public boolean isSynthetic() {
+               return field.isSynthetic();
+       }
+       
+       public Object get(Object obj) throws ReflectionException {
+               try {
+                       return field.get(obj);
+               } catch (IllegalArgumentException e) {
+                       throw new ReflectionException("", e); // TODO: Real Message
+               } catch (IllegalAccessException e) {
+                       throw new ReflectionException("", e); // TODO: Real Message
+               }       
+       }
+       
+       public void set(Object obj, Object value) throws ReflectionException {
+               try {
+                       field.set(obj, value);
+               } catch (IllegalArgumentException e) {
+                       throw new ReflectionException("", e); // TODO: Real Message
+               } catch (IllegalAccessException e) {
+                       throw new ReflectionException("", e); // TODO: Real Message
+               }
+       }
+       
+}
diff --git a/gdx/src/com/badlogic/gdx/utils/reflect/Method.java b/gdx/src/com/badlogic/gdx/utils/reflect/Method.java
new file mode 100644 (file)
index 0000000..f601b56
--- /dev/null
@@ -0,0 +1,86 @@
+package com.badlogic.gdx.utils.reflect;
+
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Modifier;
+
+public final class Method {
+       
+       private final java.lang.reflect.Method method;
+       
+       Method(java.lang.reflect.Method method) {
+               this.method = method;
+       }
+
+       public String getName() {
+               return method.getName();
+       }
+       
+       public Class getReturnType() {
+               return method.getReturnType();
+       }
+       
+       public Class[] getParameterTypes() {
+               return method.getParameterTypes();
+       }
+       
+       public Class getDeclaringClass() {
+               return method.getDeclaringClass();
+       }
+       
+       public boolean isAccessible() {
+               return method.isAccessible();           
+       }
+       
+       public void setAccessible(boolean accessible) {
+               method.setAccessible(accessible);
+       }
+       
+       public boolean isAbstract() {
+               return Modifier.isAbstract(method.getModifiers());
+       }
+       
+       public boolean isDefaultAccess() {
+               return !isPrivate() && ! isProtected() && ! isPublic();
+       }
+       
+       public boolean isFinal() {
+               return Modifier.isFinal(method.getModifiers());
+       }
+
+       public boolean isPrivate() {
+               return Modifier.isPrivate(method.getModifiers());
+       }
+       
+       public boolean isProtected() {
+               return Modifier.isProtected(method.getModifiers());
+       }
+       
+       public boolean isPublic() {
+               return Modifier.isPublic(method.getModifiers());
+       }
+       
+       public boolean isNative() {
+               return Modifier.isNative(method.getModifiers());
+       }
+       
+       public boolean isVarArgs() {
+               return method.isVarArgs();
+       }
+       
+       public boolean isStatic() {
+               return Modifier.isStatic(method.getModifiers());
+       }
+       
+       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
+               } catch (IllegalAccessException e) {
+                       throw new ReflectionException("", e); // TODO: Real Message
+               } catch (InvocationTargetException e) {
+                       throw new ReflectionException("", e); // TODO: Real Message
+               }
+       }
+       
+}
diff --git a/gdx/src/com/badlogic/gdx/utils/reflect/Reflection.java b/gdx/src/com/badlogic/gdx/utils/reflect/Reflection.java
new file mode 100644 (file)
index 0000000..8420adf
--- /dev/null
@@ -0,0 +1,116 @@
+package com.badlogic.gdx.utils.reflect;
+
+public class Reflection {
+
+       static public Class forName(String name) throws ReflectionException {
+               try {
+                       return Class.forName(name);
+               } catch (ClassNotFoundException e) {
+                       throw new ReflectionException("", e); // TODO: Real Message
+               }
+       }
+       
+       static public boolean isInstance(Class c, Object obj) {
+               return c.isInstance(obj);               
+       }
+       
+       static public boolean isAssignableFrom(Class c, Object obj) {
+               return c.isAssignableFrom(obj.getClass());              
+       }
+       
+       static public Constructor[] getConstructors(Class c) {
+               java.lang.reflect.Constructor[] constructors = 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;
+       }
+       
+       static public Constructor getConstructor(Class c, Class... parameterTypes) throws ReflectionException {
+               try {
+                       return new Constructor(c.getConstructor(parameterTypes));
+               } catch (SecurityException e) {
+                       throw new ReflectionException("", e); // TODO: Real Message
+               } catch (NoSuchMethodException e) {
+                       throw new ReflectionException("", e); // TODO: Real Message
+               }
+       }
+       
+       static public Method[] getMethods(Class c) {
+               java.lang.reflect.Method[] methods = 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;
+       }
+       
+       static public Method getMethod(Class c, String name, Class... parameterTypes) throws ReflectionException {
+               try {
+                       return new Method(c.getMethod(name, parameterTypes));
+               } catch (SecurityException e) {
+                       throw new ReflectionException("", e); // TODO: Real Message
+               } catch (NoSuchMethodException e) {
+                       throw new ReflectionException("", e); // TODO: Real Message
+               }
+       }
+
+       static public Method[] getDeclaredMethods(Class c) {
+               java.lang.reflect.Method[] methods = 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;
+       }
+       
+       static public Method getDeclaredMethod(Class c, String name, Class... parameterTypes) throws ReflectionException {
+               try {
+                       return new Method(c.getDeclaredMethod(name, parameterTypes));
+               } catch (SecurityException e) {
+                       throw new ReflectionException("", e); // TODO: Real Message
+               } catch (NoSuchMethodException e) {
+                       throw new ReflectionException("", e); // TODO: Real Message
+               }
+       }
+       
+       static public Field[] getFields(Class c) {
+               java.lang.reflect.Field[] fields = 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;
+       }
+       
+       static public Field getField(Class c, String name) throws ReflectionException {
+               try {
+                       return new Field(c.getField(name));
+               } catch (SecurityException e) {
+                       throw new ReflectionException("", e); // TODO: Real Message
+               } catch (NoSuchFieldException e) {
+                       throw new ReflectionException("", e); // TODO: Real Message
+               }
+       }
+       
+       static public Field[] getDeclaredFields(Class c) {
+               java.lang.reflect.Field[] fields = 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;
+       }
+       
+       static public Field getDeclaredField(Class c, String name) throws ReflectionException {
+               try {
+                       return new Field(c.getDeclaredField(name));
+               } catch (SecurityException e) {
+                       throw new ReflectionException("", e); // TODO: Real Message
+               } catch (NoSuchFieldException e) {
+                       throw new ReflectionException("", e); // TODO: Real Message
+               }
+       }
+       
+}
diff --git a/gdx/src/com/badlogic/gdx/utils/reflect/ReflectionException.java b/gdx/src/com/badlogic/gdx/utils/reflect/ReflectionException.java
new file mode 100644 (file)
index 0000000..9b876e5
--- /dev/null
@@ -0,0 +1,9 @@
+package com.badlogic.gdx.utils.reflect;
+
+public class ReflectionException extends Exception {
+
+       public ReflectionException (String message, Throwable e) {
+               super(message, e);
+       }
+
+}
diff --git a/gdx/src/com/badlogic/gdx/utils/reflect/Test.java b/gdx/src/com/badlogic/gdx/utils/reflect/Test.java
new file mode 100644 (file)
index 0000000..8adc2cc
--- /dev/null
@@ -0,0 +1,46 @@
+package com.badlogic.gdx.utils.reflect;
+
+import com.badlogic.gdx.maps.Map;
+import com.badlogic.gdx.maps.MapLayer;
+
+public class Test {
+
+       public static class TestClass {
+               
+               public int field1 = 10065;
+
+               public TestClass() {
+                       
+               }
+               
+               public TestClass(int field1) {
+                       this.field1 = field1;
+               }
+               
+               public void mulField1 (int value) {
+                       this.field1 *= value;
+               }
+               
+       }
+       
+       public static void main(String[] args) {
+               try {
+                       Constructor[] constructors = Reflection.getConstructors(TestClass.class);
+                       Method[] methods = Reflection.getMethods(TestClass.class);
+                       Field[] fields = Reflection.getFields(TestClass.class);
+                       
+                       for (Method method : methods) {
+                               System.out.println(method.getName());
+                       }
+                       
+                       TestClass testClass = (TestClass) constructors[1].newInstance(1);
+                       Method setField1 = Reflection.getDeclaredMethod(TestClass.class, "mulField1", int.class);
+                       setField1.invoke(testClass, 2);
+                       System.out.println(testClass.field1);
+               } catch (ReflectionException e) {
+                       e.printStackTrace();
+               }
+
+       }
+       
+}