KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jfun > yan > etc > InjectingProxy


1 package jfun.yan.etc;
2
3 import java.lang.reflect.InvocationHandler JavaDoc;
4 import java.lang.reflect.Method JavaDoc;
5 import java.lang.reflect.Proxy JavaDoc;
6
7 import jfun.yan.Mutation;
8
9 /**
10  * A proxy class that injects dependencies
11  * into objects returned from the proxied methods.
12  * <p>
13  * a {@link jfun.yan.Mutation} object is used to fulfill the injection.
14  * </p>
15  *
16  * @author Ben Yu
17  *
18  */

19 public final class InjectingProxy implements InvocationHandler JavaDoc{
20   private final Mutation injector;
21   private final Class JavaDoc target_type;
22   private final Object JavaDoc proxied;
23   
24   private InjectingProxy(Object JavaDoc proxied, Class JavaDoc target_type, Mutation injector) {
25     this.injector = injector;
26     this.proxied = proxied;
27     this.target_type = target_type;
28   }
29   public Object JavaDoc invoke(Object JavaDoc proxy, Method JavaDoc mtd, Object JavaDoc[] args)
30   throws Throwable JavaDoc {
31     if(Object JavaDoc.class.equals(mtd.getDeclaringClass())){
32       return mtd.invoke(this, args);
33     }
34     else{
35       final Object JavaDoc r = mtd.invoke(proxied, args);
36       if(target_type.isInstance(r)){
37         //the type to inject.
38
injector.mutate(r);
39       }
40       return r;
41     }
42   }
43   public boolean equals(Object JavaDoc obj) {
44     if(obj == null) return false;
45     if(Proxy.isProxyClass(obj.getClass())){
46       obj = Proxy.getInvocationHandler(obj);
47     }
48     if(obj instanceof InjectingProxy){
49       final InjectingProxy other = (InjectingProxy)obj;
50       return proxied.equals(other.proxied);
51     }
52     else return proxied.equals(obj);
53   }
54   public int hashCode() {
55     return proxied.hashCode();
56   }
57   public String JavaDoc toString() {
58     return proxied.toString();
59   }
60   /**
61    * To create a proxy that injects dependency into return values
62    * of the proxied methods.
63    * @param cloader the ClassLoader used by the dynamic proxy.
64    * @param itfs the interfaces that the proxy object implements.
65    * @param proxied the object to be proxied.
66    * @param injectee_type For return values of this type, dependencies are injected.
67    * @param injector the object responsible for injecting dependencies.
68    * @return the proxy object.
69    */

70   public static Object JavaDoc getInjectingProxy(ClassLoader JavaDoc cloader, Class JavaDoc[] itfs,
71       Object JavaDoc proxied, Class JavaDoc injectee_type, Mutation injector){
72     return Proxy.newProxyInstance(cloader, itfs,
73         new InjectingProxy(proxied, injectee_type, injector));
74   }
75   /**
76    * To create a proxy that injects dependency into return values
77    * of the proxied methods.
78    * @param cloader the ClassLoader used by the dynamic proxy.
79    * @param itf the interface that the proxy object implements.
80    * @param proxied the object to be proxied.
81    * @param injectee_type For return values of this type, dependencies are injected.
82    * @param injector the object responsible for injecting dependencies.
83    * @return the proxy object.
84    */

85   public static Object JavaDoc getInjectingProxy(ClassLoader JavaDoc cloader, Class JavaDoc itf,
86       Object JavaDoc proxied, Class JavaDoc injectee_type, Mutation injector){
87     return getInjectingProxy(cloader, new Class JavaDoc[]{itf}, proxied, injectee_type, injector);
88   }
89   /**
90    * To create a proxy that injects dependency into return values
91    * of the proxied methods.
92    * <p>
93    * The ClassLoader of the <i>proxied</i> object is used to load the proxy class.
94    * </p>
95    * @param itf the interface that the proxy object implements.
96    * @param proxied the object to be proxied.
97    * @param injectee_type For return values of this type, dependencies are injected.
98    * @param injector the object responsible for injecting dependencies.
99    * @return the proxy object.
100    */

101   public static Object JavaDoc getInjectingProxy(Class JavaDoc itf, Object JavaDoc proxied, Class JavaDoc injectee_type, Mutation injector){
102     return getInjectingProxy(proxied.getClass().getClassLoader(), itf, proxied, injectee_type, injector);
103   }
104   /**
105    * To create a proxy that injects dependency into return values
106    * of the proxied methods. The proxy implements all interfaces implemented by the proxied object.
107    * <p>
108    * The ClassLoader of the <i>proxied</i> object is used to load the proxy class.
109    * </p>
110    * @param proxied the object to be proxied.
111    * @param injectee_type For return values of this type, dependencies are injected.
112    * @param injector the object responsible for injecting dependencies.
113    * @return the proxy object.
114    */

115   public static Object JavaDoc getInjectingProxy(Object JavaDoc proxied, Class JavaDoc injectee_type, Mutation injector){
116     final Class JavaDoc[] itfs = proxied.getClass().getInterfaces();
117     return getInjectingProxy(proxied.getClass().getClassLoader(), itfs,
118         proxied, injectee_type, injector);
119   }
120
121 }
122
Popular Tags