ReflectionEngine { public Field getField(Class<?> clazz, String name) { Field field = null; try { field = clazz.getField(name); if (!Modifier.isPublic(field.getModifiers()) && !field.isAccessible()) { field = null; } } catch (NoSuchFieldException e) { field = null; } catch (Exception e) { field = null; } return field; }  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 testGetField() { Cat cat = new Cat(); assertNull(rEngine.getField(cat, "age")); assertNull(rEngine.getField(cat, "age1")); assertEquals(rEngine.getField(cat, "age2").getType(), int.class); assertEquals(rEngine.getField(Cat.class, "age2").getType(), int.class); assertEquals(rEngine.getField("p1.Cat", "age2").getType(), int.class); assertEquals(rEngine.getField(cat, "CONSTANT").getType(), String.class); assertNull(rEngine.getField(cat, "age4")); assertNull(rEngine.getField(cat, "age5")); }