Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [aspectj-users] generate a StackTraceElement

> | You can't bind the JoinPoint available in advice this way.
> | 
> Why not? I think I have even read about this in one of AJ book 

Ok, I should have said "shouldn't" not "can't".

You can't, or shouldn't, because we (intentionally) haven't 
documented in the programming guide the order or contents
of advice parameters.  By experiment you might find different
behaviors for different join points, different compiler
options, and different versions or implementations of 
the compiler/weaver.

If you want to print any available JoinPoint|StaticPart argument,
you can do that with the code below.  It's a bit like looking
at the bytecode methods that implement advice: neat, but I 
wouldn't rely on it for real code.

Wes

P.S. - 
> Why not? I think I have even read about this in one of AJ book 
> (Cookbook?).

I can't speak to the cookbook; I haven't read it.  I would
hope/expect that the cookbook gave similar caveats when 
presenting it.  If a language feature is not in the 
programming guide, developers are at their peril to rely on it.  

---------------- bugs/Main.java
package bugs;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.reflect.SourceLocation;

import util.IAspect;

/**
 */
public class Main {

    public static void main(String[] args) {
    }
}
aspect A implements IAspect {
    before() : within(Main) {
        System.out.println("before() " + thisJoinPointStaticPart);
    }
}
aspect B implements IAspect {
    before() : adviceexecution() && !within(B) {
        if (!logAdvisedJoinPoint(thisJoinPoint.getArgs())) { 
            logThisJoinPoint(-1, thisJoinPointStaticPart); 
        }
    }
    boolean logAdvisedJoinPoint(Object[] args) {
        boolean logged = false;
        for (int i = 0; !logged && (i < args.length); i++) {
            if (args[i] instanceof JoinPoint) {
                logThisJoinPoint(i, 
                  ((JoinPoint) args[i]).getStaticPart());
                return true;
            } else if (args[i] instanceof JoinPoint.StaticPart) {
                logThisJoinPoint(i, 
                   (JoinPoint.StaticPart) args[i]);
                return true;
            } 
                
        }
        return false;
    }
    void logThisJoinPoint(int index, JoinPoint.StaticPart jp) {
        Signature sig = jp.getSignature();
        SourceLocation loc = jp.getSourceLocation();
        StackTraceElement ste = new StackTraceElement
            (sig.getDeclaringType().getName(), sig.getName(),
                loc.getFileName(), loc.getLine());
        print((-1<index? "advisee " : " advice ")
               + index + ": " + ste);
    }
    void print(String s) {
        System.out.print(s + "\n");
    }
}


> 
> - --
> :alex |.::the_mindstorm::.|
> 
> | The code below works, logging all the adviceexecution
> | join points.
> |
> | Valerio, do you instead want to log the join points being
> | advised by the advice (in Main below, staticinitialization
> | and method-execution)?  If the pointcuts were named and
> | enumerable, you could do that, but it's awkward.  What's
> | the use-case for this? Does the variable
> | thisEnclosingJoinPointStaticPart help?
> |
> | Wes
> |
> | ------------------ bug/Main.java
> | package bugs;
> |
> | import org.aspectj.lang.JoinPoint;
> | import org.aspectj.lang.Signature;
> | import org.aspectj.lang.reflect.SourceLocation;
> |
> | /**
> |  */
> | public class Main {
> |
> |     public static void main(String[] args) {
> |     }
> | }
> | aspect A {
> |     before() : within(Main) {
> |         System.out.println("before() "
> |              + thisJoinPointStaticPart);
> |     }
> | }
> | aspect B implements IAspect {
> |     before() : adviceexecution() && !within(B) {
> |         log(thisJoinPointStaticPart);
> |     }
> |     void log(JoinPoint.StaticPart jp) {
> |         Signature sig = jp.getSignature();
> |         SourceLocation loc = jp.getSourceLocation();
> |         StackTraceElement ste = new StackTraceElement
> |             (sig.getDeclaringType().getName(), sig.getName(),
> |                 loc.getFileName(), loc.getLine());
> |         System.out.println("BeforeAndAfter advice: "+ ste);
> |     }
> | }
> |
> |
> |
> |> ------------Original Message------------
> |> From: Alexandru Popescu <alexandru.popescu@xxxxxxxxx>
> |> To: aspectj-users@xxxxxxxxxxx
> |> Date: Thu, Feb-10-2005 10:16 AM
> |> Subject: Re: [aspectj-users] generate a StackTraceElement
> |>
> | [quote Valerio Schiavoni::on 2/10/2005 6:44 PM]
> | | On Thursday 10 February 2005 16:29, Alexandru Popescu wrote:
> | |> The only available pointcut related to aspects is the
> | adviceexecution() but
> | |> you cannot refine it further to match a before advice and after
> | advice.
> | |
> | | i don't need to refine it. It'd be enough to be able to "dump" the
> | stack
> | | everytime an advice is executed.
> | |
> | | i tried with :
> | |
> | | pointcut Advices(): adviceexecution() && within(TraceAspect);
> | |
> | |     after(): Advices() {
> | |           StackTraceElement ste = new StackTraceElement
> | | (thisJoinPoint.getSignature().getDeclaringType().getName(),
> | |                 thisJoinPoint.getSignature().getName(),
> | |                 thisJoinPoint.getSourceLocation().getFileName(),
> | |                 thisJoinPoint.getSourceLocation().getLine());
> | |          System.out.println("BeforeAndAfter advice: "+ ste);
> | | }
> | |
> | | so, if another advice is present, i want to get info about that 
> join
> | point.
> | | This is not possible? If so...why ?
> | |
> | | thanks,
> | | valerio
> |
> | I think what you are looking for is something in this direction:
> |
> | pointcut advices(JoinPoint jp): adviceexecution() && args(jp) &&
> | within(...)
> |
> | --
> | :alex |.::the_mindstorm::.|
> _______________________________________________
> aspectj-users mailing list
> aspectj-users@xxxxxxxxxxx
> http://dev.eclipse.org/mailman/listinfo/aspectj-users
> 
> | _______________________________________________
> | aspectj-users mailing list
> | aspectj-users@xxxxxxxxxxx
> | http://dev.eclipse.org/mailman/listinfo/aspectj-users
> 
> 
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.4.0 (MingW32)
> 
> iD8DBQFCC7i5TTDTje0R2dgRAp2cAJ9aXgpux84+BDEsNCFrvFbqPnqgRACaAwgh
> lEQpjClpH6nDezCBm6ZM58Q=
> =Ri8P
> -----END PGP SIGNATURE-----
> _______________________________________________
> aspectj-users mailing list
> aspectj-users@xxxxxxxxxxx
> http://dev.eclipse.org/mailman/listinfo/aspectj-users
> 




Back to the top