@Test public void failNoMatchByMatchingArguments() throws ScriptException { assertThatThrownBy(() -> { MethodExecutionStrategy.byMatchingArguments( TestMethod.class, "doSomething", 1, 2, 3, 4); }).isInstanceOf(ScriptException.class); }
public static MethodExecutionStrategy byMatchingArguments(Class<?> clazz, String methodName, Object... arguments) throws ScriptException { List<Method> matchingMethods = new ArrayList<>(); for (Method method : clazz.getMethods()) { if (method.getName().equals(methodName) && (method.getModifiers() & Modifier.PUBLIC) != 0 && ReflectionUtil.matchesArguments(method, arguments)) { matchingMethods.add(method); } } int count = matchingMethods.size(); if (count == 0) { throw new ScriptException("No method '" + methodName + "' with matching arguments found"); } else if (count > 1) { throw new ScriptException("Ambiguous methods '" + methodName + "' with matching arguments found: " + "\n" + matchingMethods.stream().map(Object::toString).collect(Collectors.joining("\n"))); } return byMethod(matchingMethods.get(0), arguments); }
MethodExecutionStrategy implements ExecutionStrategy { public static MethodExecutionStrategy byMatchingArguments(Class<?> clazz, String methodName, Object... arguments) throws ScriptException { List<Method> matchingMethods = new ArrayList<>(); for (Method method : clazz.getMethods()) { if (method.getName().equals(methodName) && (method.getModifiers() & Modifier.PUBLIC) != 0 && ReflectionUtil.matchesArguments(method, arguments)) { matchingMethods.add(method); } } int count = matchingMethods.size(); if (count == 0) { throw new ScriptException("No method '" + methodName + "' with matching arguments found"); } else if (count > 1) { throw new ScriptException("Ambiguous methods '" + methodName + "' with matching arguments found: " + "\n" + matchingMethods.stream().map(Object::toString).collect(Collectors.joining("\n"))); } return byMethod(matchingMethods.get(0), arguments); } }
MethodExecutionStrategy implements ExecutionStrategy { public static MethodExecutionStrategy byMatchingArguments(Class<?> clazz, String methodName, Object... arguments) throws ScriptException { List<Method> matchingMethods = new ArrayList<>(); for (Method method : clazz.getMethods()) { if (method.getName().equals(methodName) && (method.getModifiers() & Modifier.PUBLIC) != 0 && ReflectionUtil.matchesArguments(method, arguments)) { matchingMethods.add(method); } } int count = matchingMethods.size(); if (count == 0) { throw new ScriptException("No method '" + methodName + "' with matching arguments found"); } else if (count > 1) { throw new ScriptException("Ambiguous methods '" + methodName + "' with matching arguments found: " + "\n" + matchingMethods.stream().map(Object::toString).collect(Collectors.joining("\n"))); } return byMethod(matchingMethods.get(0), arguments); } private  MethodExecutionStrategy(Method method, Object... arguments); }
MethodExecutionStrategy implements ExecutionStrategy { public static MethodExecutionStrategy byMatchingArguments(Class<?> clazz, String methodName, Object... arguments) throws ScriptException { List<Method> matchingMethods = new ArrayList<>(); for (Method method : clazz.getMethods()) { if (method.getName().equals(methodName) && (method.getModifiers() & Modifier.PUBLIC) != 0 && ReflectionUtil.matchesArguments(method, arguments)) { matchingMethods.add(method); } } int count = matchingMethods.size(); if (count == 0) { throw new ScriptException("No method '" + methodName + "' with matching arguments found"); } else if (count > 1) { throw new ScriptException("Ambiguous methods '" + methodName + "' with matching arguments found: " + "\n" + matchingMethods.stream().map(Object::toString).collect(Collectors.joining("\n"))); } return byMethod(matchingMethods.get(0), arguments); } private  MethodExecutionStrategy(Method method, Object... arguments); @Override Object execute(Object instance); static MethodExecutionStrategy byMethod(Method method, Object... arguments); static MethodExecutionStrategy byMainMethod(Class<?> clazz, String... arguments); static MethodExecutionStrategy byArgumentTypes(Class<?> clazz, String methodName, Class<?>[] argumentTypes, Object... arguments); static MethodExecutionStrategy byMatchingArguments(Class<?> clazz, String methodName, Object... arguments); }
MethodExecutionStrategy implements ExecutionStrategy { public static MethodExecutionStrategy byMatchingArguments(Class<?> clazz, String methodName, Object... arguments) throws ScriptException { List<Method> matchingMethods = new ArrayList<>(); for (Method method : clazz.getMethods()) { if (method.getName().equals(methodName) && (method.getModifiers() & Modifier.PUBLIC) != 0 && ReflectionUtil.matchesArguments(method, arguments)) { matchingMethods.add(method); } } int count = matchingMethods.size(); if (count == 0) { throw new ScriptException("No method '" + methodName + "' with matching arguments found"); } else if (count > 1) { throw new ScriptException("Ambiguous methods '" + methodName + "' with matching arguments found: " + "\n" + matchingMethods.stream().map(Object::toString).collect(Collectors.joining("\n"))); } return byMethod(matchingMethods.get(0), arguments); } private  MethodExecutionStrategy(Method method, Object... arguments); @Override Object execute(Object instance); static MethodExecutionStrategy byMethod(Method method, Object... arguments); static MethodExecutionStrategy byMainMethod(Class<?> clazz, String... arguments); static MethodExecutionStrategy byArgumentTypes(Class<?> clazz, String methodName, Class<?>[] argumentTypes, Object... arguments); static MethodExecutionStrategy byMatchingArguments(Class<?> clazz, String methodName, Object... arguments);  }