Skip to main content

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

I figured out a way to do this. I used Java assembly code to create a dynamicThrow method that doesn't check its exceptions. If anyone would like the compiled class file let me know.

Here's more detail:

Compiled from DynamicThrow.java
public class ajee.utility.DynamicThrow extends java.lang.Object {
    public ajee.utility.DynamicThrow();
    public static void dynamicThrow(java.lang.Throwable);
}

Method ajee.utility.DynamicThrow()
   0 aload_0
   1 invokespecial #1 <Method java.lang.Object()>
   4 return

Method void dynamicThrow(java.lang.Throwable)
   0 aload_0
   1 athrow

To get this, I wrote:
/*
*
* Created on 30-Jun-2003
*
* Copyright (c) 2003 New Aspects of Security.
* All rights reserved.
*
* Ron Bodkin
*/
package ajee.utility;

/**
 * This class provides a single method, which allows dynamically
 * throwing an exception of any type at runtime.
 * To build it requires bytecode assembly.
 */
public class DynamicThrow {
    /** This method always throws its parameter as an exception.
     * It throws <em>any</em> Throwable without including it in the
     * signature as a checked exception. Useful, e.g., in highly
     * polymorphic around advice.
     */
    public static void dynamicThrow(Throwable throwable) {
        throw (RuntimeException)throwable;
    }
}

then used a BCEL program to remove the CHECKCAST instruction :-) - pretty much their remove NOP peephole optimizer, but just removing CHECKCAST

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

> ------------Original Message-------------
> From: "Ron Bodkin" <rbodkin@xxxxxxxxxxx>
> To: aspectj-users@xxxxxxxxxxx
> Date: Sun, Jun-29-2003 9:36 PM
> Subject: 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