Overseal-oriented programming in Sun [TM] One Studio
Summary
Aspect, makes we cross-cutting module code. In a typical object-oriented application, such code has public purposes, but is implemented in the scattered code, the Aspectj of the Palo Alto Research Center, the Xerox Palo Alto A group of enthusiasm and experienced users and developers developed an open source project, and he provides a simple extension for Java language, thereby implementing aspects in Java, in which in this article, the author through Sun [ TM] ONE Studio's AspectJ module uses a simple JavaBean [TM] example to demonstrate aspect programming.
Author: Vaughn Spurlin
What is aspects
Aspect is the programming language concept, similar to class, but it is a higher level of abstraction, very cleanly packaged a class of attention, such concerns may affect many classes but it is difficult or impossible to extract into a single alone the type. A simple example is that the method is called when the information is called. You do not have to find a huge code body manual insertion information record code to many classes, aspects will use wildcard generation to describe the insertion point, plus a simple record information code. In a general sense, the AspectJ compiler is not a source code pre-processor, and does not directly insert a Java code to source code, but the compilation is generated, and the byte size is woven, not the source code level. .
Aspects are different from the class, take time to learn how to effectively create and use, just take time to learn how to create and use classes, fortunately, for a developer with object-oriented experience, simple face Aspects can quickly learn and return immediately.
Recognizing that all values of aspects need to have a very deep understanding of it, you can get a progressive way, initial, you can add some temporary aspects to help development, test and debug, follow, aspects will be used as in the standard Java code. The lasting code is added to the source code, making the source code more simply, cleaner, more modified, more easily understood, safer modifications.
Aspectj is a development tool that expands the Java language, incorporates aspect performance, an AspectJ program to remove the definition, can translate as a valid Java program, compile the AspectJ source code to generate standards that can run in any effective Java virtual machine Java bytecode. Aspectj has its own compiler, but it does not provide a complete AspectJ environment, but generates an executable bytecode with the installed Java 2 compiler. This article will run through an example, introduce some facing performance, first we look at some basic definitions:
Join Point
The joint point is a point in the execution of the Java program, like the "insertion point" mentioned above, including the following:
§ Call - That point when the method is called
§ Operation - the point that starts executing after being called
§ Assignment - When a non-private domain is assigned
Pointcut
The entry point is essentially a syntax defined on a set of joint points. It can be named and used in terms of aspect, and the connection point is anonymous and never pointing separately, but only as a member of the entry point, a detailed Any joint point in the source code inside the source is not true.
Notice Declaration
The notification statement is composed of a "notification type" and the "notice" that is executable, and when the Java program control process is passed through the real joint point, the notice is executed, the notification type truly determines what is executed Time, for example, if the notification type is "after ()" and the cut point specifies the call to some methods, then the notification body is executed after those method calls returned, the notification body code to be executed is mentioned above. The section is inserted. Internal Type Member Declaration (Inter-Type Member Declaration)
The internal type member statement is a mechanism for increasing members from the outside, which makes it possible to increase some of the members and methods that increase some of the main purposes of the class.
Aspect
Aspects are the highest layer structure, which can include entry points, notification statements, internal type members declarations, and other effective Java-class members declarations, aspects and classes definitions at the same level, are the basic module units programmed for aspect.
Simple example
To install Aspectj below is not required, you can first browse the example, then if you like, you can install aspectj to compile and run an example. The examples contain the following files, click the file name to browse the source code in the browser window (or download its compressed demo-aspect.zip http://forte.sun.com/ffj/Articles/aspectJ/demo-aspect. ZIP).
Givegreeting.java - gets and shows greetings from Beans
Hellobean.java - Bean supporting "Hello" greetings
Goodbyebean.java - Bean supporting "Good-Bye" greetings
Logger.java - aspect of recording bean method calls
Counter.java - Aspects of statistics getGreeting method
NO_ASPECT.LST - Notes of AspectJ creation list
Logging.lst - A list of AspectJs with Logger
Counting.lst - ASPECTJ creation list with Logger and Counter
No need for example
Givegreeting.java, Hellobean.java and Goodbyebean.java are ordinary Java programs, if used in normal ways, do not need aspectj compilation, givegreeting.java can be executed and produced, and Figure 1 shows the result of GiveGreeting.
Figure 1: Output result without using
Create a list with no_aspect.lst to compile in AspectJ, the same result is generated because there is no contained file in NOSPECT.LST.
Examples with Logger
If the program is compiled by AspectJ with Logging.lst, logger.java is included, performing GiveGreeting generated as shown in Figure 2, this example is the first step of facing aspects, helping to develop with a temporary aspect.
Figure 2: Output results with logging
Note that each call for the GET and SET methods will trigger record information, and the method in the bean is called information, and will be recorded. Check if the Logger.java code (below) is to see how it works:
Aspect logger {PointCut log (): Execution (* * .getgreeting (.)) || Execution (* * .SETGREETITING (.)); before (): log () {system.out.println ("Logger: " Thisjoinpoint.get.get.getsignature ());}} First define an entry point of a name that is log, which makes it any way to start starting all joint points that are named getGreeting and setgreeting. These methods have any parameters, pay attention to wildcard The use of "*" is to specify the "" * "". "Is a wildcard for the method of accessing the modifier and class name, alternative method parameters, meaning any number of parameters. The entry point consists of below: Keywords: Pointcut
Removing point identifier; log () - makes the cut point can be used in any identifier in any required place.
Entry point declaration: Execution (* * .GETGREETITING (.)) || Execution (* * .SETGREETITING (.)); - For link points belonging to this point, the condition must be true
Next, a notification is declared, it is performed before any method call indicated by the log entry point, when the notification is executed, it will print the record information, including the method of triggering a notification. The notification consists of the following part:
Notification Type: Before () - This type of notification is performed before any joint point belonging to the entry point.
Entry point: log () - This is a naming entry point reference, which is also legal for use here.
Notice: {system.out.println ("Logger:" thisjoinpoint.get.GetSignature ());} - This notice print "Logger:" Add a method of meeting the entry point condition
Examples with count
If the program is compiled by AspectJ to build a list of counting.lst, Counter.java is added (see Figure 3), performing GiveGreeting as follows:
Figure 3: Output results with counting
This example uses the internal type member declaration, demonstrating the modularity of the cross-cut point, and is added to the source code as a lasting code.
In addition to recording information, the output now includes calculation information to tell the getGreeting method has been called. Let's take a look at the internal type members in Counter.java statement:
aspect Counter {private int HelloBean.countGetGreeting = 0; private int GoodbyeBean.countGetGreeting = 0; declare parents: GoodbyeBean extends HelloBean; pointcut count (HelloBean h): call (* * .getGreeting (..)) && target (h); After (Hellobean H): count (h) {h.countgetgreeting ; system.out.println ("counter:" h.countgetgreeting "calls to" thisjoinpoint.getsignature ());}}
Declaring the integer domain countgetgreeting in two beans, the GoodbyeBean is declared as Hellobean inheritance, and if the Extends statement is legally inherited in the Java class definition, then a parent-child relationship is also legal in AspectJ. Countgetgreetin declarations and parental relationships are examples of internal type members declared. The internal type member statement is a powerful modular cross-cutting point tool. In this example, all calculations on the number of getGreeting methods are included in the Counter. If there is no aspect, the countgetgreeting domain must be joined in the bean class definition. The calculated logic is mixed with the bean and the calculated logic, which is responsible for the calculated developer needs to find the calculation logic dispersed in several classes and non-calculated code mixed together.
The next internal type member statement is an entry point named count. It includes all the joint points when the name is getGreeting, and the method must be in the Hellobean class or the child class in Hellobean. It is a formal parameter, and Hellobean h indicates that it is only applied in an example of subclass of Hellobean class or Hellobean.
The largest code block in Counter is a notification, which is executed after the method call returns, and notifications first increase the countGeeting in the bean, then print a message, like the point count, notify us of Hellobean H, visit Hellobean Example.
Use aspectj in Sun [TM] One Studio
As we discussed a simple example, we showed the powerful and easy and easy installation of Java. The latest version of Aspectj is installed.
Ø Download and install aspectj
AspectJ can be downloaded from the AspectJ website download page (http://aspectj.org/servlets/ajsite?channel=downloadto), each download is packaged as a JAR file, and execute the JAR file will start installation.
Download and Install the parts you selected in the table below
Component
Comments
Ajde-fortemodule-1.0.6.jar needs
AspectJ Development Environment, Sun One Studio or NetBeans [TM] of FORTE [TM]
Aspectj-Tools-1.0.6.jar optional
Compiler and Core Tool - AspectJ compiler and command line debugger. It is not required to use AspectJ, compiler, and core tools in Sun One Studio, but it helps to use Aspectj outside Sun One Studio.
AspectJ-DOCS-1.0.6.jar or aspectj-docs-1.0.6.tgz two optional
Complete documentation, guides and examples. AspectJ documents can also be used online, local installation is just for convenience.
Note: When ajde-fortemodule-1.0.6.jar is executed to install an AspectJ development environment, the integrated development environment should not be running.
When the installer prompts to select the installation path, select the Modules path of the integrated development environment, install Aspectj, enable the Sun ONE Studio main window to add a toolbar and add a submenu in the Tools menu.
Ø Download and install example
Next, install the AspectJ example in Sun ONE Studio:
Download DEMO-ASPECT.ZIP (http://forte.sun.com/ffj/Articles/aspectJ/demo-aspect.zip)
Unzip to the working directory
Launch Sun One Studio
Set the directory containing the Greetings Java package as a work directory Ø Compile and implement example
On the AspectJ toolbar (see Figure 4), click on the first button (from the left), start the AspectJ development environment, click this button again at any time, stop or start the AspectJ development environment again.
Figure 4: Aspectj Toolbar
Click on the third button on the AspectJ toolbar to pull out the Configuration of the Greetings example.
In the Configuration drop-down list, select
Figure 5: Establish a configuration drop-down list
When compiling is completed, click the FileSystems page of the Manager window to expand the contents of the Greetings package (see Figure 6), perform GiveGReeting to generate the result of the same example.
Greetings package is displayed in the manager
Dressing process, first select Logging.lst generation results as shown above with the result of the logging aspect, then select Counting.lst to generate the result of the example as followed above.
This demonstrates the standard process of compilation. This process is created by AspectJ, which specifies the Java class and aspects of the establishment together. The establishment list can also be used for the parameter file of the AspectJ command line tool, and the AspectJ created list is manually created.
Performing the standard process of the class established and the standard process of performing any Java class is as simple as possible, aspect performance has been used as a bytecode and a compiled class, and there is nothing special needs.
Browse
When the Aspectj development environment is running, an AspectJ page appears in the Manager window, click the page to access the AspectJ browsing page, as shown in Figure 7. Aspect browsers are a powerful tool to identify elements that have been affected in the program.
Figure 7: Access browser
Debugging
AspectJ execution time must be configured to make them visible to the debugger.
Right-click FileSystems Node in Manager
Set
Set up breakpoints for normal debugging
When the debugger is executed instead of the Java class, aspect is opened in the code editing window, the debug is in just a normal Java class.
If the NetBeans Debugger in Sun One Studio fully supports JSR-45, debugging will work very well, I know that there is a problem in some code breakpoint settings, but the author's procedures work very well, see the AspectJ website Frequently Asked Questions will get the latest information.
Aspect program guide
Look at the AspectJ Programming Guide understands the semantics and grammar, examples, terms and defects of the language. The term is aspectj extract, mainly the entry point. Defect is an AspectJ program that generates expected results, such as consumption until the overflow cycle, this problem is easy to solve, the terminology and defects are short but it is worth reading.
Aspect project
"Okay", you may think, "I have seen the potential for aspect of the program, but I really dare to use the version of 1.0 tool to develop key systems?" When language and tools are still developing, updates are added At the same time, we must work hard to compatibility, and the update is driven by the user needs, and user needs protect their program investment.
In case of some people integrate into their lasting code, it is forced to stop using Aspectj, there is a retreat, which is not necessarily necessary, but the cautious plan needs to consider the worst case.
This is a simple way to capture the intermediate code generated by Aspectj:
Click on the rightmost button on AspectJ (see Figure 4), open the AspectJ Development Environment Settings dialog will open. Select the Only Preprocess and Generate Java Source Files item in "AJC Options".
Enter the directory in Working Directory: TextField.
It is now compiled under AspectJ to generate a valid Java code in the specified directory, and the resulting code can be compiled by the standard Java compiler. Performing a compiled class will generate and execute the same correct results as the class compiled by AspectJ.
Migrate to aspective programming (AOP)
AspectJ programming guidance is not only a reference, but also the programming wizard. As a simple start, read the program guide, then use aspect to debug, test, and performance adjustment, when you become more experience, you can continue to advance, force yourself Follow programming standards, policies, and agreements, Application Aspect Programming (AOP) to existing programs, consider using the Aspect Mining Tool tool to determine potential cross-cutting relationships in the program.
Aspects of programming (AOP) does not have a deep concept in programming, just pay a little effort, the basic function is enough to produce direct benefits, which not only meets the ultimate programming checkered, but also meets every step. The conservative style that needs to be proven before the next step is required, which has a profound meaning aspect-oriented (AOP) and far-reaching significance as a structured programming to object-oriented programming, and this gradual migration is hard to blow.
About author
Vaughn Spurlin started programming career in 1967, from 1875, he as a freelance, work in computer hardware and language, including some very famous personal computers, now Vaughn is Sun ONE Studio wrote some technical articles and development training materials, and cooperated to write a book about NetBeans.