Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[aspectj-users] how to write polymorphic exception throwing

Hi all,

I'm stuck on how to write polymorphic around advice that might throw a checked exception of diffent types depending on the signature of the called method. E.g.,
    void around() : somepcd() {
        if (getThrowable() != null) {
            dynamicThrow(getThrowable());
        }
    }

With Java 1.5 generics it will hopefully be easy to do:
    protected void dynamicThrow(T t) throws T [T instanceof Throwable] {
        throw t;
    }

However, with the current AspectJ and Java it seems like it can't be done. I tried using reflection (code illustrated below) but this just throws an InvocationTargetException caused by the intended exception. Any suggestions of how to do this, or for other ways to accomplish the same goal?

Using reflection (with exception handling omitted):

    void around() : somepcd() {
        if (getThrowable() != null) {
            method = ImposterProcessing.class.getMethod("dynamicThrow", new Class[] { Throwable.class });
            method.invoke(this, new Object[] { getThrowable() });
        }
    }

    public void dynamicThrow(Throwable t) throws Throwable {
        throw t;
    }    

Ron

Ron Bodkin
Chief Technology Officer
New Aspects of Security
m: (415) 509-2895


Back to the top