Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[aspectj-users] Logging in catch statement

Hi,

I am working on logging in catch statements. Therefore I use the 'handler' pointcut designator but now I also need the
argument of the static method where the catch statement is in and I could not find a way to get it.

For example in class BufferHistory the original code was:

public static void load(File file)
{
        ...
 
        try
        {
                BufferedReader in =
new BufferedReader(new FileReader(file));
                parser.parse(
null, null, in);
        }
        catch(XmlException xe)
        {
                int line = xe.getLine();
                String message = xe.getMessage();
                Log.log(Log.ERROR,BufferHistory.
class,file + ":" + line 
                +
": " + message);
        }

        ...
}


Now I want to replace the logging part and create an aspect for the Logging.
Therefore for the load method, to get also the file argument, I used the following pointcut and advice:

pointcut catchXmlException(XmlException xe):    handler(XmlException) &&
                                                                args(xe);

before(XmlException e, File file):      withincode(* *(File)) &&
                                                // I also tried 'cflow(execution(* *(File))'
                                                
args(file) &&
                                                catchXmlException(e) &&
                                                
within(BufferHistory) {
        Log.log(Log.ERROR,BufferHistory.
class,file + ":" + e.getLine()
        +
": " + e.getMessage());
}

but this advice does not affect the exception handler above.

Unfortunately it seems that the withincode and the cflow pointcut designators do not pick out exception handler join points.

Does someone have an idea, how I can get the File-parameter of the method in the exception handler in a nice way.
There is probably a way of storing the file in a parameter in the aspect every time the load method is called and if it runs into
the catch statement, you could just use this file, but this is not a really nice way.

Thanks in advance,
  Thomas Fritz

Back to the top