1
2
3
4
5
6 package mockit.internal.reflection;
7
8 import edu.umd.cs.findbugs.annotations.NonNull;
9 import edu.umd.cs.findbugs.annotations.Nullable;
10
11 import java.lang.annotation.Annotation;
12 import java.lang.reflect.Constructor;
13 import java.lang.reflect.InvocationHandler;
14 import java.lang.reflect.Method;
15
16 import mockit.internal.util.DefaultValues;
17 import mockit.internal.util.ObjectMethods;
18
19
20
21
22
23
24
25
26
27
28
29
30
31 public final class MockInvocationHandler implements InvocationHandler {
32 public static final InvocationHandler INSTANCE = new MockInvocationHandler();
33 private static final Class<?>[] CONSTRUCTOR_PARAMETERS_FOR_PROXY_CLASS = { InvocationHandler.class };
34
35 @NonNull
36 public static Object newMockedInstance(@NonNull Class<?> proxyClass) {
37 Constructor<?> publicConstructor;
38 try {
39 publicConstructor = proxyClass.getConstructor(CONSTRUCTOR_PARAMETERS_FOR_PROXY_CLASS);
40 } catch (NoSuchMethodException e) {
41 throw new RuntimeException(e);
42 }
43
44 return ConstructorReflection.invokeAccessible(publicConstructor, INSTANCE);
45 }
46
47 @Nullable
48 @Override
49 public Object invoke(@NonNull Object proxy, @NonNull Method method, @Nullable Object[] args) {
50 Class<?> declaringClass = method.getDeclaringClass();
51 String methodName = method.getName();
52
53 if (declaringClass == Object.class) {
54 if ("equals".equals(methodName)) {
55 return args != null && args.length > 0 && proxy == args[0];
56 }
57 if ("hashCode".equals(methodName)) {
58 return System.identityHashCode(proxy);
59 }
60
61 return ObjectMethods.objectIdentity(proxy);
62 }
63
64 if (declaringClass == Annotation.class) {
65 return proxy.getClass().getInterfaces()[0];
66 }
67
68 Class<?> retType = method.getReturnType();
69 return DefaultValues.computeForType(retType);
70 }
71 }