@Test public void test_method_matcher() { Method testMethod = MethodMatcher.findMatchingMethod(TestedClass.class, Integer.class, "aa", ProjectStatus.FIRST); Assertions.assertThat(testMethod) .isNotNull(); Assertions.assertThat(testMethod.getName()) .isEqualTo("test"); Method testMethod2 = MethodMatcher.findMatchingMethod(TestedClass.class, Integer.class, "aa", ProjectStatus.FIRST, null); Assertions.assertThat(testMethod2) .isNotNull(); Assertions.assertThat(testMethod2.getName()) .isEqualTo("test2"); Method testMethod3 = MethodMatcher.findMatchingMethod(TestedClass.class, null, "aa", 1); Assertions.assertThat(testMethod3) .isNotNull(); Assertions.assertThat(testMethod3.getName()) .isEqualTo("test3"); }
public static Method findMatchingMethod(Class<?> classToInspect, Class<?> returnType, Object... params) { Method[] methods = classToInspect.getMethods(); Method checkedMethod = null; for (Method method : methods) { Type[] parameterTypes = method.getParameterTypes(); boolean matchReturnType = returnType == null || returnType.equals(method.getReturnType()); if (checkParams(parameterTypes, params) && matchReturnType) { if (checkedMethod == null) { checkedMethod = method; } else { throw BusinessException.createNew(BusinessErrorCode.AMBIGUOUS_METHOD_FOUND) .put("method1", method) .put("method2", checkedMethod) .put("object", classToInspect.getSimpleName()) .put("parameters", params); } } } return checkedMethod; }
MethodMatcher { public static Method findMatchingMethod(Class<?> classToInspect, Class<?> returnType, Object... params) { Method[] methods = classToInspect.getMethods(); Method checkedMethod = null; for (Method method : methods) { Type[] parameterTypes = method.getParameterTypes(); boolean matchReturnType = returnType == null || returnType.equals(method.getReturnType()); if (checkParams(parameterTypes, params) && matchReturnType) { if (checkedMethod == null) { checkedMethod = method; } else { throw BusinessException.createNew(BusinessErrorCode.AMBIGUOUS_METHOD_FOUND) .put("method1", method) .put("method2", checkedMethod) .put("object", classToInspect.getSimpleName()) .put("parameters", params); } } } return checkedMethod; } }
MethodMatcher { public static Method findMatchingMethod(Class<?> classToInspect, Class<?> returnType, Object... params) { Method[] methods = classToInspect.getMethods(); Method checkedMethod = null; for (Method method : methods) { Type[] parameterTypes = method.getParameterTypes(); boolean matchReturnType = returnType == null || returnType.equals(method.getReturnType()); if (checkParams(parameterTypes, params) && matchReturnType) { if (checkedMethod == null) { checkedMethod = method; } else { throw BusinessException.createNew(BusinessErrorCode.AMBIGUOUS_METHOD_FOUND) .put("method1", method) .put("method2", checkedMethod) .put("object", classToInspect.getSimpleName()) .put("parameters", params); } } } return checkedMethod; } MethodMatcher(); }
MethodMatcher { public static Method findMatchingMethod(Class<?> classToInspect, Class<?> returnType, Object... params) { Method[] methods = classToInspect.getMethods(); Method checkedMethod = null; for (Method method : methods) { Type[] parameterTypes = method.getParameterTypes(); boolean matchReturnType = returnType == null || returnType.equals(method.getReturnType()); if (checkParams(parameterTypes, params) && matchReturnType) { if (checkedMethod == null) { checkedMethod = method; } else { throw BusinessException.createNew(BusinessErrorCode.AMBIGUOUS_METHOD_FOUND) .put("method1", method) .put("method2", checkedMethod) .put("object", classToInspect.getSimpleName()) .put("parameters", params); } } } return checkedMethod; } MethodMatcher(); static Method findMatchingMethod(Class<?> classToInspect, Class<?> returnType, Object... params); @SuppressWarnings("unchecked") static Constructor<T> findMatchingConstructor(Class<T> classToInspect, Object... params); }
MethodMatcher { public static Method findMatchingMethod(Class<?> classToInspect, Class<?> returnType, Object... params) { Method[] methods = classToInspect.getMethods(); Method checkedMethod = null; for (Method method : methods) { Type[] parameterTypes = method.getParameterTypes(); boolean matchReturnType = returnType == null || returnType.equals(method.getReturnType()); if (checkParams(parameterTypes, params) && matchReturnType) { if (checkedMethod == null) { checkedMethod = method; } else { throw BusinessException.createNew(BusinessErrorCode.AMBIGUOUS_METHOD_FOUND) .put("method1", method) .put("method2", checkedMethod) .put("object", classToInspect.getSimpleName()) .put("parameters", params); } } } return checkedMethod; } MethodMatcher(); static Method findMatchingMethod(Class<?> classToInspect, Class<?> returnType, Object... params); @SuppressWarnings("unchecked") static Constructor<T> findMatchingConstructor(Class<T> classToInspect, Object... params);  }