KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > scriptella > util > ProxyAdapter


1 /*
2  * Copyright 2006-2007 The Scriptella Project Team.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package scriptella.util;
17
18 import java.lang.reflect.InvocationHandler JavaDoc;
19 import java.lang.reflect.InvocationTargetException JavaDoc;
20 import java.lang.reflect.Method JavaDoc;
21 import java.lang.reflect.Proxy JavaDoc;
22
23 /**
24  * A dynamic proxy adapter which allows overriding several
25  * methods of a target proxy.
26  * <p>To create a proxy adapter for Interface, create subclass of
27  * ProxyAdapter and define methods from Interface you want to handle,
28  * other methods invocations will be failed.
29  *
30  * @author Fyodor Kupolov
31  * @version 1.0
32  */

33 public class ProxyAdapter<T> {
34     private final T proxy;
35
36     @SuppressWarnings JavaDoc("unchecked")
37     public ProxyAdapter(Class JavaDoc... interfaces) {
38         proxy = (T) Proxy.newProxyInstance(getClass().getClassLoader(), interfaces, new InvocationHandler JavaDoc() {
39             public Object JavaDoc invoke(Object JavaDoc proxy, Method JavaDoc method, Object JavaDoc[] args) throws Throwable JavaDoc {
40                 Method JavaDoc m;
41                 try {
42                     //Determine if the method has been defined in a subclass
43
m = ProxyAdapter.this.getClass().getMethod(method.getName(), method.getParameterTypes());
44                     m.setAccessible(true);
45                 } catch (Exception JavaDoc e) { //if not found
46
throw new UnsupportedOperationException JavaDoc(method.toString(), e);
47
48                 }
49                 //Invoke the method found and return the result
50
try {
51                     return m.invoke(ProxyAdapter.this, args);
52                 } catch (InvocationTargetException JavaDoc e) {
53                     throw e.getCause();
54                 }
55             }
56         });
57     }
58
59     /**
60      * @return proxy instance implementing T.
61      */

62     public T getProxy() {
63         return proxy;
64     }
65
66
67     /**
68      * Usage example
69      */

70     public static void main(String JavaDoc[] args) throws Exception JavaDoc {
71         //Create adapter for Appendable
72
ProxyAdapter<Appendable JavaDoc> pa = new ProxyAdapter(Appendable JavaDoc.class) {
73             private StringBuilder JavaDoc sb = new StringBuilder JavaDoc();
74             //Override only 2 methods: append and toString
75
public Appendable JavaDoc append(char c) {
76                 System.out.println("Proxy append(char c) method. Append "+c);
77                 sb.append(c);
78                 return (Appendable JavaDoc) getProxy();
79             }
80
81             public String JavaDoc toString() {
82                 return "Proxy toString method: "+sb;
83             }
84
85         };
86         final Appendable JavaDoc a = pa.getProxy();
87         a.append('1').append('2');
88         System.out.println("a.toString() = " + a.toString());
89         //this invocation fails because no method has been created
90
a.append("Not implemented");
91
92     }
93 }
94
Popular Tags