Chapter II Aspectj Language
ADVICE
Notification defines several implementations to facilitate execution in a specific program period. These specific sections can be given using anonymous cleavage point using the named cleaning point. Below is an example of a notification using a naming point:
Pointcut Setter (Point P1, Int NewVal): Target (P1) && Args (NewVal)
(Call (Void Setx (Int) ||
Call (Void SetY (int));
Before (Point P1, Int NewVal): Setter (P1, NewVal) {
System.out.println ("About to Set Something In" P1
"To the new value" newval);
}
The same notice can be achieved using anonymous clearance point, as follows
Before (Point P1, Int Newval): Target (P1) && Args (NewVal)
(Call (void setx (int)) ||
Call (void sety)) {
System.out.println ("About to Set Something In" P1
"To the new value" newval);
}
Here are some different notifications
This Before notification is running before the anonymous clearance captures the connection point.
Before (POINT P, INT X): Target (P) && args (x) && call (void setx (int)) {
IF (! p.ASSERTX (x)) Return;
}
This After notification runs after the anonymous cleansing points, whether the program is normal returns or throws an exception.
AfTER (POINT P, INT X): Target (p) && args (x) && call (void setx (int)) {
IF (! p.ASSERTX (x)) throw new postconditionviological ();
}
The following AFTER is running after the anonymous clearance is captured and only in the case where the program is returned.
After (POINT P) Returning (INT X): Target (p) && call (int getX ()) {
System.out.println ("Returning Int Value" X "for P =" P);
}
The following AFTER runs after the anonymous clearance is captured and the program is abnormal.
After () THROWING (Exception E): Target (Point) && call (Void SetX (int)) {
System.out.println (e);
}
The Around Notification can replace the captured connection point and perform its own logic, and the original logic can be executed by calling a specific method Proceed.
Void Around (POINT P, INT X): Target (P)
&& args (x)
&& call (void setx (int)) {
IF (P.Assertx (x)) Proceed (P, X);
p.releaseResources ();
}
Type Declaration (Inter-Type Declarations)
Aspects can declare other types of members (fields, methods, and constructors). These are called type members. Aspects can also declare additional types of new interfaces or expand a new class. There are some examples of such statements here.
The following statement has a Boolean type field named DISABLED that each Server object is FALSE:
Private boolean server.disabled = false;
Here is a private field that can only access this field. Even if the Server object itself has another private field disabled (defined in the server or other aspect), it does not generate a name conflict because there is no difference in disabled references.
The following code segment declares that each Point object has a method called getx, which returns the value of each x variable:
Public int point.getx () {returnim. xi}
Inside the method, THIS is the currently executed POINT object. Because the method is declared as a public method, any code can call it, but if there is another point.getx () declaration, then the conflict of the compile period will be generated.
The following public declaration defines a constructor of the Point object, which has two integer parameters:
Public Point.new (int x, int y) {this.x = x; this.y = y;
Below is a public field declaration:
Public INT POINT.X = 0;
It declares an X public field for the Point object and initials to 0 because the field is public, and it can be accessed anywhere, so if there is another X field, a conflict occurs.
The following is declared with the interface of the Point class Comparable
Declare PARENTS: Point Implement Comparable;
Of course, there will be an error unless the Point class implements the interface.
The following is a Point class declare its extended geometricObject.
Declare Parents: Point Extends GeometricObject;
One aspect can have multiple types of declarations. For example, the following statement.
Public string point.name;
Public void point.setname (string name) {this.name = name;}
Types include only one target type, but usually you may want to declare the same members on multiple types. This can be implemented through a joint type of members and a private interface.
Aspect a {
PRIVATE INTERFACE HASNAME {]
DECLARE PARENTS: (Point || Line || Square) Implements Hasname
Private string hasname.name;
Public string hasname.getname () {return name;}
}
Here is a HASNAME interface and declare that Point, Line or Square implements this interface. Moreover, the private field Name and the public method GetName are declared for the interface.
Scope of type variables
AspectJ allows private, package protection (default), and public types. Private means that there is a private relationship with the aspect, and the target object is not related to the target object (ie, the target object does not know the existence of variables or methods). Therefore, if one aspect makes a field of private type declarations
Private int foo.x;
The code in the aspect can access the X field of the Foo, other classes or aspects. Similarly, if one aspect makes a package protection type declaration
Int foo.;
Then you can access it in any code in the package, and the code outside the package is free to access.
Example: Pointassertions
This example includes a class and one aspect. Aspect, declare the private assertion method assertx and asserty for Point. The call to the SETX and SETY methods is provided by using these two assertion methods. The assertion method declares is that it is because it is necessary to use them elsewhere, and only these methods can be used inside. Class point {
INT X, Y;
Public void setx (int x) {this.x = x;}
Public void sety {this.y = y;
Public static void main (String [] args) {
Point P = new point ();
P.SETX (3);
P.SETY (333); // illegal Y value
}
}
ask POINTASSERTION {
// If the value of X or Y is not between 0 and 100, it is considered illegal.
Private Boolean Point.assertX (int x) {
Return (x <= 100 && x> = 0);
}
Private Boolean Point.Asserty (int y) {
Return (Y <= 100 && y> = 0);
}
Before (POINT P, INT X): Target (P) && args (x) && call (void setx (int)) {
IF (! p.Assertx (x)) {// If illegally entered X, output prompt information
System.out.Println ("Illegal Value for X"); Return;
}
}
Before (POINT P, INT Y): Target (p) && args (y) && call (void setY (int)) {
If (! p.Asserty (y)) {// If you illegally enter Y, then output prompt information
System.out.println ("Illegal Value for Y"; Return;
}
}
}
thisjoinPoint
AspectJ provides a special reference variable, thisjoinPoint, which contains information at the current connection point and can be notified. ThisjoinPoint variables can only be used in the notification environment, just like this only used in non-static methods and constructive functions. In the notification, thisjoinPoint is the variable of the org.aspectj.lang.joinPoint type. A simple role using it is to output it directly. Like other Java objects, thisjoinPoint has a toString () method simplifies the formatted output:
Class tracenonStaticMethods {
Before (POINT P): Target (p) && call (* * (..)) {
System.out.println ("Entering" THISJOINPOINT "IN" P);
}
}
ThisjoinPoint can be used to access static and dynamic information, such as parameters, etc .:
ThisjoinPoint.getargs ();
In addition, it holds an object including all static information, which can be referenced by the following method:
ThisjoinPoint.getStaticPart ();
If you only need static information at the connection point, you may access the static part of the connection point directly using the variable thisjoinPointStaticPart. Use it to avoid running the connection point object directly when running. usually
thisjoinPointStaticPart == thisjoinpoint.getStaticPart ()
ThisjoinPoint.getkind () == thisjoinPointStaticPart.getkind ()
ThisjoinPoint.get.getsignature () == thisjoinPointStaticPart.getsignature ()
thisjoinPoint.getsource () == thisjoinPointStaticPart.getsourceLocation ()
There is also a correlation variable: ThisenClosingJoinPointStaticPart. It is similar to thisjoinPointStaticPart, using it to print the caller's location, for example
Before (): execution (* * (.)) {
System.err.println (ThisenClosingjoinPointStaticPart.getsource ())
}
Guide
This series will use Aspectj to implement some specific examples so that the reader can strengthen the understanding of AspectJ and familiar with various syntax.
More information
1. AspectJ Installation and Configuration Guide 2. To learn aspectj (a) If you need to pay, please write a name author and source.