<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    Swing


    天行健 君子以自強不息

    posts - 69, comments - 215, trackbacks - 0, articles - 16
       :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

    Reflect&Proxy

    Posted on 2008-04-03 10:57 zht 閱讀(369) 評論(0)  編輯  收藏 所屬分類: J2SE
    Reflect&Proxy

    Reflect&Proxy are two functions provided by java,
    Following is some example about how to use it.

    1.Reflect
    By reflect, the Class instance can be created, not using 'new' method
    as following:
    (1)Creating the class instance:
     Class clazz = Class.forName("twaver.Node");//Node.class;
     Constructor cs = clazz.getConstructor(new Class[] { Object.class });
     Object object = cs.newInstance(new Object[] { "ID-679" });
     @param parameterTypes the parameter array
     new Class[] { Object.class }
     means that the process will use the construcor
     which has one parameter to create the class instance.
     cs.newInstance method will create the class instance with the parameter "new Object[] { "ID-679" }"
    (2)Getting the class method:
     Method getIDMethod = clazz.getMethod("getID", new Class[] {});
     Method setNameMethod = clazz.getMethod("setName", new Class[] { String.class });
     @param name the name of the method
     @param parameterTypes the list of parameters
     setNameMethod.invoke(object, new Object[] { "todd.zhang" });
     Invokes the setNameMthod of object instance with the parameter  new Object[] { "todd.zhang" }


     
    public class ReflectTest {
        
    public static void main(String[] args) throws Exception {
            Node node 
    = new Node("ID-679");
            node.setName(
    "todd.zhang");
            System.out.println(node.getID());
            System.out.println(node.getName());

            System.out.println(
    "-----------------------");

            Class clazz 
    = Class.forName("twaver.Node");//Node.class;

            Constructor cs 
    = clazz.getConstructor(new Class[] { Object.class });
            Method getIDMethod 
    = clazz.getMethod("getID"new Class[] {});
            Method setNameMethod 
    = clazz.getMethod("setName"new Class[] { String.class });
            Method getNameMethod 
    = clazz.getMethod("getName"new Class[] {});

            Object object 
    = cs.newInstance(new Object[] { "ID-679" });
            setNameMethod.invoke(object, 
    new Object[] { "todd.zhang" });
            System.out.println(getIDMethod.invoke(object, 
    new Object[] {}));
            System.out.println(getNameMethod.invoke(object, 
    new Object[] {}));
        }
    }


    2.Proxy
    Proxy is an application of reflect function.
     ClassLoader classLoader = ProxyAnything.class.getClassLoader();
     Class[] interfaces = new Class[] { Interface_A.class, Interface_B.class };
     //the interface array that the proxy will realize
     InvocationHandler handler = new ProxyAnything();
     //the handler of the proxy , each invoked method of Interface_A and Interface_B will be hold up by handler
     //the method invoke will turn to the handler first, and hanlder will deside how to deal with the invoke.
     Proxy proxy = (Proxy) Proxy.newProxyInstance(classLoader, interfaces, handler);
     //it is like the implement of interface ,so it can be transform to interface compulsively.
     proxy instanceof Interface_A will be true
     
     Interface_A a = (Interface_A) proxy;
     Interface_B b = (Interface_B) proxy;
     a.do_A1();
     b.do_B2();

    InvocationHandler may proxy sereval class,
    public Object invoke(Object proxy, Method m, Object[] args) throws Throwable
    the proxy can be used to judge which it is.
    the proxy just is a  rind, each operation will be deal with hanlder


    interface Interface_A {
        
    public void do_A1();
        
    public void do_A2();
        
    public void do_A3();
    }

    interface Interface_B {
        
    public void do_B1();
        
    public void do_B2();
        
    public void do_B3();
    }

    public class ProxyAnything implements InvocationHandler {

        
    private Interface_A businessA;
        
    private Interface_B businessB;

        
    public ProxyAnything() {
            
    this.businessA = new Interface_A() {
                
    public void do_A1() {
                    System.out.println(
    "doing A1");
                }

                
    public void do_A2() {
                    System.out.println(
    "doing A2");
                }

                
    public void do_A3() {
                    System.out.println(
    "doing A3");
                }
            };
            
    this.businessB = new Interface_B() {
                
    public void do_B1() {
                    System.out.println(
    "doing B1");
                }

                
    public void do_B2() {
                    System.out.println(
    "doing B2");
                }

                
    public void do_B3() {
                    System.out.println(
    "doing B3");
                }
            };
        }

        
    public Object invoke(Object proxy, Method m, Object[] args) throws Throwable {
            
    if (m.getDeclaringClass() == Interface_A.class) {
                
    if (m.getName().equals("do_A3")) {
                    System.out.println(
    "you can not invoke do_A3");
                } 
    else {
                    
    return m.invoke(this.businessA, args);
                }
            }
            
    if (m.getDeclaringClass() == Interface_B.class) {
                System.out.println(m.getName() 
    + " is called.");
                
    return m.invoke(this.businessB, args);
            }

            
    return null;
        }

        
    public static void main(String[] args) throws Exception {
            ClassLoader classLoader 
    = ProxyAnything.class.getClassLoader();
            Class[] interfaces 
    = new Class[] { Interface_A.class, Interface_B.class };
            InvocationHandler handler 
    = new ProxyAnything();
            Proxy proxy 
    = (Proxy) Proxy.newProxyInstance(classLoader, interfaces, handler);

            
    if (proxy instanceof Interface_A) {
                System.out.println(
    "proxy instanceof Interface_A");
            }
            
    if (proxy instanceof Interface_B) {
                System.out.println(
    "proxy instanceof Interface_B");
            }

            Interface_A a 
    = (Interface_A) proxy;
            Interface_B b 
    = (Interface_B) proxy;

            a.do_A1();
            a.do_A2();
            a.do_A3();
            b.do_B1();
            b.do_B2();
            b.do_B3();

        }

    }
    主站蜘蛛池模板: 1000部免费啪啪十八未年禁止观看| 麻豆91免费视频| 在线观看国产区亚洲一区成人| 亚洲午夜福利精品无码| 亚洲色图综合在线| 偷自拍亚洲视频在线观看99| 国产精品永久免费视频| 亚洲av成人一区二区三区观看在线| 老牛精品亚洲成av人片| 久草福利资源网站免费| 国产一卡2卡3卡4卡无卡免费视频 国产一卡二卡3卡四卡免费 | 免费国产真实迷j在线观看| 亚洲AV伊人久久青青草原| 亚洲人成在线播放网站岛国| 亚洲三级视频在线| 污网站在线观看免费| 久久国产精品一区免费下载| 久久久久久亚洲精品| 男人的天堂av亚洲一区2区| 国产成人免费片在线视频观看| 久久综合图区亚洲综合图区| 337P日本欧洲亚洲大胆精品| 又粗又黄又猛又爽大片免费| 污网站免费在线观看| 亚洲综合伊人久久综合| 99热在线精品免费播放6| 亚洲国产综合精品中文字幕| ASS亚洲熟妇毛茸茸PICS| 免费在线黄色电影| 日日AV拍夜夜添久久免费| 亚洲天天做日日做天天看 | 国产成人精品免费视频网页大全| 亚洲AV中文无码乱人伦在线视色| 九九综合VA免费看| 亚洲AⅤ优女AV综合久久久| 亚洲成人高清在线观看| 久久精品免费电影| 亚洲中文无码卡通动漫野外| 午夜免费福利视频| 亚洲国产AV无码专区亚洲AV| fc2免费人成在线|