Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[aspectj-users] Dealing with wrapped classes

Hi everyone,

Love AspectJ.  I work on end-to-end tracing using X-Trace, and AspectJ is an awesome mechanism to do some of the tracing that I'm interested in.

I'm trying to profile file accesses; specifically, calls to FileInputStream+.read(..) and FileOutputStream+.write(..).  However, in many cases, file streams are wrapped in filter classes, such as Buffered streams or Data streams.  The specific base classes that provide this mechanism are FilterInputStream and FilterOutputStream.  Often, multiple filter streams are applied recursively to some base stream, for example new DataOutputStream(new BufferedOutputStream(new FileOutputStream("myfile.txt")));

Is there a way to define a pointcut that matches any Filter stream, whose underlying stream is a File stream?  The naive solution I have so far is:

  Object around(FilterInputStream o): target(o) && call(* FilterInputStream+.read(..)) {
    InputStream base = o;
    while (base instanceof FilterInputStream)
      base = ((FilterInputStream) base).in;
    boolean isFileStream = base instanceof FileInputStream;
    
    if (isFileStream) {
      long start = System.nanoTime();
      Object ret = proceed(o);
      long duration = System.nanoTime() - start;
      // do profiling stuff
      return ret;
    } else {
      return proceed(o);
    }
  }

I'm worried about the overhead of recursively examining the filter streams to determine whether the base stream is a file stream.  I'll also mention that I don't want to weave rt.jar.  It would be great if I could insert some kind of proxy class when the appropriate Filter instance is created, then the pointcut can just look for the proxy class.

Cheers,

Jon

Back to the top