import java.lang.reflect.*; /** * DynamicBeanProxy2 is an alternate version of DynamicBeanProxy that reuses the * BeanProxy class. * * @author Thornton Rose */ public class DynamicBeanProxy2 { // newProxyInstance(Class): /** * Create new proxy instance for given class. */ public static IBeanProxy newProxyInstance(Class theBeanClass) throws Exception { return (IBeanProxy) Proxy.newProxyInstance( theBeanClass.getClassLoader(), new Class[] { IBeanProxy.class }, new BeanProxyInvocationHandler(theBeanClass)); } // newProxyInstance(Object): /** * Create new proxy instance for given object. */ public static IBeanProxy newProxyInstance(Object theBean) throws Exception { IBeanProxy proxy = newProxyInstance(theBean.getClass()); proxy.setBean(theBean); return proxy; } /** * BeanProxyInvocationHandler is the invocation handler for method * invocations on dynamic bean proxies (implementations of IBeanProxy). */ protected static class BeanProxyInvocationHandler implements InvocationHandler { private BeanProxy beanProxy; // BeanProxyInvocationHandler(Class): /** * Create a new BeanProxyInvocationHandler for the given class. */ public BeanProxyInvocationHandler(Class theBeanClass) throws Exception { beanProxy = new BeanProxy(theBeanClass); } // invoke(Object, Method, Object[]): /** * Handle invocation of the given method for the given proxy. */ public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { return method.invoke(beanProxy, args); } } }