001/*
002 * (c) 2003-2005, 2009, 2010 ThoughtWorks Ltd
003 * All rights reserved.
004 *
005 * The software in this package is published under the terms of the BSD
006 * style license a copy of which has been included with this distribution in
007 * the LICENSE.txt file.
008 * 
009 * Created on 10-May-2004
010 */
011package com.thoughtworks.proxy.toys.decorate;
012
013import java.io.Serializable;
014import java.lang.reflect.Method;
015
016/**
017 * Identity implementation for a Decorator. The implementation will just pass through any values. Override all methods
018 * that should behave differently.
019 *
020 * @author Dan North
021 * @since 1.0
022 */
023public abstract class Decorator<T> implements Serializable {
024    private static final long serialVersionUID = 1L;
025
026    /**
027     * Called before a method is invoked on an object, to possibly decorate the arguments being passed to the method
028     * invocation.
029     *
030     * @param proxy  the proxy the method will be invoked on
031     * @param method the method to be invoked
032     * @param args   the arguments being passed to the method
033     * @return the decorated arguments (typically just the ones supplied)
034     * @since 1.0
035     */
036    public Object[] beforeMethodStarts(final T proxy, final Method method, final Object[] args) {
037        return args;
038    }
039
040    /**
041     * Called on the way back from a method invocation, to possibly decorate the result.
042     *
043     * @param proxy  the proxy the method was be invoked on
044     * @param method the invoked method
045     * @param args   the arguments passed to the method
046     * @param result the result of the method invocation
047     * @return the decorated result (typically just the supplied result)
048     * @since 1.0
049     */
050    public Object decorateResult(final T proxy, final Method method, final Object[] args, final Object result) {
051        return result;
052    }
053
054    /**
055     * Called when a called method fails, to possibly decorate the type of error.
056     *
057     * @param proxy  the proxy the method was be invoked on
058     * @param method the invoked method
059     * @param args   the arguments passed to the method
060     * @param cause  the original exception thrown
061     * @return the decorated exception (typically just the supplied cause)
062     * @since 1.0
063     */
064    public Throwable decorateTargetException(
065            final T proxy, final Method method, final Object[] args, final Throwable cause) {
066        return cause;
067    }
068
069    /**
070     * Called when a method cannot be invoked, to possibly decorate the type of error.
071     *
072     * @param proxy  the proxy the method was be invoked on
073     * @param method the invoked method
074     * @param args   the arguments passed to the method
075     * @param cause  the original exception thrown
076     * @return the decorated exception (typically just the supplied cause)
077     * @since 1.0
078     */
079    public Exception decorateInvocationException(
080            final T proxy, final Method method, final Object[] args, final Exception cause) {
081        return cause;
082    }
083}