CarpaccioHelper { public static ObjectAndMethod findObjectWithThisMethod(List<Object> objects, String function, int numberOfParams) { if (objects != null && function != null) { Method method; Object object; int numberOfObjects = objects.size(); for (int j = 0; j < numberOfObjects; ++j) { object = objects.get(j); int methodCount = object.getClass().getMethods().length; for (int i = 0; i < methodCount; ++i) { method = object.getClass().getMethods()[i]; if (function.equals(method.getName()) && method.getParameterTypes().length == numberOfParams) { return new ObjectAndMethod(object, method); } } } CarpaccioLogger.v(TAG, "can't find controller with the method " + function + " , controllers=" + objects.toString()); } return null; }  static Object construct(String name); static Class[] getClassesWithHeaderClass(Object[] args, Class headerClass); static Class[] getClasses(Object[] args); static Object[] getArgumentsWithView(View view, Class[] parametersType, Object[] args); static String getFunctionName(String tag); static String[] getAttributes(String tag); static Object removeTag(View view, String actionName); static String[] trim(String[] strings); static ObjectAndMethod findObjectWithThisMethod(List<Object> objects, String function, int numberOfParams); static Method callFunction(Object object, String name, View view, Object[] args); static Method callMethod(Object object, Method method, String name, View view, Object[] args); static T callFunction(Object object, String name); static T callFunction(Object object, String name, Object[] args); static Integer stringToInt(String s); static Double stringToDouble(String s); static Long stringToLong(String s); static Float stringToFloat(String s); static boolean isNumber(Class destinationClass); static Object stringToNumber(String s, Class destinationClass); static T findParentOfClass(View view, Class<T> theClass); static Carpaccio findParentCarpaccio(View view); static Carpaccio registerToParentCarpaccio(View view); static String TAG; static boolean LOG_FAILURES; }
@Test public void testFindObjectWithThisMethod() throws Exception { Controller1 testObject = new Controller1(); Controller2 testObjectfail = new Controller2(); ObjectAndMethod objectAndMethod = CarpaccioHelper.findObjectWithThisMethod(Arrays.asList(testObjectfail, testObject), "setText", 2); assertNotNull(objectAndMethod); assertEquals(testObject, objectAndMethod.getObject()); assertEquals("setText", objectAndMethod.getMethod().getName()); assertEquals(2, objectAndMethod.getMethod().getParameterTypes().length); }
@Test public void testFindObjectWithThisMethod_fail() throws Exception { Controller2 testObjectfail1 = new Controller2(); Controller2 testObjectfail2 = new Controller2(); List<Object> arrayList = new ArrayList<>(); arrayList.add(testObjectfail1); arrayList.add(testObjectfail2); ObjectAndMethod objectAndMethod = CarpaccioHelper.findObjectWithThisMethod(arrayList, "setText", 2); assertNull(objectAndMethod); }