package com.yxy.chukonu.java.dynamic.proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class DynamicProxyWithJDK implements InvocationHandler{

	//the real object we want to delegate
    private Object subject;

    public DynamicProxyWithJDK(Object subject)
    {
        this.subject = subject;
    }
    
    @Override
    /**
     * @param object: the proxy instance
     * @param method: the method name of the proxy instance we want to call
     * @param args: the arguments of the calling method
     */
    public Object invoke(Object object, Method method, Object[] args) throws Throwable{
    	 System.out.println("do sth before invoking...");
         
//         System.out.println("invoking Method:" + method);
         Object result = method.invoke(subject, args);
         System.out.println("result: "+result) ;
         System.out.println("do sth after invoking...");
         return result;
    }
    
    static interface Human{
    	public String getName() ;
    }

    static class Man implements Human{
    	public String getName(){
    		return "Man" ;
    	}
    }
    
    static class Woman implements Human{
    	public String getName(){
    		return "Woman" ;
    	}
    }
    
    
    public static void main(String[] args)
    {
        Human man = new Man();
        //inject delegation instance to handler
        InvocationHandler handler = new DynamicProxyWithJDK(man);
        /*
         * 通过Proxy的newProxyInstance方法来创建我们的代理对象，我们来看看其三个参数
         * 第一个参数 handler.getClass().getClassLoader() ，我们这里使用handler这个类的ClassLoader对象来加载我们的代理对象
         * 第二个参数realSubject.getClass().getInterfaces()，我们这里为代理对象提供的接口是真实对象所实行的接口，表示我要代理的是该真实对象，这样我就能调用这组接口中的方法了
         * 第三个参数handler， 我们这里将这个代理对象关联到了上方的 InvocationHandler 这个对象上
         */
        Human proxyInstance = (Human)Proxy.newProxyInstance(handler.getClass().getClassLoader(), man.getClass().getInterfaces(), handler);
        proxyInstance.getName() ;
    }

}

@@ -1477,365 +1477,8 @@
 n);%0A
-        /*%0A         * %E9%80%9A%E8%BF%87Proxy%E7%9A%84newProxyInstance%E6%96%B9%E6%B3%95%E6%9D%A5%E5%88%9B%E5%BB%BA%E6%88%91%E4%BB%AC%E7%9A%84%E4%BB%A3%E7%90%86%E5%AF%B9%E8%B1%A1%EF%BC%8C%E6%88%91%E4%BB%AC%E6%9D%A5%E7%9C%8B%E7%9C%8B%E5%85%B6%E4%B8%89%E4%B8%AA%E5%8F%82%E6%95%B0%0A         * %E7%AC%AC%E4%B8%80%E4%B8%AA%E5%8F%82%E6%95%B0 handler.getClass().getClassLoader() %EF%BC%8C%E6%88%91%E4%BB%AC%E8%BF%99%E9%87%8C%E4%BD%BF%E7%94%A8handler%E8%BF%99%E4%B8%AA%E7%B1%BB%E7%9A%84ClassLoader%E5%AF%B9%E8%B1%A1%E6%9D%A5%E5%8A%A0%E8%BD%BD%E6%88%91%E4%BB%AC%E7%9A%84%E4%BB%A3%E7%90%86%E5%AF%B9%E8%B1%A1%0A         * %E7%AC%AC%E4%BA%8C%E4%B8%AA%E5%8F%82%E6%95%B0realSubject.getClass().getInterfaces()%EF%BC%8C%E6%88%91%E4%BB%AC%E8%BF%99%E9%87%8C%E4%B8%BA%E4%BB%A3%E7%90%86%E5%AF%B9%E8%B1%A1%E6%8F%90%E4%BE%9B%E7%9A%84%E6%8E%A5%E5%8F%A3%E6%98%AF%E7%9C%9F%E5%AE%9E%E5%AF%B9%E8%B1%A1%E6%89%80%E5%AE%9E%E8%A1%8C%E7%9A%84%E6%8E%A5%E5%8F%A3%EF%BC%8C%E8%A1%A8%E7%A4%BA%E6%88%91%E8%A6%81%E4%BB%A3%E7%90%86%E7%9A%84%E6%98%AF%E8%AF%A5%E7%9C%9F%E5%AE%9E%E5%AF%B9%E8%B1%A1%EF%BC%8C%E8%BF%99%E6%A0%B7%E6%88%91%E5%B0%B1%E8%83%BD%E8%B0%83%E7%94%A8%E8%BF%99%E7%BB%84%E6%8E%A5%E5%8F%A3%E4%B8%AD%E7%9A%84%E6%96%B9%E6%B3%95%E4%BA%86%0A         * %E7%AC%AC%E4%B8%89%E4%B8%AA%E5%8F%82%E6%95%B0handler%EF%BC%8C %E6%88%91%E4%BB%AC%E8%BF%99%E9%87%8C%E5%B0%86%E8%BF%99%E4%B8%AA%E4%BB%A3%E7%90%86%E5%AF%B9%E8%B1%A1%E5%85%B3%E8%81%94%E5%88%B0%E4%BA%86%E4%B8%8A%E6%96%B9%E7%9A%84 InvocationHandler %E8%BF%99%E4%B8%AA%E5%AF%B9%E8%B1%A1%E4%B8%8A%0A         */%0A
     

update dynmaic proxy example