diff options
Diffstat (limited to 'docs/modules/ROOT/examples/tracing')
18 files changed, 1036 insertions, 0 deletions
diff --git a/docs/modules/ROOT/examples/tracing/Circle.java b/docs/modules/ROOT/examples/tracing/Circle.java new file mode 100644 index 000000000..76b73e20a --- /dev/null +++ b/docs/modules/ROOT/examples/tracing/Circle.java @@ -0,0 +1,71 @@ +/* + +Copyright (c) Xerox Corporation 1998-2002. All rights reserved. + +Use and copying of this software and preparation of derivative works based +upon this software are permitted. Any distribution of this software or +derivative works must comply with all applicable United States export control +laws. + +This software is made available AS IS, and Xerox Corporation makes no warranty +about the software, its performance or its conformity to any specification. + +|<--- this code is formatted to fit into 80 columns --->| +|<--- this code is formatted to fit into 80 columns --->| +|<--- this code is formatted to fit into 80 columns --->| + +*/ + +package tracing; + +/** + * + * Circle is a 2D shape. It extends the TwoDShape class with the radius + * variable, and it implements TwoDShape's abstract methods for + * correctly computing a circle's area and distance. + * + */ +public class Circle extends TwoDShape { + protected double r; // radius + + /* + * All sorts of constructors + */ + public Circle(double x, double y, double r) { + super(x, y); this.r = r; + } + + public Circle(double x, double y) { + this(x, y, 1.0); + } + + public Circle(double r) { + this(0.0, 0.0, r); + } + + public Circle() { + this(0.0, 0.0, 1.0); + } + + /** + * Returns the perimeter of this circle + */ + public double perimeter() { + return 2 * Math.PI * r; + } + + /** + * Returns the area of this circle + */ + public double area() { + return Math.PI * r*r; + } + + /** + * This method overrides the one in the superclass. It adds some + * circle-specific information. + */ + public String toString() { + return ("Circle radius = " + String.valueOf(r) + super.toString()); + } +} diff --git a/docs/modules/ROOT/examples/tracing/ExampleMain.java b/docs/modules/ROOT/examples/tracing/ExampleMain.java new file mode 100644 index 000000000..93cc465b7 --- /dev/null +++ b/docs/modules/ROOT/examples/tracing/ExampleMain.java @@ -0,0 +1,44 @@ +/* + +Copyright (c) Xerox Corporation 1998-2002. All rights reserved. + +Use and copying of this software and preparation of derivative works based +upon this software are permitted. Any distribution of this software or +derivative works must comply with all applicable United States export control +laws. + +This software is made available AS IS, and Xerox Corporation makes no warranty +about the software, its performance or its conformity to any specification. + +|<--- this code is formatted to fit into 80 columns --->| +|<--- this code is formatted to fit into 80 columns --->| +|<--- this code is formatted to fit into 80 columns --->| + +*/ + +package tracing; + +/** + * + * A main function for testing 2D shapes. + * + */ +public class ExampleMain { + public static void main(String[] args) { + Circle c1 = new Circle(3.0, 3.0, 2.0); + Circle c2 = new Circle(4.0); + + Square s1 = new Square(1.0, 2.0); + + System.out.println("c1.perimeter() = " + c1.perimeter()); + System.out.println("c1.area() = " + c1.area()); + + System.out.println("s1.perimeter() = " + s1.perimeter()); + System.out.println("s1.area() = " + s1.area()); + + System.out.println("c2.distance(c1) = " + c2.distance(c1)); + System.out.println("s1.distance(c1) = " + s1.distance(c1)); + + System.out.println("s1.toString(): " + s1.toString()); + } +} diff --git a/docs/modules/ROOT/examples/tracing/README b/docs/modules/ROOT/examples/tracing/README new file mode 100644 index 000000000..bf8e70390 --- /dev/null +++ b/docs/modules/ROOT/examples/tracing/README @@ -0,0 +1,32 @@ + +This directory contains several examples of tracing aspects, +including a reusable tracing library and examples of +using that library. + +A lesson in the AspectJ Primer explains all of this code. + +To work with these (or any other examples), first be sure .../examples +is on your classpath, where ... is where you have installed AspectJ. + + +--To compile and run the example without tracing-- + + ajc @.../examples/tracing/notrace.lst + + java tracing.ExampleMain + + +--To compile and run the example with tracing version<N>-- + + ajc @.../examples/tracing/tracev<N>.lst + + java tracing.version<N>.TraceMyClasses + +where <N> is 1, 2, 3 or 4 + +--To use the tracing.lib.AbstractTrace aspect-- + + Make sure .../examples is in your classpath. + + In order to use this aspect, please read the documentation under + tracing/doc diff --git a/docs/modules/ROOT/examples/tracing/Square.java b/docs/modules/ROOT/examples/tracing/Square.java new file mode 100644 index 000000000..78b36882d --- /dev/null +++ b/docs/modules/ROOT/examples/tracing/Square.java @@ -0,0 +1,71 @@ +/* + +Copyright (c) Xerox Corporation 1998-2002. All rights reserved. + +Use and copying of this software and preparation of derivative works based +upon this software are permitted. Any distribution of this software or +derivative works must comply with all applicable United States export control +laws. + +This software is made available AS IS, and Xerox Corporation makes no warranty +about the software, its performance or its conformity to any specification. + +|<--- this code is formatted to fit into 80 columns --->| +|<--- this code is formatted to fit into 80 columns --->| +|<--- this code is formatted to fit into 80 columns --->| + +*/ + +package tracing; + +/** + * + * Square is a 2D shape. It extends the TwoDShape class with the side + * variable, and it implements TwoDShape's abstract methods for + * correctly computing a square's area and distance. + * + */ +public class Square extends TwoDShape { + protected double s; // side + + /* + * All sorts of constructors + */ + public Square(double x, double y, double s) { + super(x, y); this.s = s; + } + + public Square(double x, double y) { + this(x, y, 1.0); + } + + public Square(double s) { + this(0.0, 0.0, s); + } + + public Square() { + this(0.0, 0.0, 1.0); + } + + /** + * Returns the perimeter of this square + */ + public double perimeter() { + return 4 * s; + } + + /** + * Returns the area of this square + */ + public double area() { + return s*s; + } + + /** + * This method overrides the one in the superclass. It adds some + * circle-specific information. + */ + public String toString() { + return ("Square side = " + String.valueOf(s) + super.toString()); + } +} diff --git a/docs/modules/ROOT/examples/tracing/TwoDShape.java b/docs/modules/ROOT/examples/tracing/TwoDShape.java new file mode 100644 index 000000000..531b6f120 --- /dev/null +++ b/docs/modules/ROOT/examples/tracing/TwoDShape.java @@ -0,0 +1,69 @@ +/* +Copyright (c) Xerox Corporation 1998-2002. All rights reserved. + +Use and copying of this software and preparation of derivative works based +upon this software are permitted. Any distribution of this software or +derivative works must comply with all applicable United States export control +laws. + +This software is made available AS IS, and Xerox Corporation makes no warranty +about the software, its performance or its conformity to any specification. +*/ + +package tracing; + +/** + * TwoDShape is an abstract class that defines generic functionality + * for 2D shapes. + */ +public abstract class TwoDShape { + /** + * Coordinates of the center of the shape. + */ + protected double x, y; + + protected TwoDShape(double x, double y) { + this.x = x; this.y = y; + } + + /** + * Returns the x coordinate of the shape. + */ + public double getX() { return x; } + + /** + * Returns the y coordinate of the shape. + */ + public double getY() { return y; } + + /** + * Returns the distance between this shape and the shape given as + * parameter. + */ + public double distance(TwoDShape s) { + double dx = Math.abs(s.getX() - x); + double dy = Math.abs(s.getY() - y); + return Math.sqrt(dx*dx + dy*dy); + } + + /** + * Returns the perimeter of this shape. Must be defined in + * subclasses. + */ + public abstract double perimeter(); + + /** + * Returns the area of this shape. Must be defined in + * subclasses. + */ + public abstract double area(); + + /** + * Returns a string representation of 2D shapes -- simply its + * coordinates. + */ + public String toString() { + return (" @ (" + String.valueOf(x) + ", " + String.valueOf(y) + ") "); + } +} + diff --git a/docs/modules/ROOT/examples/tracing/lib/AbstractTrace.java b/docs/modules/ROOT/examples/tracing/lib/AbstractTrace.java new file mode 100644 index 000000000..8c67b12c3 --- /dev/null +++ b/docs/modules/ROOT/examples/tracing/lib/AbstractTrace.java @@ -0,0 +1,185 @@ +/* + +Copyright (c) Xerox Corporation 1998-2002. All rights reserved. + +Use and copying of this software and preparation of derivative works based +upon this software are permitted. Any distribution of this software or +derivative works must comply with all applicable United States export control +laws. + +This software is made available AS IS, and Xerox Corporation makes no warranty +about the software, its performance or its conformity to any specification. + +|<--- this code is formatted to fit into 80 columns --->| +|<--- this code is formatted to fit into 80 columns --->| +|<--- this code is formatted to fit into 80 columns --->| + +*/ + +package tracing.lib; + +import java.io.PrintStream; +import org.aspectj.lang.JoinPoint; + + +/** + * This class provides support for printing trace messages into a stream. + * The trace messages consist of the class name, method name (if method) + * and the list of parameter types.<P> + * The class is thread-safe. Different threads may use different output streams + * by simply calling the method initStream(myStream).<P> + * This class should be extended. + * It defines 3 abstract crosscuts for injecting the tracing functionality + * into any constructors and methods of any application classes.<P> + * + * One example of using this class might be + * <PRE> + * import tracing.lib.AbstractTrace; + * aspect TraceMyClasses extends AbstractTrace of eachJVM() { + * pointcut classes(): within(TwoDShape) | within(Circle) | within(Square); + * pointcut constructors(): executions(new(..)); + * pointcut methods(): executions(!abstract * *(..)) + * } + * </PRE> + * (Make sure .../aspectj/examples is in your classpath) + */ +public abstract aspect AbstractTrace { + + /** + * Application classes - left unspecified. + * Subclasses should concretize this crosscut with class names. + */ + abstract pointcut classes(); + /** + * Constructors - left unspecified. + * Subclasses should concretize this crosscut with constructors. + */ + abstract pointcut constructors(); + /** + * Methods - left unspecified. + * Subclasses should concretize this crosscut with method names. + */ + abstract pointcut methods(); + + before(): classes() && constructors() { + doTraceEntry(thisJoinPoint, true); + } + after(): classes() && constructors() { + doTraceExit(thisJoinPoint, true); + } + + before(): classes() && methods() { + doTraceEntry(thisJoinPoint, false); + } + after(): classes() && methods() { + doTraceExit(thisJoinPoint, false); + } + + /* + * From here on, it's an ordinary class implementation. + * The static state is thread-safe by using ThreadLocal variables. + */ + + /** + * This method initializes this thread's trace output stream. + * By default, the output stream is System.err, and it is the same for + * all threads. In multithreaded applications, you may want to define + * different output streams for the different threads. For doing it, + * simply call this method in the beginning of each thread's main loop, + * giving it different output streams. + */ + public void initStream(PrintStream _stream) { + setStream(_stream); + } + + + private ThreadLocal stream = new ThreadLocal() { + protected Object initialValue() { + return System.err; + } + }; + private ThreadLocal callDepth = new ThreadLocal() { + protected Object initialValue() { + return new Integer(0); + } + }; + + private PrintStream getStream() { + return (PrintStream)stream.get(); + } + private void setStream(PrintStream s) { + stream.set(s); + } + private int getCallDepth() { + return ((Integer)(callDepth.get())).intValue(); + } + private void setCallDepth(int n) { + callDepth.set(new Integer(n)); + } + + private void doTraceEntry (JoinPoint jp, boolean isConstructor) { + setCallDepth(getCallDepth() + 1); + printEntering(jp, isConstructor); + } + + private void doTraceExit (JoinPoint jp, boolean isConstructor) { + printExiting(jp, isConstructor); + setCallDepth(getCallDepth() - 1); + } + + private void printEntering (JoinPoint jp, boolean isConstructor) { + printIndent(); + getStream().print("--> "); + getStream().print(jp); + // printParameterTypes(jp); + getStream().println(); + } + + private void printExiting (JoinPoint jp, boolean isConstructor) { + printIndent(); + getStream().print("<-- "); + getStream().print(jp); + // printParameterTypes(jp); + getStream().println(); + } + +// private void printParameterTypes(JoinPoint jp) { +// Class[] ptypes = jp.parameterTypes; + +// getStream().print("("); +// for (int i = 0; i < ptypes.length; i++) { +// getStream().print(ptypes[i].getName()); +// if (i < ptypes.length - 1) getStream().print(", "); +// } +// getStream().print(")"); +// } + + private void printIndent() { + for (int i = 0; i < getCallDepth(); i++) + getStream().print(" "); + } + + /** + * This method is not being used. + * It's being included solely for illustrating how to access and use + * the information in JoinPoint. + * If you want, you can replace the calls to printParameterTypes (above) + * by calls to this method. + */ +// private void printParameters(JoinPoint jp) { +// Class[] ptypes = jp.parameterTypes; +// String[] pnames = jp.parameterNames; +// Object[] params = jp.parameters; + +// getStream().print("("); +// for (int i = 0; i < ptypes.length; i++) { +// getStream().print(ptypes[i].getName() + " " + +// pnames[i] + "=" + +// params[i]); +// if (i < ptypes.length - 1) getStream().print(", "); +// } +// getStream().print(")"); +// } + +} + diff --git a/docs/modules/ROOT/examples/tracing/lib/TraceMyClasses.java b/docs/modules/ROOT/examples/tracing/lib/TraceMyClasses.java new file mode 100644 index 000000000..95c3a859c --- /dev/null +++ b/docs/modules/ROOT/examples/tracing/lib/TraceMyClasses.java @@ -0,0 +1,67 @@ +/* + +Copyright (c) Xerox Corporation 1998-2002. All rights reserved. + +Use and copying of this software and preparation of derivative works based +upon this software are permitted. Any distribution of this software or +derivative works must comply with all applicable United States export control +laws. + +This software is made available AS IS, and Xerox Corporation makes no warranty +about the software, its performance or its conformity to any specification. + +|<--- this code is formatted to fit into 80 columns --->| +|<--- this code is formatted to fit into 80 columns --->| +|<--- this code is formatted to fit into 80 columns --->| + +*/ + +package tracing.lib; + +import tracing.TwoDShape; +import tracing.Circle; +import tracing.Square; +import tracing.ExampleMain; + +import java.io.FileOutputStream; +import java.io.PrintStream; +import java.io.FileNotFoundException; + +aspect TraceMyClasses extends AbstractTrace { + /** + * The application classes + */ + pointcut classes(): within(TwoDShape) || within(Circle) || within(Square); + /** + * The constructors in those classes - but only the ones with 3 + * arguments. + */ + pointcut constructors(): execution(new(double, double, double)); + /** + * This specifies all the message executions. + */ + pointcut methods(): execution(* *(..)); + + /** + * A main function for testing the trace aspect. + */ + public static void main(String[] _args) { + final String[] args = _args; + new Thread() { + public void run() { + TraceMyClasses.aspectOf().initStream(System.err); + ExampleMain.main(args); + } + }.start(); + + new Thread() { + public void run() { + try { + TraceMyClasses.aspectOf().initStream(new PrintStream(new FileOutputStream("AJTRACETEST"))); + } + catch (FileNotFoundException e) {} + ExampleMain.main(args); + } + }.start(); + } +} diff --git a/docs/modules/ROOT/examples/tracing/notrace.lst b/docs/modules/ROOT/examples/tracing/notrace.lst new file mode 100644 index 000000000..ebd820352 --- /dev/null +++ b/docs/modules/ROOT/examples/tracing/notrace.lst @@ -0,0 +1,4 @@ +TwoDShape.java +Circle.java +Square.java +ExampleMain.java diff --git a/docs/modules/ROOT/examples/tracing/tracelib.lst b/docs/modules/ROOT/examples/tracing/tracelib.lst new file mode 100644 index 000000000..a9a494d10 --- /dev/null +++ b/docs/modules/ROOT/examples/tracing/tracelib.lst @@ -0,0 +1,6 @@ +TwoDShape.java +Circle.java +Square.java +ExampleMain.java +lib/AbstractTrace.java +lib/TraceMyClasses.java diff --git a/docs/modules/ROOT/examples/tracing/tracev1.lst b/docs/modules/ROOT/examples/tracing/tracev1.lst new file mode 100644 index 000000000..35a95c46f --- /dev/null +++ b/docs/modules/ROOT/examples/tracing/tracev1.lst @@ -0,0 +1,6 @@ +TwoDShape.java +Circle.java +Square.java +ExampleMain.java +version1/Trace.java +version1/TraceMyClasses.java diff --git a/docs/modules/ROOT/examples/tracing/tracev2.lst b/docs/modules/ROOT/examples/tracing/tracev2.lst new file mode 100644 index 000000000..e37fe1a11 --- /dev/null +++ b/docs/modules/ROOT/examples/tracing/tracev2.lst @@ -0,0 +1,6 @@ +TwoDShape.java +Circle.java +Square.java +ExampleMain.java +version2/Trace.java +version2/TraceMyClasses.java diff --git a/docs/modules/ROOT/examples/tracing/tracev3.lst b/docs/modules/ROOT/examples/tracing/tracev3.lst new file mode 100644 index 000000000..2804f306d --- /dev/null +++ b/docs/modules/ROOT/examples/tracing/tracev3.lst @@ -0,0 +1,6 @@ +TwoDShape.java +Circle.java +Square.java +ExampleMain.java +version3/Trace.java +version3/TraceMyClasses.java diff --git a/docs/modules/ROOT/examples/tracing/version1/Trace.java b/docs/modules/ROOT/examples/tracing/version1/Trace.java new file mode 100644 index 000000000..97b5edb3f --- /dev/null +++ b/docs/modules/ROOT/examples/tracing/version1/Trace.java @@ -0,0 +1,77 @@ +/* +Copyright (c) Xerox Corporation 1998-2002. All rights reserved. + +Use and copying of this software and preparation of derivative works based +upon this software are permitted. Any distribution of this software or +derivative works must comply with all applicable United States export control +laws. + +This software is made available AS IS, and Xerox Corporation makes no warranty +about the software, its performance or its conformity to any specification. +*/ + +package tracing.version1; + +import java.io.PrintStream; + +/** + * + * This class provides some basic functionality for printing trace messages + * into a stream. + * + */ +public class Trace { + /** + * 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; + } + + /** + * Prints an "entering" message. It is intended to be called in the + * beginning of the blocks to be traced. + */ + public static void traceEntry(String str) { + if (TRACELEVEL == 0) return; + if (TRACELEVEL == 2) callDepth++; + printEntering(str); + } + + /** + * Prints an "exiting" message. It is intended to be called in the + * end of the blocks to be traced. + */ + public static void traceExit(String str) { + if (TRACELEVEL == 0) return; + printExiting(str); + 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(" "); + } +} diff --git a/docs/modules/ROOT/examples/tracing/version1/TraceMyClasses.java b/docs/modules/ROOT/examples/tracing/version1/TraceMyClasses.java new file mode 100644 index 000000000..736e96413 --- /dev/null +++ b/docs/modules/ROOT/examples/tracing/version1/TraceMyClasses.java @@ -0,0 +1,69 @@ +/* +Copyright (c) Xerox Corporation 1998-2002. All rights reserved. + +Use and copying of this software and preparation of derivative works based +upon this software are permitted. Any distribution of this software or +derivative works must comply with all applicable United States export control +laws. + +This software is made available AS IS, and Xerox Corporation makes no warranty +about the software, its performance or its conformity to any specification. +*/ + +package tracing.version1; + +/** + * + * This class connects the tracing functions in the Trace class with + * the constructors and methods in the application classes. + * + */ +import tracing.TwoDShape; +import tracing.Circle; +import tracing.Square; +import tracing.ExampleMain; + +aspect TraceMyClasses { + /** + * Application classes. + */ + pointcut myClass(): within(TwoDShape) || within(Circle) || within(Square); + /** + * The constructors in those classes. + */ + pointcut myConstructor(): myClass() && execution(new(..)); + /** + * The methods of those classes. + */ + pointcut myMethod(): myClass() && execution(* *(..)); + + /** + * Prints trace messages before and after executing constructors. + */ + before (): myConstructor() { + Trace.traceEntry("" + thisJoinPointStaticPart.getSignature()); + } + after(): myConstructor() { + Trace.traceExit("" + thisJoinPointStaticPart.getSignature()); + } + + /** + * Prints trace messages before and after executing methods. + */ + before (): myMethod() { + Trace.traceEntry("" + thisJoinPointStaticPart.getSignature()); + } + after(): myMethod() { + Trace.traceExit("" + thisJoinPointStaticPart.getSignature()); + } + + /** + * A main function for testing the trace aspect. + */ + public static void main(String[] args) { + Trace.TRACELEVEL = 2; + Trace.initStream(System.err); + ExampleMain.main(args); + } +} + diff --git a/docs/modules/ROOT/examples/tracing/version2/Trace.java b/docs/modules/ROOT/examples/tracing/version2/Trace.java new file mode 100644 index 000000000..6c43d60f8 --- /dev/null +++ b/docs/modules/ROOT/examples/tracing/version2/Trace.java @@ -0,0 +1,117 @@ +/* +Copyright (c) Xerox Corporation 1998-2002. All rights reserved. + +Use and copying of this software and preparation of derivative works based +upon this software are permitted. Any distribution of this software or +derivative works must comply with all applicable United States export control +laws. + +This software is made available AS IS, and Xerox Corporation makes no warranty +about the software, its performance or its conformity to any specification. +*/ + +package tracing.version2; + +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. + * It defines one abstract crosscut for injecting that tracing functionality + * into any application classes. + * To use it, provide a subclass that concretizes the abstract crosscut. + */ +abstract aspect Trace { + + /* + * 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 = 2; + protected static PrintStream stream = System.err; + protected static int callDepth = 0; + + /** + * Initialization. + */ + public static void initStream(PrintStream s) { + stream = s; + } + + protected static void traceEntry(String str) { + if (TRACELEVEL == 0) return; + if (TRACELEVEL == 2) callDepth++; + printEntering(str); + } + + protected static void traceExit(String str) { + if (TRACELEVEL == 0) return; + printExiting(str); + 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. + * Subclasses should concretize this pointcut with class names. + */ + abstract pointcut myClass(); + /** + * The constructors in those classes. + */ + pointcut myConstructor(): myClass() && execution(new(..)); + /** + * The methods of those classes. + */ + pointcut myMethod(): myClass() && execution(* *(..)); + + /** + * Prints trace messages before and after executing constructors. + */ + before(): myConstructor() { + traceEntry("" + thisJoinPointStaticPart.getSignature()); + } + after(): myConstructor() { + traceExit("" + thisJoinPointStaticPart.getSignature()); + } + + /** + * Prints trace messages before and after executing methods. + */ + before(): myMethod() { + traceEntry("" + thisJoinPointStaticPart.getSignature()); + } + after(): myMethod() { + traceExit("" + thisJoinPointStaticPart.getSignature()); + } +} diff --git a/docs/modules/ROOT/examples/tracing/version2/TraceMyClasses.java b/docs/modules/ROOT/examples/tracing/version2/TraceMyClasses.java new file mode 100644 index 000000000..d9ba21c54 --- /dev/null +++ b/docs/modules/ROOT/examples/tracing/version2/TraceMyClasses.java @@ -0,0 +1,37 @@ +/* +Copyright (c) Xerox Corporation 1998-2002. All rights reserved. + +Use and copying of this software and preparation of derivative works based +upon this software are permitted. Any distribution of this software or +derivative works must comply with all applicable United States export control +laws. + +This software is made available AS IS, and Xerox Corporation makes no warranty +about the software, its performance or its conformity to any specification. +*/ + +package tracing.version2; + +import tracing.TwoDShape; +import tracing.Circle; +import tracing.Square; +import tracing.ExampleMain; + +/** + * + * This class concretizes the abstract crosscut in Trace, + * applying the trace facility to these application classes. + * + */ +public aspect TraceMyClasses extends Trace { + pointcut myClass(): within(TwoDShape) || within(Circle) || within(Square); + + /** + * A main function for testing the trace aspect. + */ + public static void main(String[] args) { + Trace.TRACELEVEL = 2; + Trace.initStream(System.err); + ExampleMain.main(args); + } +} diff --git a/docs/modules/ROOT/examples/tracing/version3/Trace.java b/docs/modules/ROOT/examples/tracing/version3/Trace.java new file mode 100644 index 000000000..8fc9d8613 --- /dev/null +++ b/docs/modules/ROOT/examples/tracing/version3/Trace.java @@ -0,0 +1,123 @@ +/* + +Copyright (c) Xerox Corporation 1998-2002. All rights reserved. + +Use and copying of this software and preparation of derivative works based +upon this software are permitted. Any distribution of this software or +derivative works must comply with all applicable United States export control +laws. + +This software is made available AS IS, and Xerox Corporation makes no warranty +about the software, its performance or its conformity to any specification. + +|<--- this code is formatted to fit into 80 columns --->| +|<--- this code is formatted to fit into 80 columns --->| +|<--- this code is formatted to fit into 80 columns --->| + +*/ + +package tracing.version3; + +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 Trace { + + /* + * 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); + } +} diff --git a/docs/modules/ROOT/examples/tracing/version3/TraceMyClasses.java b/docs/modules/ROOT/examples/tracing/version3/TraceMyClasses.java new file mode 100644 index 000000000..c986d2615 --- /dev/null +++ b/docs/modules/ROOT/examples/tracing/version3/TraceMyClasses.java @@ -0,0 +1,46 @@ +/* + +Copyright (c) Xerox Corporation 1998-2002. All rights reserved. + +Use and copying of this software and preparation of derivative works based +upon this software are permitted. Any distribution of this software or +derivative works must comply with all applicable United States export control +laws. + +This software is made available AS IS, and Xerox Corporation makes no warranty +about the software, its performance or its conformity to any specification. + +|<--- this code is formatted to fit into 80 columns --->| +|<--- this code is formatted to fit into 80 columns --->| +|<--- this code is formatted to fit into 80 columns --->| + +*/ + +package tracing.version3; + +import tracing.TwoDShape; +import tracing.Circle; +import tracing.Square; +import tracing.ExampleMain; + +/** + * + * This class concretizes the abstract crosscut in Trace, + * applying the trace facility to these application classes. + * + */ +public aspect TraceMyClasses extends Trace { + pointcut myClass(Object obj): + this(obj) && + (within(TwoDShape) || within(Circle) || within(Square)); + + /** + * A main function for testing the trace aspect. + */ + public static void main(String[] args) { + Trace.TRACELEVEL = 2; + Trace.initStream(System.err); + ExampleMain.main(args); + } +} + |