AspectJ in Practice: Reap the Benefits of Modularity

AspectJ in Practice: Reap the Benefits of Modularity

spect-oriented programming (AOP), a contemporary programming technology with a large number of supporters, is in essence an improved version of object-oriented (OO) programming technology that enables program objects to be classified and built into hierarchical structures. Its concepts are currently utilized in complex programs that process financial transactions.

Fundamentally, AOP assumes that any complex system has some inherent crosscutting issues and concerns, which traditional programming methodologies do not address well. Aspects are new programming constructs that cross-cut the modularity of traditional programming constructs in carefully-designed and principled ways. For example, a single aspect can affect the implementation of a number of methods in a number of classes, enabling aspects to capture the cross-modular structure of these kinds of concerns in a clean way. This way AOP keeps these concerns from getting tangled by isolating crosscutting aspects into individual blocks.

AspectJ, a general-purpose, aspect-oriented extension to the Java language, is a collection of tools for AOP. Originally developed by professionals from Xerox’s Palo Alto Research Center to grow the technology and community in 2002, it has since moved to the Eclipse Project. AspectJ contains expansions and tools that enable Java developers to better manage the problems in large program systems and to reap the benefits of modularity. AspectJ provides clean modularization of crosscutting concerns, such as:

  • Error checking and handling
  • Synchronization
  • Context-sensitive behavior
  • Performance optimizations
  • Monitoring and logging
  • Debugging support
  • Multi-object protocols

This extension (which you can think of as a plug-in or addition) is not only very easy to learn and use, but since it’s developed on the Java platform, it also adheres to the “write once, run anywhere” rule. It can run with any Java program and on any Java-2-compatible platform. Consisting of a compiler, a debugger, a generator of documentation, and a structure browser, AspectJ can offer invaluable assistance with logging, debugging, or testing for almost all projects, making all these procedures as easy as 1-2-3. Developers can also establish further separation from their final software project releases by simply excluding AspectJ from the compilation process.

This article demonstrates how you can use AspectJ in your development processes. It includes a “Hello world” example that already has become very well known in the AspectJ development community. The article can serve as either an introduction or your first endeavor into the AOP world. For a deeper understanding of the topic, consult the official documentation and manuals.

Which situations require AspectJ, you ask. Imagine the following scenario: you’re a member of a development team facing a huge project that a number of clients are going to buy, but each client has a few requests for the project. One needs logging functions in 10,000 specific places within your code. Another requires some very specific logging functions only for critical (from his point of view) parts and situations. A third client requires added networking, localization, etc.

Of course, you could rebuild the project’s structure, adding new functions with new configurations, creating new class dependencies, and so on, but since the project is already in development, you are under time pressure (as usual). Maybe add “hardcoded” patches? Yes, but a “patched” project likely will get out of control very quickly. Besides, it also requires testing, bug fixing, etc. Doesn’t that all sound like a real headache? Not with AspectJ. You can create aspects, which will do everything necessary.

Aspects are the essence of AspectJ. In general, an aspect is a modular unit that crosscuts the structure of other modular units. It exists in either design or implementation:

  • A design aspect is a modular unit of the design that crosscuts the structure of other parts of the design.
  • A code aspect is a modular unit of the program that crosscuts other modular units of the code.

This is a very general definition that doesn’t address OO, but suites the introductory purposes of this article. Aspects can have type, extend classes and other aspects, and be either abstract or concrete. They do not have constructors or finalizers, cannot be created with the new operator, and cannot be used interchangeably with classes. For more detailed info, consult the official documentation. However, here’s a brief listing of some of the things you can do with aspects:

  • Place code before or after a method call
  • Change method arguments
  • Track method calls using regular expressions
  • Change class hierarchies
  • Control the treatment of exceptions

Getting back to your project, even if you’ve never worked with AspectJ, you can easily recognize the advantages it would offer:

  • You can place additional code around method calls to provide logging.
  • To separate these logging, localization, or networking functions (it doesnt really matter for which function type you use your aspects) among your customers, you can track your methods with regular expressions, which AspectJ uses.
  • And the main advantage: none of these features will require any code updates. You can just create new aspects and recompile the project.

Install AspectJ
Since seeing a product work is better than just reading about it, download the most recent version of AspectJ. Once it’s downloaded, installation is pretty easy (assuming you already have the Java 2 SDK installed). Just execute the following command:

java jar downloaded_filename.jar

This will run an installer, which will help you complete the installation. After that, you need to correctly set the path names mentioned on the last step of installation. You are then ready to work with AspectJ in the “big project” example to follow.

Look at the following code for your big project:

1:				// Our Java example  TestClass.java2:				public class TestClass {3:4:					public static void method1() {5:						System.out.println("Hello World!");6:					}7:8:					public static void method2() {9:						System.out.println("Really Hello!");10:					}11:12:					public static void say(String x) {13:						System.out.println(x);14:					}15:16:					public static void main(String[] args) {17:						method1();18:						method2();19:						say("I can talk!");20:					}21:				}

This code intentionally is easy, beginner-level Java to make understanding the technology easier. You can create more complex code after you’ve mastered aspects. Lines 4-6 and 8-10 define the class methods, which perform the only job: printing the well-known “Hello World” prompt. Lines 12-14 contain the other method, which says whatever you “ask” it to say. And last but not least, the main() function of the class calls all the methods.

Compile and run this example:

javac TestClass.javajava TestClass

The output is the following:

Hello World! Really Hello!I can talk!

Nothing unexpected, I presume. Now, create the AspectJ code in Listing 1.

Strange code, isn’t it? It looks pretty hard at first glance. But don’t worry, it’s not. Line 2 creates the aspect TestMe. Line 4 defines pointcut (it picks out certain join points in the program flow) for all public methods of class TestClass that are without arguments. Line 5 defines pointcut for all public methods of class TestClass that accept arguments of String type. Lines 7-10 define what will be executed before all public methods of class TestClass that accept one String type argument (defined in second pointcut). Lines 12-15 define what will be done after the previously mentioned methods. Lines 17-19 and 21-23 share the same definitions as the first pointcut methods. Notice that lines 9, 14, 18, and 22 use the following call:

thisJointPointStaticPart.getSignature().getName()

The result is the name of the method before or after which you are functioning. Argument a in lines 7 and 12 holds the value of the argument that passed to the methods as a String type argument.

Compile AspectJ
Compilation is easy, too. Just execute the following:

ajc TestMe.aj TestClass.javajava TestClass

(Note: You can list as many .java files as you need after TestClass.java; all of them will use the aspects created in TestMe.aj)

The output will be the following:

 -- TestClass.method1 start --Hello World! -- TestClass.method1 stop -- -- TestClass.method2 start --Really Hello! -- TestClass.method2 stop -- -- TestClass.say(I can talk!) start --I can talk! -- TestClass.say(I can talk!) stop --

Now do you see how useful AspectJ can be? Even this simple example shows the power of this technology. You didn’t change a single line of your main class code, but you already have a perfect solution for logging or testing!

Another AspectJ Benefit
Another interesting functionality of aspect is very useful when you need localization. To see it, just comment lines 7-15 (otherwise it creates a circular dependency at method-call) and add the following code after them:

void around(String a): methodCallArg(a) {				proceed("it is not '" + a + "' anymore!");			}

Compile and run it the same way you did before. Look at the output. If you completed everything properly, you’ll see that the say() method (which previously received the String “I can talk!”) receives and outputs the next String: “it is not I can talk! anymore!” A real argument passed to the previously mentioned code, and only after being changed, did it pass directly to the say() method. This could serve as an interesting scheme with localization. For example, you can check some constants about which language you are using once, and then provide output in that exact language.

devx-admin

devx-admin

Share the Post:
Poland Energy Future

Westinghouse Builds Polish Power Plant

Westinghouse Electric Company and Bechtel have come together to establish a formal partnership in order to design and construct Poland’s inaugural nuclear power plant at

EV Labor Market

EV Industry Hurting For Skilled Labor

The United Auto Workers strike has highlighted the anticipated change towards a future dominated by electric vehicles (EVs), a shift which numerous people think will

Soaring EV Quotas

Soaring EV Quotas Spark Battle Against Time

Automakers are still expected to meet stringent electric vehicle (EV) sales quotas, despite the delayed ban on new petrol and diesel cars. Starting January 2023,

Affordable Electric Revolution

Tesla Rivals Make Bold Moves

Tesla, a name synonymous with EVs, has consistently been at the forefront of the automotive industry’s electric revolution. The products that Elon Musk has developed

Poland Energy Future

Westinghouse Builds Polish Power Plant

Westinghouse Electric Company and Bechtel have come together to establish a formal partnership in order to design and construct Poland’s inaugural nuclear power plant at the Lubiatowo-Kopalino site in Pomerania.

EV Labor Market

EV Industry Hurting For Skilled Labor

The United Auto Workers strike has highlighted the anticipated change towards a future dominated by electric vehicles (EVs), a shift which numerous people think will result in job losses. However,

Soaring EV Quotas

Soaring EV Quotas Spark Battle Against Time

Automakers are still expected to meet stringent electric vehicle (EV) sales quotas, despite the delayed ban on new petrol and diesel cars. Starting January 2023, more than one-fifth of automobiles

Affordable Electric Revolution

Tesla Rivals Make Bold Moves

Tesla, a name synonymous with EVs, has consistently been at the forefront of the automotive industry’s electric revolution. The products that Elon Musk has developed are at the forefront because

Sunsets' Technique

Inside the Climate Battle: Make Sunsets’ Technique

On February 12, 2023, Luke Iseman and Andrew Song from the solar geoengineering firm Make Sunsets showcased their technique for injecting sulfur dioxide (SO₂) into the stratosphere as a means

AI Adherence Prediction

AI Algorithm Predicts Treatment Adherence

Swoop, a prominent consumer health data company, has unveiled a cutting-edge algorithm capable of predicting adherence to treatment in people with Multiple Sclerosis (MS) and other health conditions. Utilizing artificial

Personalized UX

Here’s Why You Need to Use JavaScript and Cookies

In today’s increasingly digital world, websites often rely on JavaScript and cookies to provide users with a more seamless and personalized browsing experience. These key components allow websites to display

Geoengineering Methods

Scientists Dimming the Sun: It’s a Good Thing

Scientists at the University of Bern have been exploring geoengineering methods that could potentially slow down the melting of the West Antarctic ice sheet by reducing sunlight exposure. Among these

why startups succeed

The Top Reasons Why Startups Succeed

Everyone hears the stories. Apple was started in a garage. Musk slept in a rented office space while he was creating PayPal with his brother. Facebook was coded by a

Bold Evolution

Intel’s Bold Comeback

Intel, a leading figure in the semiconductor industry, has underperformed in the stock market over the past five years, with shares dropping by 4% as opposed to the 176% return

Semiconductor market

Semiconductor Slump: Rebound on the Horizon

In recent years, the semiconductor sector has faced a slump due to decreasing PC and smartphone sales, especially in 2022 and 2023. Nonetheless, as 2024 approaches, the industry seems to

Elevated Content Deals

Elevate Your Content Creation with Amazing Deals

The latest Tech Deals cater to creators of different levels and budgets, featuring a variety of computer accessories and tools designed specifically for content creation. Enhance your technological setup with

Learn Web Security

An Easy Way to Learn Web Security

The Web Security Academy has recently introduced new educational courses designed to offer a comprehensible and straightforward journey through the intricate realm of web security. These carefully designed learning courses

Military Drones Revolution

Military Drones: New Mobile Command Centers

The Air Force Special Operations Command (AFSOC) is currently working on a pioneering project that aims to transform MQ-9 Reaper drones into mobile command centers to better manage smaller unmanned

Tech Partnership

US and Vietnam: The Next Tech Leaders?

The US and Vietnam have entered into a series of multi-billion-dollar business deals, marking a significant leap forward in their cooperation in vital sectors like artificial intelligence (AI), semiconductors, and

Huge Savings

Score Massive Savings on Portable Gaming

This week in tech bargains, a well-known firm has considerably reduced the price of its portable gaming device, cutting costs by as much as 20 percent, which matches the lowest

Cloudfare Protection

Unbreakable: Cloudflare One Data Protection Suite

Recently, Cloudflare introduced its One Data Protection Suite, an extensive collection of sophisticated security tools designed to protect data in various environments, including web, private, and SaaS applications. The suite

Drone Revolution

Cool Drone Tech Unveiled at London Event

At the DSEI defense event in London, Israeli defense firms exhibited cutting-edge drone technology featuring vertical-takeoff-and-landing (VTOL) abilities while launching two innovative systems that have already been acquired by clients.

2D Semiconductor Revolution

Disrupting Electronics with 2D Semiconductors

The rapid development in electronic devices has created an increasing demand for advanced semiconductors. While silicon has traditionally been the go-to material for such applications, it suffers from certain limitations.

Cisco Growth

Cisco Cuts Jobs To Optimize Growth

Tech giant Cisco Systems Inc. recently unveiled plans to reduce its workforce in two Californian cities, with the goal of optimizing the company’s cost structure. The company has decided to

FAA Authorization

FAA Approves Drone Deliveries

In a significant development for the US drone industry, drone delivery company Zipline has gained Federal Aviation Administration (FAA) authorization, permitting them to operate drones beyond the visual line of

Mortgage Rate Challenges

Prop-Tech Firms Face Mortgage Rate Challenges

The surge in mortgage rates and a subsequent decrease in home buying have presented challenges for prop-tech firms like Divvy Homes, a rent-to-own start-up company. With a previous valuation of

Lighthouse Updates

Microsoft 365 Lighthouse: Powerful Updates

Microsoft has introduced a new update to Microsoft 365 Lighthouse, which includes support for alerts and notifications. This update is designed to give Managed Service Providers (MSPs) increased control and

Website Lock

Mysterious Website Blockage Sparks Concern

Recently, visitors of a well-known resource website encountered a message blocking their access, resulting in disappointment and frustration among its users. While the reason for this limitation remains uncertain, specialists