/*
 * 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.Method;
import java.lang.reflect.Modifier;

/**
 * @version $Id: MethodProxy.java,v 1.13 2003/02/01 19:44:50 baliuka Exp $
 */
abstract public class MethodProxy {
    
    private static final Method INVOKE_SUPER =
      ReflectUtils.findMethod("MethodProxy.invokeSuper(Object, Object[])");
        private static final Method INVOKE =
      ReflectUtils.findMethod("MethodProxy.invoke(Object, Object[])");


    private static final ClassNameFactory NAME_FACTORY =
      new ClassNameFactory("ProxiedByCGLIB");

    private static final ClassLoader DEFAULT_LOADER =
      MethodProxy.class.getClassLoader();

    abstract public Object invokeSuper(Object obj, Object[] args) throws Throwable;
    
    abstract public Object invoke(Object obj, Object[] args) throws Throwable;

    protected MethodProxy() { }

    public static MethodProxy create( Method method, Method superMethod  ) {
        try {
            Class declaring = method.getDeclaringClass();
            String className = NAME_FACTORY.getNextName(declaring);
            ClassLoader loader = declaring.getClassLoader();
            if (loader == null) {
                loader = DEFAULT_LOADER;
            }
            Class gen = new Generator(className, superMethod, method , loader).define();
            return (MethodProxy)gen.getConstructor(Constants.TYPES_EMPTY).newInstance(null);
        } catch (RuntimeException e) {
            throw e;
        } catch (Exception e) {
            throw new CodeGenerationException(e);
        }
    }

    private static class Generator extends CodeGenerator {
        private Method method;
        private Method superMethod;
        
        public Generator(String className, Method superMethod, Method method, ClassLoader loader) {
            super(className, MethodProxy.class, loader);
            this.method = method;
            this.superMethod = superMethod;
        }

       protected void generate() {
         generateNullConstructor();  
         generate(MethodProxy.INVOKE,method);
         generate(MethodProxy.INVOKE_SUPER,superMethod);
       }
       protected void generate(Method proxyMethod, Method method) {
            
            begin_method(proxyMethod);
            if( !Modifier.isPublic( method.getModifiers() ) && 
                MethodProxy.INVOKE == proxyMethod){
              throw_exception(IllegalAccessException.class, "not public 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();
        }
    }
}

@@ -2692,17 +2692,17 @@
 va,v 1.1
-3
+4
  2003/02
@@ -2707,26 +2707,29 @@
 02/0
-1 19:44:50 baliuka
+2 00:57:58 herbyderby
  Exp
@@ -3457,17 +3457,147 @@
  create(
- 
+Method method, Method superMethod) %7B%0A        return create(method, superMethod, null);%0A    %7D%0A%0A    public static MethodProxy create(
 Method m
@@ -3621,18 +3621,36 @@
 erMethod
-  
+, ClassLoader loader
 ) %7B%0A    
@@ -3689,17 +3689,22 @@
 aring = 
-m
+superM
 ethod.ge
@@ -3802,27 +3802,53 @@
         
-ClassLoader
+if (loader == null) %7B%0A               
  loader 
@@ -3881,32 +3881,36 @@
 ();%0A            
+    
 if (loader == nu
@@ -3923,32 +3923,36 @@
                 
+  
+  
 loader = DEFAULT
@@ -3960,16 +3960,34 @@
 LOADER;%0A
+                %7D%0A
         

Fix classloader issue.