ReflectionEngine { public Method getMethod(Class<?> clazz, String name) { Method m = null; try { for (Method tempMethod : clazz.getMethods()) { if (tempMethod.getName().equals(name)) { m = tempMethod; break; } } } catch (Exception e) { m = null; } return m; }  Object createArray(String fqn, int[] dimensions); Class<?> getClass(Class<?> clazz, String name); Class<?>[] getClassParameters(Object[] parameters); MethodInvoker getConstructor(Class<?> clazz, Class<?>[] parameters); MethodInvoker getConstructor(String classFQN, Object[] parameters); Field getField(Class<?> clazz, String name); Field getField(Object obj, String name); Field getField(String classFQN, String name); Object getFieldValue(Object obj, Field field); Method getMethod(Class<?> clazz, String name); MethodInvoker getMethod(Class<?> clazz, String name, Class<?>[] parameters); MethodInvoker getMethod(Object object, String name, Object[] parameters); MethodInvoker getMethod(String classFQN, String name, Object[] parameters); Object invoke(Object object, MethodInvoker invoker, Object[] parameters); void setFieldValue(Object obj, Field field, Object value); String[] getPublicMethodNames(Object obj); String[] getPublicFieldNames(Object obj); String[] getPublicStaticFieldNames(Class<?> clazz); String[] getPublicStaticMethodNames(Class<?> clazz); String[] getPublicStaticClassNames(Class<?> clazz); String[] getPublicStaticNames(Class<?> clazz); final static int cacheSize; final static Object RETURN_VOID; }
@Test public void testGetSimpleMethod() { assertNull(rEngine.getMethod(Cat.class, "methodABC")); assertNotNull(rEngine.getMethod(Cat.class, "meow")); assertNotNull(rEngine.getMethod(Cat.class, "meow15")); }
@Test public void testGetMethod() { try { ReflectionEngine engine = new ReflectionEngine(); TestEngine2 tEngine = new TestEngine2(); MethodInvoker mInvoker = engine.getMethod(tEngine, "method1", new Object[] { new Object(), new Object() }); assertArrayEquals(mInvoker.getMethod().getParameterTypes(), new Class[] { Object.class, Object.class }); mInvoker = engine.getMethod(tEngine, "method1", new Object[] { new Object(), new Object() }); assertArrayEquals(mInvoker.getMethod().getParameterTypes(), new Class[] { Object.class, Object.class }); mInvoker = engine.getMethod(tEngine, "method2", new Object[] { new Object() }); assertArrayEquals(mInvoker.getMethod().getParameterTypes(), new Class[] { Object.class }); mInvoker = engine.getMethod(tEngine, "method1", new Object[] {}); assertArrayEquals(mInvoker.getMethod().getParameterTypes(), new Class[] {}); try { mInvoker = engine.getMethod(tEngine, "method3", new Object[] { new Object() }); fail(); } catch (Exception e) { } try { mInvoker = engine.getMethod(tEngine, "method2", new Object[] { new Object(), new Object() }); fail(); } catch (Exception e) { } try { mInvoker = engine.getMethod(tEngine, "method1", new Object[] { new Object(), new Object(), new Object() }); fail(); } catch (Exception e) { } mInvoker = engine.getMethod(tEngine, "method1", new Object[] { new String(), new ArrayList<String>() }); assertArrayEquals(mInvoker.getMethod().getParameterTypes(), new Class[] { String.class, Object.class }); mInvoker = engine.getMethod(tEngine, "method1", new Object[] { new String(), new String() }); assertArrayEquals(mInvoker.getMethod().getParameterTypes(), new Class[] { String.class, char.class }); mInvoker = engine.getMethod(tEngine, "method1", new Object[] { 2, 2.2 }); assertArrayEquals(mInvoker.getMethod().getParameterTypes(), new Class[] { int.class, double.class }); mInvoker = engine.getMethod(tEngine, "method1", new Object[] { "2", true }); assertArrayEquals(mInvoker.getMethod().getParameterTypes(), new Class[] { Object.class, Boolean.class }); } catch (Exception e) { e.printStackTrace(); fail(); } }
@Test public void testGetMethodDeepInheritance() { try { ReflectionEngine engine = new ReflectionEngine(); MethodInvoker mInvoker; TestEngine2 tEngine2 = new TestEngine2(); mInvoker = engine.getMethod(tEngine2, "method3", new Object[] { tEngine2 }); assertArrayEquals(mInvoker.getMethod().getParameterTypes(), new Class[] { TestInterface.class }); TestEngine3 tEngine3 = new TestEngine3(); mInvoker = engine.getMethod(tEngine3, "method3", new Object[] { tEngine3 }); assertArrayEquals(mInvoker.getMethod().getParameterTypes(), new Class[] { TestInterface.class }); } catch (Exception e) { e.printStackTrace(); fail(); } }