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;
024
025import javax.annotation.Nullable;
026
027/**
028 * Abstract implementation of {@link InvocationHandler} that handles {@link Object#equals},
029 * {@link Object#hashCode} and {@link Object#toString}.
030 *
031 * @author Ben Yu
032 * @since 12.0
033 */
034@Beta
035public abstract class AbstractInvocationHandler implements InvocationHandler {
036
037  private static final Object[] NO_ARGS = {};
038
039  /**
040   * {@inheritDoc}
041   *
042   * <p><ul>
043   * <li>{@code proxy.hashCode()} delegates to {@link AbstractInvocationHandler#hashCode}
044   * <li>{@code proxy.toString()} delegates to {@link AbstractInvocationHandler#toString}
045   * <li>{@code proxy.equals(argument)} returns true if: <ul>
046   *   <li>{@code proxy} and {@code argument} are of the same type
047   *   <li>and {@link AbstractInvocationHandler#equals} returns true for the {@link
048   *       InvocationHandler} of {@code argument}
049   *   </ul>
050   * <li>other method calls are dispatched to {@link #handleInvocation}.
051   * </ul>
052   */
053  @Override public final Object invoke(Object proxy, Method method, @Nullable Object[] args)
054      throws Throwable {
055    if (args == null) {
056      args = NO_ARGS;
057    }
058    if (args.length == 0 && method.getName().equals("hashCode")) {
059      return hashCode();
060    }
061    if (args.length == 1
062        && method.getName().equals("equals")
063        && method.getParameterTypes()[0] == Object.class) {
064      Object arg = args[0];
065      return proxy.getClass().isInstance(arg) && equals(Proxy.getInvocationHandler(arg));
066    }
067    if (args.length == 0 && method.getName().equals("toString")) {
068      return toString();
069    }
070    return handleInvocation(proxy, method, args);
071  }
072
073  /**
074   * {@link #invoke} delegates to this method upon any method invocation on the proxy instance,
075   * except {@link Object#equals}, {@link Object#hashCode} and {@link Object#toString}. The result
076   * will be returned as the proxied method's return value.
077   * 
078   * <p>Unlike {@link #invoke}, {@code args} will never be null. When the method has no parameter,
079   * an empty array is passed in.
080   */
081  protected abstract Object handleInvocation(Object proxy, Method method, Object[] args)
082      throws Throwable;
083
084  /**
085   * By default delegates to {@link Object#equals} so instances are only equal if they are
086   * identical. {@code proxy.equals(argument)} returns true if: <ul>
087   * <li>{@code proxy} and {@code argument} are of the same type
088   * <li>and this method returns true for the {@link InvocationHandler} of {@code argument}
089   * </ul>
090   * Subclasses can override this method to provide custom equality.
091   */
092  @Override public boolean equals(Object obj) {
093    return super.equals(obj);
094  }
095
096  /**
097   * By default delegates to {@link Object#hashCode}. The dynamic proxies' {@code hashCode()} will
098   * delegate to this method. Subclasses can override this method to provide custom equality.
099   */
100  @Override public int hashCode() {
101    return super.hashCode();
102  }
103
104  /**
105   * By default delegates to {@link Object#toString}. The dynamic proxies' {@code toString()} will
106   * delegate to this method. Subclasses can override this method to provide custom string
107   * representation for the proxies.
108   */
109  @Override public String toString() {
110    return super.toString();
111  }
112}