• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2017 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 org.mockito.internal.creation.DelegatingMethod;
8 import org.mockito.internal.invocation.mockref.MockWeakReference;
9 import org.mockito.internal.debugging.LocationImpl;
10 import org.mockito.internal.progress.SequenceNumber;
11 import org.mockito.invocation.Invocation;
12 import org.mockito.invocation.InvocationFactory;
13 import org.mockito.invocation.Location;
14 import org.mockito.mock.MockCreationSettings;
15 
16 import java.lang.reflect.Method;
17 import java.util.concurrent.Callable;
18 
19 public class DefaultInvocationFactory implements InvocationFactory {
20 
createInvocation(Object target, MockCreationSettings settings, Method method, final Callable realMethod, Object... args)21     public Invocation createInvocation(Object target, MockCreationSettings settings, Method method, final Callable realMethod, Object... args) {
22         RealMethod superMethod = new RealMethod.FromCallable(realMethod);
23         return createInvocation(target, settings, method, superMethod, args);
24     }
25 
createInvocation(Object target, MockCreationSettings settings, Method method, RealMethodBehavior realMethod, Object... args)26     public Invocation createInvocation(Object target, MockCreationSettings settings, Method method, RealMethodBehavior realMethod, Object... args) {
27         RealMethod superMethod = new RealMethod.FromBehavior(realMethod);
28         return createInvocation(target, settings, method, superMethod, args);
29     }
30 
createInvocation(Object target, MockCreationSettings settings, Method method, RealMethod superMethod, Object[] args)31     private Invocation createInvocation(Object target, MockCreationSettings settings, Method method, RealMethod superMethod, Object[] args) {
32         return createInvocation(target, method, args, superMethod, settings);
33     }
34 
createInvocation(Object mock, Method invokedMethod, Object[] arguments, RealMethod realMethod, MockCreationSettings settings, Location location)35     public static InterceptedInvocation createInvocation(Object mock, Method invokedMethod, Object[] arguments, RealMethod realMethod, MockCreationSettings settings, Location location) {
36         return new InterceptedInvocation(
37             new MockWeakReference<Object>(mock),
38             createMockitoMethod(invokedMethod, settings),
39             arguments,
40             realMethod,
41             location,
42             SequenceNumber.next()
43         );
44     }
45 
createInvocation(Object mock, Method invokedMethod, Object[] arguments, RealMethod realMethod, MockCreationSettings settings)46     private static InterceptedInvocation createInvocation(Object mock, Method invokedMethod, Object[]
47         arguments, RealMethod realMethod, MockCreationSettings settings) {
48         return createInvocation(mock, invokedMethod, arguments, realMethod, settings, new LocationImpl());
49     }
50 
createMockitoMethod(Method method, MockCreationSettings settings)51     private static MockitoMethod createMockitoMethod(Method method, MockCreationSettings settings) {
52         if (settings.isSerializable()) {
53             return new SerializableMethod(method);
54         } else {
55             return new DelegatingMethod(method);
56         }
57     }
58 }
59