[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
RE: [aspectj-dev] Can throw arbitrary Checked Exception in around?

Hi Jonghyun,

 

It might be interesting to compare notes on your approach to virtual mock objects. I have more recently done some work on ajMock, extended the jmock framework to allow mocking behavior at an arbitrary join point, which I think is the “right” AOP way to mock. In ajmock, I ended up using this approach to throw arbitrary exceptions (see below). You can see the ajmock code in the Glassbox cvs repository on sourceforge (see glassbox.test.ajmock under monitor/testsrc):

public class Thrower {

    static Unsafe theUnsafe = getUnsafe();

   

    protected static Unsafe getUnsafe ()

    {

        try {

            Field field = Unsafe.class.getDeclaredField("theUnsafe");

            field.setAccessible(true);

            return (Unsafe)field.get(null);

        } catch (Exception ex) {

            throw new RuntimeException("can't get Unsafe instance", ex);

        }

    }

 

    public static void throwException(Throwable throwable) {

        theUnsafe.throwException(throwable);

    }

}

 

I think there are some cases where it would be nice for AspectJ to be smarter about knowing that a given throwable should be allowed to be thrown at a given join point (e.g., rethrowing a checked throwable that was just caught). However, I think requiring a throws clause that’s compatible with the join point in question is the right approach for AspectJ, unlike the way that Java dynamic proxies let you throw anything but convert incompatible throwables to an UndeclaredThrowableException. I think there are reasonable work-arounds for the few use cases where you really need to throw arbitrary throwables…


From: aspectj-dev-bounces@xxxxxxxxxxx [mailto:aspectj-dev-bounces@xxxxxxxxxxx] On Behalf Of Jonghyun Yoon
Sent: Friday, September 08, 2006 5:00 PM
To: aspectj-dev@xxxxxxxxxxx
Subject: [aspectj-dev] Can throw arbitrary Checked Exception in around?

 

Thank you Ron Bodkin

 

Your answer (2) is what I looking for.

I really appreciate.

I am actually implementing something like virtual mock.

I have been implemented all mock feature except static mehtod exception throwing.

(To throw exception in my mock, It need to write .aj file) other exception rhrowings are

implemented by CGLib.

 

However, Does Aspectj creator have plan to implement this feature?

I think this should be included in core aspectj feature.

 

Thank you.

 

On 9/6/06, aspectj-dev-request@xxxxxxxxxxx <aspectj-dev-request@xxxxxxxxxxx > wrote:

Send aspectj-dev mailing list submissions to
       aspectj-dev@xxxxxxxxxxx

To subscribe or unsubscribe via the World Wide Web, visit
       https://dev.eclipse.org/mailman/listinfo/aspectj-dev
or, via email, send a message with subject or body 'help' to
       aspectj-dev-request@xxxxxxxxxxx

You can reach the person managing the list at
       aspectj-dev-owner@xxxxxxxxxxx

When replying, please edit your Subject line so it is more specific
than "Re: Contents of aspectj-dev digest..."


Today's Topics:

  1. RE: Re: aspectj-dev Digest, Vol 19, Issue 2 (Ron Bodkin)


----------------------------------------------------------------------

Message: 1
Date: Mon, 4 Sep 2006 13:05:07 -0700
From: "Ron Bodkin" <rbodkin@xxxxxxxxxxxxxx >
Subject: RE: [aspectj-dev] Re: aspectj-dev Digest, Vol 19, Issue 2
To: "'AspectJ developer discussions'" <aspectj-dev@xxxxxxxxxxx>
Message-ID: < 00f501c6d05d$6bdf9ab0$680fa8c0@Aqua>
Content-Type: text/plain; charset="us-ascii"

Hi Jonghyun,



I've hit cases where I've wanted to do this. There are two good solutions:

1)       Most often you can use the Exception Introduction pattern as
Ramnivas Laddad describes in AspectJ in Action: you wrap the exception in an
unchecked exception and then unpack it in some receiving type

2)       in odd cases, e.g., for virtual mock object testing frameworks, you
can use other techniques to throw checked exceptions and rely on dynamic
tests to ensure safety. I believe this should be a last resort, rather like
using Java reflections's setAccessible to access non-public members.



This topic has come up before on the AspectJ users mailing list, so a more
full discussion can be read at



http://dev.eclipse.org/mhonarc/lists/aspectj-dev/msg01412.html

http://dev.eclipse.org/mhonarc/lists/aspectj-dev/msg01400.html (you can
download a direct bytecode implementation of throwing checked exceptions
from the aTrack open source project too).



HTH,

Ron



_____

From: aspectj-dev-bounces@xxxxxxxxxxx
[mailto: aspectj-dev-bounces@xxxxxxxxxxx] On Behalf Of Jonghyun Yoon
Sent: Monday, September 04, 2006 5:25 AM
To: aspectj-dev@xxxxxxxxxxx
Subject: [aspectj-dev] Re: aspectj-dev Digest, Vol 19, Issue 2



Thank you about your response



I am not saying about breaking JLS. What I am saying is totally regal.

Please read my first mail.

For more simple explaination.



//original method

someMethod()



//advice

around() throws * : call(* *.someMethod()) {

throw new CheckedException();

}



//weven method and also illegal if someMethod() can not throw
CheckedException

someMethod()

throw new CheckedException()...


//but if aspectj compiler make next code,

//it is totally regal because It will never throw checked exception which
someMethod() can not throw

try {

someMethod()

throw new CheckedException()...

} catch (Throwable t) {

if (isPointCutCanThrow(t)) {

   throw t

} else {

   throw new RunTimeException(t);

}

}





I hoped there was solution like this. But it seems aspectj does not
implement it yet..

I hope it will be implemented soon.




On 9/4/06, aspectj-dev-request@xxxxxxxxxxx <aspectj-dev-request@xxxxxxxxxxx
> wrote:

Send aspectj-dev mailing list submissions to
       aspectj-dev@xxxxxxxxxxx

To subscribe or unsubscribe via the World Wide Web, visit
      https://dev.eclipse.org/mailman/listinfo/aspectj-dev
or, via email, send a message with subject or body 'help' to
      aspectj-dev-request@xxxxxxxxxxx

You can reach the person managing the list at
       aspectj-dev-owner@xxxxxxxxxxx  <mailto:aspectj-dev-owner@xxxxxxxxxxx>


When replying, please edit your Subject line so it is more specific
than "Re: Contents of aspectj-dev digest..."


Today's Topics:

1. Re: Can throw arbitrary Checked Exception in around? (Wes)


----------------------------------------------------------------------

Message: 1
Date: Sat, 2 Sep 2006 22:32:43 -0700
From: "Wes" <wes@xxxxxxxxxxxxxx >
Subject: Re: [aspectj-dev] Can throw arbitrary Checked Exception in
      around?
To: "AspectJ developer discussions" <aspectj-dev@xxxxxxxxxxx>
Message-ID: < 20060902223243.1136929287.wes@xxxxxxxxxxxxxx>
Content-Type: text/plain; charset="utf-8"


See the documentation of "declare soft".

http://www.google.com/search?q=site%3Awww.eclipse.org+%22declare+soft
http://www.eclipse.org/aspectj/doc/released/progguide/semantics-declare.html
http://www.eclipse.org/aspectj/doc/next/adk15notebook/declare-soft.html

Exception checking is the responsibility of every Java compiler, including
AspectJ's.

Wes

------------Original Message------------
From: "Jonghyun Yoon" < <mailto:ormastes@xxxxxxxxx>   ormastes@xxxxxxxxx>
To: aspectj-dev@xxxxxxxxxxx
Date: Sat, Sep-2-2006 6:08 AM
Subject: [aspectj-dev] Can throw arbitrary Checked Exception in around?
Does anyone know how to throw abitrary checked Exception in around?
When I try to write advise which throw arbitrary Exception
It always show me error message can't throw checked Exception 'xxxx'
Only way to throw exception without error is
Object around() throws AnException: call(* *(..) throws AnException)  {
  throw new AnException();
}
But problem is that it must be written to every Exceptions and even it is
done
it can not be prevent new Exceptions.
Is there any solution for this problem?
If there isn't, I hope aspectj developer make a solution like this.
when aspecj compiler weaving
suround the advice by try catch block
check throwable can be thrown by target point cut
if it can be thrown by the point cut just rethrow it
if not just wrap it with RuntimeException and throw it.
It may reduce performance but does not brake Java Language Specification and
reduce a lot of works.
It will be not a problem if compiler give warning about performance penalty
Or only some special grammar allow to throw checked Exceptions(like: Object
around() throws * : somePointcut()).
I hope there is some solution already exist but i don't know about it.
Thank you.


_______________________________________________
aspectj-dev mailing list
aspectj-dev@xxxxxxxxxxx  <mailto:aspectj-dev@xxxxxxxxxxx>
https://dev.eclipse.org/mailman/listinfo/aspectj-dev
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
http://eclipse.org/pipermail/aspectj-dev/attachments/20060902/6f106153/attac
hment.html

------------------------------

_______________________________________________
aspectj-dev mailing list
aspectj-dev@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/aspectj-dev


End of aspectj-dev Digest, Vol 19, Issue 2
******************************************



-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://eclipse.org/pipermail/aspectj-dev/attachments/20060904/ebc21946/attachment.html

------------------------------

_______________________________________________
aspectj-dev mailing list
aspectj-dev@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/aspectj-dev


End of aspectj-dev Digest, Vol 19, Issue 4
******************************************