/*
 * 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.proxy;

import java.lang.reflect.*;
import java.util.Arrays;
import java.util.List;
import net.sf.cglib.core.CodeGenerationException;
import net.sf.cglib.reflect.*;

/**
 * Classes generated by {@link Enhancer} pass this object to the
 * registered {@link MethodInterceptor} objects 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.
 * @version $Id: MethodProxy.java,v 1.3 2003/11/08 19:18:44 herbyderby Exp $
 */
public class MethodProxy {
    private static final Class[] METHOD_ARRAY = { Method.class };

    private FastClass f1;
    private FastClass f2;
    private int i1;
    private int i2;

    /**
     * For internal use by {@link Enhancer} only; see the {@link net.sf.cglib.reflect.FastMethod} class
     * for similar functionality.
     */
    public static MethodProxy create(ClassLoader loader, Class c1, final String m1, Class c2, String m2) {
        FastClass f1 = helper(loader, c1);
        FastClass f2 = helper(loader, c2);
        int i1 = f1.getIndex(m1);
        int i2 = f2.getIndex(m2);
        if (i1 < 0) {
            return new MethodProxy(f1, i1, f2, i2) {
                public Object invoke(Object obj, Object[] args) throws Throwable {
                    throw new IllegalArgumentException("Protected method: " + m1);
                }
            };
        } else {
            return new MethodProxy(f1, i1, f2, i2);
        }
    }

    private MethodProxy(FastClass f1, int i1, FastClass f2, int i2) {
        this.f1 = f1;
        this.i1 = i1;
        this.f2 = f2;
        this.i2 = i2;
    }

    /**
     * Return the {@link net.sf.cglib.reflect.FastClass} method index
     * for the method used by {@link #invokeSuper}. This index uniquely
     * identifies the method within the generated proxy, and therefore
     * can be useful to reference external metadata.
     * @see #getSuperName
     */
    public int getSuperIndex() {
        return i2;
    }

    /**
     * Returns the name of the synthetic method created by CGLIB which is
     * used by {@link #invokeSuper} to invoke the superclass
     * (non-intercepted) method implementation. The parameter types are
     * guaranteed to be the same.
     * @param type the class generated by Enhancer
     * @param method the original method; only the name and parameter types are used.
     * @return the name of the synthetic proxy method, or null if no matching method can be found
     */
    public static String getSuperName(Class type, Method method) {
        List params = Arrays.asList(method.getParameterTypes());
        Method[] methods = type.getDeclaredMethods();
        for (int i = 0; i < methods.length; i++) {
            String name = methods[i].getName();
            if (name.startsWith(MethodInterceptorGenerator.ACCESS_PREFIX) &&
                Arrays.asList(methods[i].getParameterTypes()).equals(params)) {
                return name;
            }
        }
        return null;
    }
    
    private static FastClass helper(ClassLoader loader, Class type) {
        FastClass.Generator g = new FastClass.Generator();
        g.setType(type);
        g.setClassLoader(loader);
        return g.create();
    }

    /**
     * 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
     */
    public Object invoke(Object obj, Object[] args) throws Throwable {
        try {
            return f1.invoke(i1, obj, args);
        } catch (InvocationTargetException e) {
            throw e.getTargetException();
        }
    }

    /**
     * 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
     */
    public Object invokeSuper(Object obj, Object[] args) throws Throwable {
        try {
            return f2.invoke(i2, obj, args);
        } catch (InvocationTargetException e) {
            throw e.getTargetException();
        }
    }
}

@@ -3061,17 +3061,17 @@
 ava,v 1.
-3
+4
  2003/11
@@ -3078,16 +3078,16 @@
 /08 
-19:18:44
+20:08:05
  her
@@ -5452,16 +5452,149 @@
 FIX) &&%0A
+                name.substring(MethodInterceptorGenerator.ACCESS_PREFIX.length(), name.lastIndexOf('_')).equals(method.getName()) &&%0A
         

fix getSuperName