InvocationHandlerAdapter.java revision 744d6f6da5766da5c9ed5d6732604d5e2004771e
1/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.google.dexmaker.mockito;
18
19import com.google.dexmaker.stock.ProxyBuilder;
20import java.lang.reflect.InvocationHandler;
21import java.lang.reflect.Method;
22import org.mockito.internal.invocation.InvocationImpl;
23import org.mockito.internal.invocation.MockitoMethod;
24import org.mockito.internal.invocation.realmethod.RealMethod;
25import org.mockito.internal.progress.SequenceNumber;
26import org.mockito.internal.util.ObjectMethodsGuru;
27import org.mockito.invocation.MockHandler;
28
29/**
30 * Handles proxy method invocations to dexmaker's InvocationHandler by calling
31 * a MockitoInvocationHandler.
32 */
33final class InvocationHandlerAdapter implements InvocationHandler {
34    private MockHandler handler;
35    private final ObjectMethodsGuru objectMethodsGuru = new ObjectMethodsGuru();
36
37    public InvocationHandlerAdapter(MockHandler handler) {
38        this.handler = handler;
39    }
40
41    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
42        if (objectMethodsGuru.isEqualsMethod(method)) {
43            return proxy == args[0];
44        } else if (objectMethodsGuru.isHashCodeMethod(method)) {
45            return System.identityHashCode(proxy);
46        }
47
48        ProxiedMethod proxiedMethod = new ProxiedMethod(method);
49        return handler.handle(new InvocationImpl(proxy, proxiedMethod, args, SequenceNumber.next(),
50                proxiedMethod));
51    }
52
53    public MockHandler getHandler() {
54        return handler;
55    }
56
57    public void setHandler(MockHandler handler) {
58        this.handler = handler;
59    }
60
61    private static class ProxiedMethod implements MockitoMethod, RealMethod {
62        private final Method method;
63
64        public ProxiedMethod(Method method) {
65            this.method = method;
66        }
67
68        public String getName() {
69            return method.getName();
70        }
71
72        public Class<?> getReturnType() {
73            return method.getReturnType();
74        }
75
76        public Class<?>[] getParameterTypes() {
77            return method.getParameterTypes();
78        }
79
80        public Class<?>[] getExceptionTypes() {
81            return method.getExceptionTypes();
82        }
83
84        public boolean isVarArgs() {
85            return method.isVarArgs();
86        }
87
88        public Method getJavaMethod() {
89            return method;
90        }
91
92        public Object invoke(Object target, Object[] arguments) throws Throwable {
93            return ProxyBuilder.callSuper(target, method, arguments);
94        }
95    }
96}
97