csalem05
asked on
AspectJ exception handler retrieving object and method information
I'm new to AspectJ and I'm I'm not very familiar with it. I'm trying to write an exception handler that writes a log whenever an exception is thrown, I need to track which class and method the exception occurred in and the arguments that were passed into that method. My question is, how would I retrieve the arguments from the method where the exception was thrown?
ASKER
I'm not sure what you mean.
If I have this class:
class TestAspect{
public void testAspect1(String arg1){ throw Exception; }
public void testAspect2(int arg1, intarg2){ throw Exception(); }
}
And this aspect:
public aspect ExceptionAspect{
pointcut exceptionHandler(Exception e): handler(Exception+) && args(e);
before(Exception e): exceptionHandler(e){
}
}
I can get the object where the exception was throw with thisJoinPointStaticPart. I still don't understand how I would get the arguments that were passed into the method that threw the exception.
If I run:
new TestAspect().testAspect1(" test");
inside the exceptionHandler aspect how would I get the String "test"
or if I run:
new TestAspect().testAspect2(1 , 2);
inside the exceptionHandler aspect how would i get the integers 1 and 2?
If I have this class:
class TestAspect{
public void testAspect1(String arg1){ throw Exception; }
public void testAspect2(int arg1, intarg2){ throw Exception(); }
}
And this aspect:
public aspect ExceptionAspect{
pointcut exceptionHandler(Exception
before(Exception e): exceptionHandler(e){
}
}
I can get the object where the exception was throw with thisJoinPointStaticPart. I still don't understand how I would get the arguments that were passed into the method that threw the exception.
If I run:
new TestAspect().testAspect1("
inside the exceptionHandler aspect how would I get the String "test"
or if I run:
new TestAspect().testAspect2(1
inside the exceptionHandler aspect how would i get the integers 1 and 2?
> I can get the object where the exception was throw with thisJoinPointStaticPart. I still don't understand how I would get the arguments that were passed into the method that threw the exception.
When an exception is thrown within a function, that exception may be caught, its arguments may be serialized and rethrown as a custom exception message which may be caught in any consuming method and its input parameters analysed.
When an exception is thrown within a function, that exception may be caught, its arguments may be serialized and rethrown as a custom exception message which may be caught in any consuming method and its input parameters analysed.
Method1(Class a, Class b)
try
{
}
Catch
(
string newMessageString = serialize (a) + serialize(b) + use reflection to get the name of the method dynamically;
new exception(newMessageString ());
)
try
{
}
Catch
(
string newMessageString = serialize (a) + serialize(b) + use reflection to get the name of the method dynamically;
new exception(newMessageString
)
ASKER
I trying to get rid of the try catches, that's why I'm using an aspect.
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
I was hoping there was someway to define a generic set of arguments when defining the pointcut, but I'm beginning to think it's just not possible.
Thanks for your help.
Thanks for your help.
check the following tutorial, and check the section named "Exposing Context in Pointcuts"
http://www.eclipse.org/aspectj/doc/released/progguide/starting-aspectj.html
http://www.eclipse.org/aspectj/doc/released/progguide/starting-aspectj.html
If you were also looking at how to get the method name, you should use reflection for the same,