Programming Tutorials Browser Tutorials Articles Struts Tutorials Hibernate Tutorials

  Tutorial: Use AOP to maintain legacy Java applications

Use AOP to maintain legacy Java applications This artical shows you how to use aspect-oriented programming (AOP) to gain an unprecedented view into the inner workings of even the most opaque of legacy applications. Please note that this article assume

Tutorial Details:

Crosscutting is an AOP technique for ensuring that independent concerns remain modularized but are also flexible enough to be applied at different points throughout an application. The two varieties of crosscutting are static and dynamic. Dynamic crosscutting entails changing the execution behavior of an object by weaving in new behavior at specific points of interest. Static crosscutting lets us alter the very structure of an object by injecting additional methods and/or attributes into it.

The syntax of static crosscutting is quite different from that of dynamic crosscutting. The following terminology applies to dynamic crosscutting:

* A join point is a particular point of execution in a Java program, such as a method in a class.

* A pointcut is a language-specific construct that denotes or captures a particular join point.

* An advice is a piece of code (typically, a crosscutting functionality) to be executed when a specific pointcut is reached.

* An aspect is a construct that defines pointcuts and advices and the mapping between them. Aspects are used by the AOP compiler to weave additional functionality into existing objects at specific execution points.

All of the code demonstrations in this article will utilize dynamic crosscutting. See Resources to learn more about static crosscutting.

AOP under AspectJ
To follow the examples in this article you should be familiar with the following features specific to AOP under AspectJ:

* AspectJ provides a compiler/bytecode weaver called ajc that compiles AspectJ and Java language files. ajc weaves together aspects as necessary to produce .class files compliant with any Java VM (1.1 or later).

* AspectJ supports aspects that specify that a particular join point should never be reached. If the ajc process determines otherwise, it signals a compile-time warning or error (depending on the aspect).

Application and system analysis
In the following sections you will learn two different mechanisms of application and system analysis using AOP. The first mechanism, which I call static analysis, requires you to do the following:

1. Check out from CVS (or whatever code versioning system you use) the entire application code base into your local area.

2. Alter the build file to use the AspectJ compiler (ajc).

3. Include the aspect classes at the appropriate places.

4. Run a complete build of the system.

The second mechanism, which I call dynamic analysis, requires you to not only build the system in your local area but also run the actual use cases of the system and gather information from runtime reflection into the running system. In the sections that follow I\'ll go over each mechanism in detail, using code examples to illustrate key concepts.

Static analysis
I\'ll examine a number of techniques for performing static analysis of a legacy application, applying them to three common maintenance scenarios, as follows:

* Evaluating the impact of an interface change
* Identifying dead or unreachable code
* Instituting loose coupling

Now, let\'s get started!

Evaluating the impact of an interface change
An object-oriented legacy application or system should consist of a number of modules that expose a well-defined interface to its clients. Suppose, then, that you have been tasked with incorporating a new requirement that entails changing an existing interface. Being new to the code, your first challenge is to figure out the exact impact that changing this interface will have on the system\'s clients. For the sake of this example, the implied impact is more than that of merely changing each of the clients to make the method calls per the changed signatures. Therefore, knowing all the clients of the interface may be instrumental in determining the best approach to implementing the new requirement, as well as whether the requirement is worthwhile for the given system. I\'ll use static analysis to first determine the clients of the interface, and then evaluate the impact of interface change on the system.

This technique is based on aspects signaling compile-time warnings if specific join points are found to be reachable. In this case, you will write join points to capture calls to all methods of the specific interface. You must first extract the application code base in a local area, alter the build file to use the AspectJ compiler and to include the aspect classes at the appropriate places, and then run a complete build of the system. If you have captured the join points correctly you can expect to see compile-time warnings to signal calls to the methods of interest in the codebase.

The compile-time aspect shown in Listing 1 detects and displays all classes that call (an implementor of) the interface com.infosys.setl.apps.AppA.InterfaceA, while excluding the (one or more) implementor classes themselves. So, for the sample InterfaceA, its implementor class ClassA, and the caller class ClassB, the aspect will generate a compile-time warning for the call a.methodA() in ClassB.
Listing 1. Classes that use InterfaceA


package com.infosys.setl.apps.AppA;
public interface InterfaceA
{
public String methodA();
public int methodB();
public void methodC();
}

package com.infosys.setl.apps.AppA;
public class ClassA implements InterfaceA
{
public String methodA()
{
return \"Hello, World!\";
}
public int methodB()
{
return 1;
}
public void methodC()
{
System.out.println(\"Hello, World!\");
}
}

package com.infosys.setl.apps.AppB;
import com.infosys.setl.apps.AppA.*;
public class ClassB
{
public static void main(String[] args)
{
try
{
InterfaceA a =
(InterfaceA)Class.forName(\"com.infosys.setl.apps.AppA.ClassA\").newInstance();
System.out.println(a.methodA());
}
catch (Exception e)
{
e.printStackTrace();
}
}
}

package com.infosys.setl.aspects;
public aspect InterfaceCallersAspect
{
pointcut callerMethodA(): call (* com.infosys.setl.apps.AppA.InterfaceA.methodA(..)) &&
!within(com.infosys.setl.apps.AppA.InterfaceA+);
pointcut callerMethodB(): call (* com.infosys.setl.apps.AppA.InterfaceA.methodB(..)) &&
!within(com.infosys.setl.apps.AppA.InterfaceA+);
pointcut callerMethodC(): call (* com.infosys.setl.apps.AppA.InterfaceA.methodC(..)) &&
!within(com.infosys.setl.apps.AppA.InterfaceA+);
declare warning: callerMethodA(): \"Call to InterfaceA.methodA\";
declare warning: callerMethodB(): \"Call to InterfaceA.methodB\";
declare warning: callerMethodC(): \"Call to InterfaceA.methodC\";
}



 

Read Tutorial at: Click here to view the tutorial

Rate Tutorial:
Use AOP to maintain legacy Java applications

View Tutorial:
Use AOP to maintain legacy Java applications

Related Tutorials:

Opening up new ports to Java with javax.comm - JavaWorld - September 1998
Opening up new ports to Java with javax.comm - JavaWorld - September 1998
 
The state of Java application middleware, Part 1 - JavaWorld March 1999
The state of Java application middleware, Part 1 - JavaWorld March 1999
 
Programming restrictions on EJB - JavaWorld August 2000
Programming restrictions on EJB - JavaWorld August 2000
 
Embed Java code into your native apps - JavaWorld May 2001
Embed Java code into your native apps - JavaWorld May 2001
 
I want my AOP!, Part 1
I want my AOP!, Part 1
 
Use Web services to integrate Web applications with EISs
Use Web services to integrate Web applications with EISs
 
I want my AOP!, Part 2
I want my AOP!, Part 2
 
I want my AOP!, Part 3
I want my AOP!, Part 3
 
Ilog JRules 4.0: Working by the rules
Ilog JRules 4.0: Working by the rules
 
Rumble in the jungle: J2EE versus .Net, Part 1
Rumble in the jungle: J2EE versus .Net, Part 1
 
Use AOP to maintain legacy Java applications
Use AOP to maintain legacy Java applications This artical shows you how to use aspect-oriented programming (AOP) to gain an unprecedented view into the inner workings of even the most opaque of legacy applications. Please note that this article assume
 
J2EE Connector Architecture
J2EE Connector Architecture Introduction If you\'ve ever had to integrate legacy data, data sources, or functionality with a new application, you\'ve no doubt faced a number of challenges: for instance, figuring out how to connect to legacy systems, m
 
Improve Application Management With JMX
Improve Application Management With JMX Leverage JMX technology and existing tools to boost the operations management capabilities of your business applications.
 
Object-relation mapping without the container
If you follow the latest developer buzz then you\\\\\'ve likely heard of IOC (Inversion of Control) containers and AOP (aspect-oriented programming).
 
Performance Analysis of J2EE Applications Using AOP Techniques
Performance Analysis of J2EE Applications Using AOP Techniques In this article we demonstrate the use of AOP techniques through which J2EE applications can be easily instrumented without any modifications to application code. We have developed a very sim
 
Second-generation aspect-oriented programming
Second-generation aspect-oriented programming
 
Aspect-Oriented Annotations
Aspect-Oriented Annotations Annotations are one of the new language features in J2SE 5.0, and allow you to attach metadata onto any Java construct. Meanwhile, Aspect-Oriented Programming (AOP) is a fairly new technology that makes it easier for you to en
 
jGuard v0.60 released!
jGuard v0.60 released! i am pleased to announce a new major release(v0.60) of the java security library called jGuard(http://sourceforge.net/projects/jguard/). this library is build on top of jaas, for J2EE web applications. his goal is to provide f
 
Aspect-Oriented Programming in Java
This article is divided into three parts: The first part explaines the concepts of AOP, the second introduces AspectJ(TM), an implementation of the AOP concepts in Java, and part three compares the AOP approach to metalevel programming.
 
Java Server Pages Dynamically Generated Web Content.
JavaServer PagesTM (JSP TM) technology allows Web developers and designers to rapidly develop and easily maintain, information-rich, dynamic Web pages that leverage existing business systems.
 
Site navigation
 

 

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2006. All rights reserved.