/*
 * The Apache Software License, Version 1.1
 *
 * Copyright (c) 2002 The Apache Software Foundation.  All rights
 * reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * 3. The end-user documentation included with the redistribution,
 *    if any, must include the following acknowledgment:
 *       "This product includes software developed by the
 *        Apache Software Foundation (http://www.apache.org/)."
 *    Alternately, this acknowledgment may appear in the software itself,
 *    if and wherever such third-party acknowledgments normally appear.
 *
 * 4. The names "Apache" and "Apache Software Foundation" must
 *    not be used to endorse or promote products derived from this
 *    software without prior written permission. For written
 *    permission, please contact apache@apache.org.
 *
 * 5. Products derived from this software may not be called "Apache",
 *    nor may "Apache" appear in their name, without prior written
 *    permission of the Apache Software Foundation.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 * ====================================================================
 *
 * This software consists of voluntary contributions made by many
 * individuals on behalf of the Apache Software Foundation.  For more
 * information on the Apache Software Foundation, please see
 * <http://www.apache.org/>.
 */
package net.sf.cglib;

import java.lang.reflect.*;
import net.sf.cglib.util.*;

/**
 * Classes generated by Enhancer pass this object to the
 * registered MethodInterceptors when an intercepted method is invoked. It can
 * be used to either invoke the original method, or call the same method on a different
 * object of the same type.
 * @see Enhancer
 * @see MethodInterceptor
 * @version $Id: MethodProxy.java,v 1.21 2003/06/24 21:00:09 herbyderby Exp $
 */
abstract public class MethodProxy {
    private static final FactoryCache cache = new FactoryCache(MethodProxy.class);
    private static final Constructor GENERATOR =
      ReflectUtils.findConstructor("MethodProxy$Generator(Method, Method)");
    private static final Method INVOKE_SUPER =
      ReflectUtils.findMethod("MethodProxy.invokeSuper(Object, Object[])");
    private static final Method INVOKE =
      ReflectUtils.findMethod("MethodProxy.invoke(Object, Object[])");

    /**
     * Invoke the original (super) method on the specified object.
     * @param obj the enhanced object, must be the object passed as the first
     * argument to the MethodInterceptor
     * @param args the arguments passed to the intercepted method; you may substitute a different
     * argument array as long as the types are compatible
     * @see MethodInterceptor#intercept
     */
    abstract public Object invokeSuper(Object obj, Object[] args) throws Throwable;

    /**
     * Invoke the original method, on a different object of the same type.
     * @param obj the compatible object; recursion will result if you use the object passed as the first
     * argument to the MethodInterceptor (usually not what you want)
     * @param args the arguments passed to the intercepted method; you may substitute a different
     * argument array as long as the types are compatible
     * @see MethodInterceptor#intercept
     */
    abstract public Object invoke(Object obj, Object[] args) throws Throwable;

    protected MethodProxy() { }

    /**
     * Create a new MethodProxy. Used internally by Enhancer.
     */
    public static MethodProxy create(Method method, Method superMethod) {
        return create(method, superMethod, null);
    }

    /**
     * Create a new MethodProxy. Used internally by Enhancer.
     */
    public static MethodProxy create(Method method, Method superMethod, ClassLoader loader) {
        if (loader == null) {
            loader = superMethod.getDeclaringClass().getClassLoader();
        }
        return (MethodProxy)cache.getFactory(loader, null, GENERATOR, superMethod, method);
    }

    static class Generator extends CodeGenerator {
        private Method method;
        private Method superMethod;
        
        public Generator(Method superMethod, Method method) {
            setSuperclass(MethodProxy.class);
            setNamePrefix(superMethod.getDeclaringClass().getName());
            this.superMethod = superMethod;
            this.method = method;
        }

        public void generate() {
            null_constructor();
            generate(MethodProxy.INVOKE, method);
            generate(MethodProxy.INVOKE_SUPER, superMethod);
        }

        private void generate(Method proxyMethod, Method method) {
            begin_method(proxyMethod);
            if (Modifier.isProtected(method.getModifiers())) {
                throw_exception(IllegalAccessException.class, "protected method: " + method);
            } else {
                load_arg(0);
                checkcast(method.getDeclaringClass());
                Class[] types = method.getParameterTypes();
                for (int i = 0; i < types.length; i++) {
                    load_arg(1);
                    push(i);
                    aaload();
                    unbox(types[i]);
                }
                this.invoke(method);
                box(method.getReturnType());
            }
            return_value();
            end_method();
        }
    }
}

@@ -2975,17 +2975,17 @@
 va,v 1.2
-1
+2
  2003/06
@@ -2995,13 +2995,13 @@
  21:
-00:09
+33:11
  her
@@ -3259,24 +3259,147 @@
  Method)%22);%0A
+    private static final MethodProxyKey KEY_FACTORY =%0A      (MethodProxyKey)KeyFactory.create(MethodProxyKey.class, null);%0A
     private 
@@ -3550,16 +3550,16 @@
 NVOKE =%0A
-
       Re
@@ -3622,16 +3622,104 @@
 %5B%5D)%22);%0A%0A
+    interface MethodProxyKey %7B%0A        Object newInstance(Method m1, Method m2);%0A    %7D%0A%0A
     /**%0A
@@ -5251,32 +5251,99 @@
 er();%0A        %7D%0A
+        Object key = KEY_FACTORY.newInstance(method, superMethod);%0A
         return (
@@ -5379,20 +5379,19 @@
 loader, 
-null
+key
 , GENERA
@@ -5399,22 +5399,22 @@
 OR, 
-superM
+m
 ethod, 
-m
+superM
 etho
@@ -5579,30 +5579,25 @@
 ator(Method 
-superM
+m
 ethod, Metho
@@ -5590,33 +5590,38 @@
  method, Method 
-m
+superM
 ethod) %7B%0A       

Add caching to MethodProxy