Skip to main content

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

Hello Wes,
On Thursday 10 February 2005 20:11, Wes Isberg wrote:
> The code below works, logging all the adviceexecution
> join points.
i don't know why, but still can't get any output.

let me try to sum up what I have now:
a stacktraceaspect, like that:
/--StackTracerAspect.java--/
public aspect StackTracerAspect {

 before(): adviceexecution() && !within(StackTracerAspect) {
  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);
       }
}

Then, i have  a TraceAspect, which is the taken by the tracing example of the 
programming guide.
(i paste it only to be as clear as possible)
package tracing;

import java.io.PrintStream;

/**
 * 
 * This class provides support for printing trace messages into a stream. 
Trace
 * messages are printed before and after constructors and methods are 
executed.
 * The messages are appended with the string representation of the objects 
whose
 * constructors and methods are being traced. It defines one abstract pointcut
 * for injecting that tracing functionality into any application classes.
 *  
 */
abstract aspect TraceAspect {

    /*
	 * Functional part
	 */

    /**
	 * There are 3 trace levels (values of TRACELEVEL): 0 - No messages are
	 * printed 1 - Trace messages are printed, but there is no indentation
	 * according to the call stack 2 - Trace messages are printed, and they are
	 * indented according to the call stack
	 */
    public static int TRACELEVEL = 0;
    protected static PrintStream stream = null;
    protected static int callDepth = 0;

    /**
	 * Initialization.
	 */
    public static void initStream(PrintStream s) {
        stream = s;
    }

    protected static void traceEntry(String str, Object o) {
        if (TRACELEVEL == 0) return;
        if (TRACELEVEL == 2) callDepth++;
        printEntering(str + ": " + o.toString());
    }

    protected static void traceExit(String str, Object o) {
        if (TRACELEVEL == 0) return;
        printExiting(str + ": " + o.toString());
        if (TRACELEVEL == 2) callDepth--;
    }

    private static void printEntering(String str) {
        printIndent();
        stream.println("--> " + str);
    }

    private static void printExiting(String str) {
        printIndent();
        stream.println("<-- " + str);
    }


    private static void printIndent() {
        for (int i = 0; i < callDepth; i++)
            stream.print("  ");
    }


    /*
	 * Crosscut part
	 */

    /**
	 * Application classes - left unspecified.
	 */
    abstract pointcut myClass(Object obj);
    /**
	 * The constructors in those classes.
	 */
    pointcut myConstructor(Object obj): myClass(obj) && execution(new(..));
    /**
	 * The methods of those classes.
	 */
    // toString is called from within our advice, so we shouldn't
    // advise its executions. But if toString is overridden, even
    // this might not be enough, so we might want
    //    && !cflow(execution(String toString()))
    pointcut myMethod(Object obj): myClass(obj) && 
        execution(* *(..)) && !execution(String toString());

    before(Object obj): myConstructor(obj) {
          traceEntry("" + thisJoinPointStaticPart.getSignature(), obj);
                
    }
    
    after(Object obj): myConstructor(obj) {
             traceExit("" + thisJoinPointStaticPart.getSignature(), obj);

    }

    before(Object obj): myMethod(obj) {
     
     traceEntry("" + thisJoinPointStaticPart.getSignature(), obj);
          
    }
    after(Object obj): myMethod(obj) {
     traceExit("" + thisJoinPointStaticPart.getSignature(), obj);
    }
    
}


Then, when I launch this toy application, the only output is something like 
that:
  --> tracing.TwoDShape(double, double): Circle radius = 0.0 @ (0.0, 0.0) 
  <-- tracing.TwoDShape(double, double): Circle radius = 0.0 @ (3.0, 3.0) 
  --> tracing.Circle(double, double, double): Circle radius = 0.0 @ (3.0, 3.0) 
  <-- tracing.Circle(double, double, double): Circle radius = 2.0 @ (3.0, 3.0) 
  --> tracing.TwoDShape(double, double): Circle radius = 0.0 @ (0.0, 0.0) 
  <-- tracing.TwoDShape(double, double): Circle radius = 0.0 @ (0.0, 0.0) 
  --> tracing.Circle(double, double, double): Circle radius = 0.0 @ (0.0, 0.0) 
  <-- tracing.Circle(double, double, double): Circle radius = 4.0 @ (0.0, 0.0) 
  --> tracing.Circle(double): Circle radius = 4.0 @ (0.0, 0.0) 
  <-- tracing.Circle(double): Circle radius = 4.0 @ (0.0, 0.0) 
  --> double tracing.Circle.perimeter(): Circle radius = 2.0 @ (3.0, 3.0) 
  <-- double tracing.Circle.perimeter(): Circle radius = 2.0 @ (3.0, 3.0) 
c1.perimeter() = 12.566370614359172
  --> double tracing.Circle.area(): Circle radius = 2.0 @ (3.0, 3.0) 
  <-- double tracing.Circle.area(): Circle radius = 2.0 @ (3.0, 3.0) 
c1.area() = 12.566370614359172
  --> double tracing.TwoDShape.distance(TwoDShape): Circle radius = 4.0 @ 
(0.0, 0.0) 
    --> double tracing.TwoDShape.getX(): Circle radius = 2.0 @ (3.0, 3.0) 
    <-- double tracing.TwoDShape.getX(): Circle radius = 2.0 @ (3.0, 3.0) 
    --> double tracing.TwoDShape.getY(): Circle radius = 2.0 @ (3.0, 3.0) 
    <-- double tracing.TwoDShape.getY(): Circle radius = 2.0 @ (3.0, 3.0) 
  <-- double tracing.TwoDShape.distance(TwoDShape): Circle radius = 4.0 @ 
(0.0, 0.0) 
c2.distance(c1) = 4.242640687119285

which doesn't report anything related with the join points.....

where am I getting lost ?

thanks 
Valerio


> Does the variable thisEnclosingJoinPointStaticPart help?
i read the programming guide, but i didn't understand what that variable is 
for.

>
> Wes



Back to the top