KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > portal > test > portal > ProxyDelegate


1 /*****************************************
2  * *
3  * JBoss Portal: The OpenSource Portal *
4  * *
5  * Distributable under LGPL license. *
6  * See terms of license at gnu.org. *
7  * *
8  *****************************************/

9 package org.jboss.portal.test.portal;
10
11 import java.lang.reflect.InvocationHandler JavaDoc;
12 import java.lang.reflect.InvocationTargetException JavaDoc;
13 import java.lang.reflect.Method JavaDoc;
14 import java.lang.reflect.Proxy JavaDoc;
15
16 /**
17  * @author <a HREF="mailto:julien@jboss.org">Julien Viet</a>
18  * @version $Revision: 1.2 $
19  */

20 public class ProxyDelegate
21 {
22    public Object JavaDoc createProxy(Class JavaDoc itf)
23    {
24       InvocationHandler JavaDoc ih = new InvocationHandler JavaDoc()
25       {
26          public Object JavaDoc invoke(Object JavaDoc proxy, Method JavaDoc method, Object JavaDoc[] args) throws Throwable JavaDoc
27          {
28             try
29             {
30                Class JavaDoc implementorClass = ProxyDelegate.this.getClass();
31                Method JavaDoc implementedMethod = implementorClass.getMethod(method.getName(), method.getParameterTypes());
32                return implementedMethod.invoke(ProxyDelegate.this, args);
33             }
34             catch (NoSuchMethodException JavaDoc e)
35             {
36                throw new UnsupportedOperationException JavaDoc();
37             }
38             catch (InvocationTargetException JavaDoc e)
39             {
40                throw e.getTargetException();
41             }
42             catch (Exception JavaDoc e)
43             {
44                throw new RuntimeException JavaDoc();
45             }
46          }
47       };
48       return Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(), new Class JavaDoc[]{itf}, ih);
49    }
50 }
51
Popular Tags