| To: | Edward Sumerfield <esumerfd@xxxxxxxxxxx> |
|---|---|
| Subject: | Re: [cinjug-users] Dynamic Proxy with Exceptions |
| From: | Michael Schneider <michaelschneider@xxxxxxxx> |
| Date: | Fri, 04 Nov 2005 00:10:45 -0500 |
| Cc: | CinJug <users@xxxxxxxxxx> |
| Delivered-to: | mailing list users@cinjug.org |
| In-reply-to: | <003601c5e0f4$256168b0$6601a8c0@junior> |
| Mailing-list: | contact users-help@cinjug.org; run by ezmlm |
| References: | <003601c5e0f4$256168b0$6601a8c0@junior> |
| User-agent: | Mozilla Thunderbird 1.0.6 (Windows/20050716) |
Ed, In you invoke method, you need to catch the InvocationTargetException and unwrap the excpetion, here is the modified code. Hope this is helpful, Mike ------------------------------------------------------------------- import java.lang.reflect.InvocationHandler; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.lang.reflect.Proxy; import junit.framework.TestCase; /**
* @author Ed Sumerfield
*/
public class DynamicProxyTest extends TestCase {
private ProxyTestInterface m_proxy; /* (non-Javadoc)
* @see junit.framework.TestCase#setUp()
*/
protected void setUp() throws Exception {
ProxyTestImpl impl = new ProxyTestImpl();
Class[] arguments = new Class[] { ProxyTestInterface.class };
InvocationHandler proxyImpl = new ProxyTestProxy(impl);m_proxy = (ProxyTestInterface) Proxy.newProxyInstance(impl.getClass() .getClassLoader(), arguments, proxyImpl); } /**
* Prove that we can call a proxy method
*/
public void testCallProxyMethod() {
m_proxy.aMethod();
} /**
* Prove that we can call a proxy method and pass in an argument
*/
public void testCallProxyMethodWithArgs() {
m_proxy.aMethodWithArgs("hello world");
} /**
* Prove that an exception can be thrown from the delegate.
* @throws Exception
*/
public void testThrowException() throws Throwable {
try {
m_proxy.aMethodWithAnException();
fail("Should have thrown exception");
} catch (ProxyTestException e) {
// Success
}
}
}interface ProxyTestInterface {
public void aMethod();public void aMethodWithArgs(String a_text); public void aMethodWithAnException() throws ProxyTestException; } class ProxyTestImpl implements ProxyTestInterface {
public void aMethod() {
System.out.println("aMethod called");
} public void aMethodWithArgs(String a_text) {
System.out.println("aMethodWithArgs called: " + a_text);
} public void aMethodWithAnException() throws ProxyTestException {
System.out.println("aMethodWithAnException called");
throw new ProxyTestException();
}
}class ProxyTestProxy implements InvocationHandler {
private ProxyTestInterface m_impl; public ProxyTestProxy(ProxyTestInterface a_impl) {
m_impl = a_impl;
} public Object invoke(Object a_proxy, Method a_method, Object[] a_args)
throws Throwable {
System.out.println("Proxy calling: " + a_method.getName());Object retVal = null; try { retVal = a_method.invoke(m_impl, a_args);
} catch (InvocationTargetException ite) {/* the method invocation threw an exception, so "unwrap" it and throw it. */ throw ite.getTargetException(); } return retVal; } } class ProxyTestException extends Exception {
}-- The greatest performance improvement occurs on the transition of from the non-working state to the working state. |
| <Prev in Thread] | Current Thread | [Next in Thread> |
|---|---|---|
| ||
| Previous by Date: | Dynamic Proxy with Exceptions, Edward Sumerfield |
|---|---|
| Next by Date: | Test Msg, bkbonner (sent by Nabble.com) |
| Previous by Thread: | Dynamic Proxy with Exceptions, Edward Sumerfield |
| Next by Thread: | RE: [cinjug-users] Dynamic Proxy with Exceptions, James Carman |
| Indexes: | [Date] [Thread] [Top] [All Lists] |