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