Swing


天行健 君子以自强不息

posts - 69, comments - 215, trackbacks - 0, articles - 16
   :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

Reflect&Proxy

Posted on 2008-04-03 10:57 zht 阅读(351) 评论(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();

    }

}