AspectJ, aspect-oriented Java programming

Aspect J is an extension of Java to implement aspect-oriented programming (AOP), a technique that modulates cross-functional problems, in other words, allows the same process to be applied to different programs.

Aspect J has existed since 2001, and AOP has been developed by Palo Alto Research Center since 1994. Here the aggregate is not a class, but a concern that is divided among several classes. Problems can be properties, areas of interest in the system, and AOP describes their relationships and collects them into a program. Aspects encapsulate general behavior. multiple classes.
Aspects of the system can be inserted, modified, or deleted at compile time.

The difference between aspect and class can be expressed by example. Suppose user registration management is a class, then it consists of the methods used by each program that uses the package. But if user registration is a "concern," then it is mixed with other packages in each program that uses it. Like class, it can be replaced by other care. The concern provides for interaction with other processes, while the class knows only its attributes.

Using AspectJ allows you to significantly reduce the size of the program without any loss of performance and, accordingly, simplifies design. This improves modularity and code reusability. This is especially useful for debugging large projects.

Adding AspectJ to Java

Adding aspects to Java code follows the following principles:

  1. Java add-on: can be added to an existing program (and replace a bunch of code or add debug controls or processing elements).
  2. Connection points represent specific points in program execution, such as calling a method, creating an exception, and so on.
  3. A connection point can contain other connection points.
  4. A spot cut is a design that includes a set of connection points according to a specified criterion. For example, a set of methods in a batch at run time.
  5. A tip is code that runs before, after, or during a connection point.
  6. The ajc compiler generates the AspectJ source code, either the .java source code or the .class bytecode file.
  7. AspectJ code can be reused in other programs.

Sample code

This aspect concerns the execution of each method in Eclipse. One council executes some code before the call, and the other council executes some code after the call.

public aspect xxxx
{ 
 pointcut anyMethod(): 
execution(*org.eclipse..*(..)); 
  before(): anyMethod() 
{
     ... some code... 
 } 
  after(): anyMethod() 
{
     ... some code... 
 }
} 

Tools and Documentation