001/* 002 * Copyright (C) 2012 The Guava Authors 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package com.google.common.reflect; 018 019import com.google.common.annotations.Beta; 020 021import java.lang.reflect.InvocationHandler; 022import java.lang.reflect.Method; 023import java.lang.reflect.Proxy; 024import java.util.Arrays; 025 026import javax.annotation.Nullable; 027 028/** 029 * Abstract implementation of {@link InvocationHandler} that handles {@link Object#equals}, 030 * {@link Object#hashCode} and {@link Object#toString}. For example: <pre> 031 * class Unsupported extends AbstractInvocationHandler { 032 * protected Object handleInvocation( 033 * Object proxy, Method method, Object[] args) { 034 * throw new UnsupportedOperationException(); 035 * } 036 * } 037 * 038 * CharSequence unsupported = Reflection.newProxy(CharSequence.class, new Unsupported()); 039 * </pre> 040 * 041 * @author Ben Yu 042 * @since 12.0 043 */ 044@Beta 045public abstract class AbstractInvocationHandler implements InvocationHandler { 046 047 private static final Object[] NO_ARGS = {}; 048 049 /** 050 * {@inheritDoc} 051 * 052 * <p><ul> 053 * <li>{@code proxy.hashCode()} delegates to {@link AbstractInvocationHandler#hashCode} 054 * <li>{@code proxy.toString()} delegates to {@link AbstractInvocationHandler#toString} 055 * <li>{@code proxy.equals(argument)} returns true if: <ul> 056 * <li>{@code proxy} and {@code argument} are of the same type 057 * <li>and {@link AbstractInvocationHandler#equals} returns true for the {@link 058 * InvocationHandler} of {@code argument} 059 * </ul> 060 * <li>other method calls are dispatched to {@link #handleInvocation}. 061 * </ul> 062 */ 063 @Override public final Object invoke(Object proxy, Method method, @Nullable Object[] args) 064 throws Throwable { 065 if (args == null) { 066 args = NO_ARGS; 067 } 068 if (args.length == 0 && method.getName().equals("hashCode")) { 069 return hashCode(); 070 } 071 if (args.length == 1 072 && method.getName().equals("equals") 073 && method.getParameterTypes()[0] == Object.class) { 074 Object arg = args[0]; 075 if (arg == null) { 076 return false; 077 } 078 if (proxy == arg) { 079 return true; 080 } 081 return isProxyOfSameInterfaces(arg, proxy.getClass()) 082 && equals(Proxy.getInvocationHandler(arg)); 083 } 084 if (args.length == 0 && method.getName().equals("toString")) { 085 return toString(); 086 } 087 return handleInvocation(proxy, method, args); 088 } 089 090 /** 091 * {@link #invoke} delegates to this method upon any method invocation on the proxy instance, 092 * except {@link Object#equals}, {@link Object#hashCode} and {@link Object#toString}. The result 093 * will be returned as the proxied method's return value. 094 * 095 * <p>Unlike {@link #invoke}, {@code args} will never be null. When the method has no parameter, 096 * an empty array is passed in. 097 */ 098 protected abstract Object handleInvocation(Object proxy, Method method, Object[] args) 099 throws Throwable; 100 101 /** 102 * By default delegates to {@link Object#equals} so instances are only equal if they are 103 * identical. {@code proxy.equals(argument)} returns true if: <ul> 104 * <li>{@code proxy} and {@code argument} are of the same type 105 * <li>and this method returns true for the {@link InvocationHandler} of {@code argument} 106 * </ul> 107 * <p>Subclasses can override this method to provide custom equality. 108 */ 109 @Override public boolean equals(Object obj) { 110 return super.equals(obj); 111 } 112 113 /** 114 * By default delegates to {@link Object#hashCode}. The dynamic proxies' {@code hashCode()} will 115 * delegate to this method. Subclasses can override this method to provide custom equality. 116 */ 117 @Override public int hashCode() { 118 return super.hashCode(); 119 } 120 121 /** 122 * By default delegates to {@link Object#toString}. The dynamic proxies' {@code toString()} will 123 * delegate to this method. Subclasses can override this method to provide custom string 124 * representation for the proxies. 125 */ 126 @Override public String toString() { 127 return super.toString(); 128 } 129 130 private static boolean isProxyOfSameInterfaces(Object arg, Class<?> proxyClass) { 131 return proxyClass.isInstance(arg) 132 // Equal proxy instances should mostly be instance of proxyClass 133 // Under some edge cases (such as the proxy of JDK types serialized and then deserialized) 134 // the proxy type may not be the same. 135 // We first check isProxyClass() so that the common case of comparing with non-proxy objects 136 // is efficient. 137 || (Proxy.isProxyClass(arg.getClass()) 138 && Arrays.equals(arg.getClass().getInterfaces(), proxyClass.getInterfaces())); 139 } 140}