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;
023
024import javax.annotation.Nullable;
025
026/**
027 * Abstract implementation of {@link InvocationHandler} that handles {@link Object#equals},
028 * {@link Object#hashCode} and {@link Object#toString}.
029 *
030 * @author Ben Yu
031 * @since 12.0
032 */
033@Beta
034public abstract class AbstractInvocationHandler implements InvocationHandler {
035
036  private static final Object[] NO_ARGS = {};
037
038  /**
039   * {@inheritDoc}
040   * 
041   * <p>{@link Object#equals}, {@link Object#hashCode} are implemented according to referential
042   * equality (the default behavior of {@link Object}). {@link Object#toString} delegates to
043   * {@link #toString} that can be overridden by subclasses.
044   */
045  @Override public final Object invoke(Object proxy, Method method, @Nullable Object[] args)
046      throws Throwable {
047    if (args == null) {
048      args = NO_ARGS;
049    }
050    if (args.length == 0 && method.getName().equals("hashCode")) {
051      return System.identityHashCode(proxy);
052    }
053    if (args.length == 1
054        && method.getName().equals("equals")
055        && method.getParameterTypes()[0] == Object.class) {
056      return proxy == args[0];
057    }
058    if (args.length == 0 && method.getName().equals("toString")) {
059      return toString();
060    }
061    return handleInvocation(proxy, method, args);
062  }
063
064  /**
065   * {@link #invoke} delegates to this method upon any method invocation on the proxy instance,
066   * except {@link Object#equals}, {@link Object#hashCode} and {@link Object#toString}. The result
067   * will be returned as the proxied method's return value.
068   * 
069   * <p>Unlike {@link #invoke}, {@code args} will never be null. When the method has no parameter,
070   * an empty array is passed in.
071   */
072  protected abstract Object handleInvocation(Object proxy, Method method, Object[] args)
073      throws Throwable;
074
075  /**
076   * The dynamic proxies' {@link Object#toString} will delegate to this method. Subclasses can
077   * override this to provide custom string representation of the proxies.
078   */
079  @Override public String toString() {
080    return super.toString();
081  }
082}