<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 閱讀(361) 評論(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();

        }

    }
    主站蜘蛛池模板: 亚洲精品tv久久久久久久久久| 综合久久久久久中文字幕亚洲国产国产综合一区首 | 伊人久久综在合线亚洲91| 国产精品白浆在线观看免费| 国产日韩久久免费影院| 亚洲av无码一区二区三区乱子伦| 国产四虎免费精品视频| 丰满少妇作爱视频免费观看| 亚洲码在线中文在线观看| 亚洲国产成人久久综合区| 99re在线这里只有精品免费| 精品女同一区二区三区免费播放| 亚洲AV午夜福利精品一区二区| 最近免费中文字幕大全视频| 男女拍拍拍免费视频网站| 亚洲一区二区三区在线观看网站| 亚洲熟女少妇一区二区| 午夜免费福利影院| 日韩精品无码免费一区二区三区 | 久久精品国产精品亚洲蜜月| 永久免费视频v片www| 国产无遮挡无码视频免费软件| 亚洲 日韩 色 图网站| 久久精品国产亚洲沈樵| 国产精品免费综合一区视频| 免费av一区二区三区| 国产精品亚洲天堂| 亚洲毛片无码专区亚洲乱| 亚洲一级片免费看| 免费的一级片网站| 成人免费激情视频| 日本黄色动图免费在线观看| 人妻仑刮八A级毛片免费看| 亚洲日韩AV一区二区三区四区| 久久精品国产精品亚洲毛片| 亚洲宅男天堂在线观看无病毒| 在线永久免费观看黄网站| av大片在线无码免费| 1000部夫妻午夜免费| 免费看搞黄视频网站| 美女被免费网站91色|