1/*
2 * Copyright (c) 2007 Mockito contributors
3 * This program is made available under the terms of the MIT License.
4 */
5
6package org.mockito.internal.invocation;
7
8import org.hamcrest.Matcher;
9import org.mockito.exceptions.Reporter;
10import org.mockito.internal.matchers.LocalizedMatcher;
11import org.mockito.internal.progress.ArgumentMatcherStorage;
12import org.mockito.invocation.Invocation;
13
14import java.io.Serializable;
15import java.util.List;
16
17@SuppressWarnings("unchecked")
18public class MatchersBinder implements Serializable {
19
20    private static final long serialVersionUID = -311433939339443463L;
21
22    public InvocationMatcher bindMatchers(ArgumentMatcherStorage argumentMatcherStorage, Invocation invocation) {
23        List<LocalizedMatcher> lastMatchers = argumentMatcherStorage.pullLocalizedMatchers();
24        validateMatchers(invocation, lastMatchers);
25
26        InvocationMatcher invocationWithMatchers = new InvocationMatcher(invocation, (List<Matcher>)(List) lastMatchers);
27        return invocationWithMatchers;
28    }
29
30    private void validateMatchers(Invocation invocation, List<LocalizedMatcher> lastMatchers) {
31        if (!lastMatchers.isEmpty()) {
32            int recordedMatchersSize = lastMatchers.size();
33            int expectedMatchersSize = invocation.getArguments().length;
34            if (expectedMatchersSize != recordedMatchersSize) {
35                new Reporter().invalidUseOfMatchers(expectedMatchersSize, lastMatchers);
36            }
37        }
38    }
39}