InvocationHandlerAdapter.java revision d0cddaf12d3ec4842cd81232ac3c718730f02b20
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 if (args == null) { 49 throw new IllegalArgumentException(); 50 } 51 52 53 ProxiedMethod proxiedMethod = new ProxiedMethod(method); 54 return handler.handle(new InvocationImpl(proxy, proxiedMethod, args, SequenceNumber.next(), 55 proxiedMethod)); 56 } 57 58 public MockHandler getHandler() { 59 return handler; 60 } 61 62 public void setHandler(MockHandler handler) { 63 this.handler = handler; 64 } 65 66 private static class ProxiedMethod implements MockitoMethod, RealMethod { 67 private final Method method; 68 69 public ProxiedMethod(Method method) { 70 this.method = method; 71 } 72 73 public String getName() { 74 return method.getName(); 75 } 76 77 public Class<?> getReturnType() { 78 return method.getReturnType(); 79 } 80 81 public Class<?>[] getParameterTypes() { 82 return method.getParameterTypes(); 83 } 84 85 public Class<?>[] getExceptionTypes() { 86 return method.getExceptionTypes(); 87 } 88 89 public boolean isVarArgs() { 90 return method.isVarArgs(); 91 } 92 93 public Method getJavaMethod() { 94 return method; 95 } 96 97 public Object invoke(Object target, Object[] arguments) throws Throwable { 98 return ProxyBuilder.callSuper(target, method, arguments); 99 } 100 } 101} 102