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