users
[Top] [All Lists]

RE: [cinjug-users] Dynamic Proxy with Exceptions

To: "'Edward Sumerfield'" <esumerfd@xxxxxxxxxxx>, "'CinJug'" <users@xxxxxxxxxx>
Subject: RE: [cinjug-users] Dynamic Proxy with Exceptions
From: "James Carman" <james@xxxxxxxxxxxxxxxxxxxx>
Date: Fri, 4 Nov 2005 09:15:22 -0500
Delivered-to: mailing list users@cinjug.org
In-reply-to: <003601c5e0f4$256168b0$6601a8c0@junior>
Mailing-list: contact users-help@cinjug.org; run by ezmlm
Thread-index: AcXg9C2B53gYYLClQsiij7eM3etqagAVcGpQ

If you want to create dynamic proxies, you might want to check out Jakarta Commons Proxy (http://jakarta.apache.org/commons/sandbox/proxy/).  It’s a sandbox project right now, so the API is still somewhat unstable, but next week I will be working towards a first release and promoting it into the “commons proper.”  But, Commons Proxy will make dynamic proxies very easy, even if you don’t have an interface to proxy and you need to subclass!

 


From: Edward Sumerfield [mailto:esumerfd@xxxxxxxxxxx]
Sent: Thursday, November 03, 2005 10:59 PM
To: CinJug
Subject: [cinjug-users] Dynamic Proxy with Exceptions

 

I want to create a dynamic proxy that can call a method that might throw an exception. When this method does throw I get an java.lang.reflect.UndeclaredThrowableException exception and am not sure what the fix is. The "testThrowException" test is the one that fails.

import java.lang.reflect.InvocationHandler;
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());
        return a_method.invoke(m_impl, a_args);
    }
}

class ProxyTestException extends Exception
{
}

 

Ed

 

<Prev in Thread] Current Thread [Next in Thread>