© IBM Corporation1
Aspect Oriented ProgrammingWhy, What and How?
Andy ClementAspectJ Committer
IBM Hursley [email protected]
2© IBM Corporation
Agenda
� Why?– Why do we need AOP ?
� What?– What is AOP?– AspectJ
� How?– The need for tool support– Demo: AspectJ Development Tools (AJDT) for Eclipse– Demo: Using AOP on real middleware– Adopting the technology
3© IBM Corporation
Good Modularity: What oo is good at…
� XML parsing in org.apache.tomcat– Red shows relevant lines of code– Nicely fits in one box
���������
����������� �
4© IBM Corporation
Good Modularity: What oo is good at…
� URL pattern matching in org.apache.tomcat– Red shows relevant lines of code– Nicely fits in two boxes (using inheritance)
� ���������� �����
����������� �
5© IBM Corporation
No Modularity: What oo is bad at…
� Where is logging in org.apache.tomcat?– Not in just one place, not even in a small number of places– Scattered and tangled
��������
����������� �
6© IBM Corporation
The Cost of Tangled Code
� Redundant code– Same fragment of code in many places– e.g. �������������������� �����
� Difficult to reason about– Non-explicit structure– The big picture of the tangling isn’t clear
� Difficult to change– Have to find all the code involved– And be sure to change it consistently– And be sure not to break it by accident
7© IBM Corporation
AO tries to recognize …
� Crosscutting concerns:– Are inherent in any complex system– Have a clear purpose– Have a natural structure
� Lets capture them explicitly:– In a modular way– With language and tool support
When designing software, the ideal mapping is 1..1:One concept at the design level maps to one class or package
(one module) at the code level
8© IBM Corporation
AspectJ
� An aspect oriented programming (AOP) language
� Java language extension
� Divides system into – core concerns (classes)– crosscutting concerns (aspects)
� Broad IDE support– Eclipse, Emacs, JBuilder, NetBeans, …
9© IBM Corporation
What is an AspectJ Aspect?
� Aspects are...– At the design level … concerns that crosscut– At the implementation level … a programming
construct
Aspect = pointcut + advice
10© IBM Corporation
What’s a Pointcut?
� Join point– An event in the execution of Java program.– e.g. Method call, field access, exception
handler, object initialization
� Pointcut– Picks out joinpoints in a particular system– e.g. call of method ‘foo()’, set of field ‘x’,
constructor for object ‘Fred’– Can also expose context from the matched
join point
11© IBM Corporation
Types of Pointcuts
Event based - well defined execution points, e.g.– method calls, executions – field gets / sets– exception handling– object and class initialisation
Scoping - only select joinpoints within a certain scope, e.g.– within a set of packages, – within the implementation of a method,– in the control flow of some event (e.g. downstream of an
unsecured call)
Context matching - expose context at the joinpoint, e.g.– identity of the caller– identity of the target object– exception being handled– value being put into a field
12© IBM Corporation
What’s Advice?
� Advice– Java logic to execute when conditions of a pointcut are
met.
� Types of Advice:– before()
� Executes before the joinpoint– after() throwing/returning
� Executes after the joinpoint, execution can be made conditional on how joinpoint exits (i.e. with/without exception)
– around()� Executes ‘instead of’ the joinpoint – has a choice about
whether to invoke the original logic
� Can be parameterized to process context exposed by the pointcut
13© IBM Corporation
Eclipse is #1 for AOSD
� AspectJ Development Tools (AJDT) for Eclipse– Open Source– Developed in Hursley– Partnership with AspectJ team– Fully tested in WebSphere Studio and Rational
Eclipse based tools
14© IBM Corporation
AJDT Demo - Figures
��� ������ �����������������
��������������������� ����! ��������"������ ��#����$���#��%��&��� ����� �������"���� �
15© IBM Corporation
Webservices Invocation Framework
� Middleware component– Simple Java API for invoking webservices, no matter how or
where they are provided
� Released to Apache– But IBM wants a version tightly coupled to IBMs normal
‘qualities of service’� IBM tracing/monitoring/management
� How do we manage this?– Manage an IBM internal of the apache codebase?– Put the IBM facilities into the open source codebase?
16© IBM Corporation
AJDT Demo - WSIF
��� �' �()� �����������������
��������������������� ����! ��������"������ ��#����$���#��%��&��� ����� �������"���� �
17© IBM Corporation
Demo conclusions
� Capabilities of AOP technology look very promising– Code size reduction– No tangling– Faster development times– Product-line engineering
18© IBM Corporation
Eliminating tangling
�����������������������������������������
����������������� �����!�"������ ��#���� � ���
$$�%��&����#������� ��#����$$�%��&����#������� ��#����$$�%��&����#������� ��#����$$�%��&����#������� ��#����� '(������#��������('()*+('� '(������#��������('()*+('� '(������#��������('()*+('� '(������#��������('()*+('�!����!����!����!���
���������!��,�� �
�����--���������������..�������������..�������������..�������������..�
����������%�--���������������%�--���������������%�--���������������%�--����� �/��0--����/��0--����/��0--����/��0--�������������%�--���������������%�--���������������%�--���������������%�--���������
������������������������������������������������������������������������ ��������/���-�����������-�#0���!������������������&&���������-����&&���������-����&&���������-����&&���������-����������
��&&���� ����'(#����������&&���� ����'(#����������&&���� ����'(#����������&&���� ����'(#��������(�(�(�(�
�
�����������������������������������������
����������������� �����!�"������ ��#���� � ������������!��,�� �
�����--�����������/���-�����������-�#0���!����
�
Crosscutting concernsextracted
Example: Code to handle EJB Entity bean passivation
BEFORE AFTER
19© IBM Corporation
Adopting AOP
time & confidence
reward
20© IBM Corporation
Adopting AOP
time & confidence
reward
Risky Space
21© IBM Corporation
Adopting AOP
time & confidence
reward
exploration/enforcement
auxiliary /infrastructure
core /business
AO Analysis,AO Design,AO Strategy
22© IBM Corporation
Exploration & Enforcement
� Aspects to monitor, analyze, debug system– Can use individually or share with team
� Aspects that ensure design integrity– Can run privately, as part of nightly build, or shared w/ team
� Sharing with team can be supported by– integration in IDE or including in ant script
� No runtime dependency on AspectJ– production code has not been touched by AspectJ
23© IBM Corporation
Don’t use System.err.println !!!
#0�-�����#���#0�-�����#���#0�-�����#���#0�-�����#��� ���������&&��&���������&&��&���������&&��&���������&&��& ����
#�����0�#�����0�#�����0�#�����0� ���#�1���#�1���#�1���#�1,��!��,��!��,��!��,��!������� ��#-������ ��#-������ ��#-������ ��#-���2��2��2��2 ........����,��!��,��!��,��!��,��!��%����-����0&&��%����-����0&&��%����-����0&&��%����-����0&&������
#�����0�#�����0�#�����0�#�����0� #������&1#������&1#������&1#������&1&��&��&��&��2222 ��������0���������0���������0���������0� 33333333 &��&��&��&��2222 ���������������������������������������� 33333333��--��--��--��--2222 4!��,��-�5�#��������64����4!��,��-�5�#��������64����4!��,��-�5�#��������64����4!��,��-�5�#��������64��������
���-������-������-������-��� ,�����&,�����&,�����&,�����&1111 ���#����#����#����#� ........ #������&1#������&1#������&1#������&17���8��#����'�0����!��-�&&��77���8��#����'�0����!��-�&&��77���8��#����'�0����!��-�&&��77���8��#����'�0����!��-�&&��7����
� Warn developers using System.out, System.err and printStackTrace
24© IBM Corporation
Architectural Layering
View
Persistence
Model
Controller Layer 1
Layer 2
Layer 3
Layer 4
25© IBM Corporation
Map packages to components
��#�����#�����#�����#��� 9��!�����0����9��!�����0����9��!�����0����9��!�����0����
#�����0�#�����0�#�����0�#�����0� ��:��,��:��,��:��,��:��,�1��1��1��1�,��!��,��!��,��!��,��!�����,���,���,���,��2���2���2���2�#�����0�#�����0�#�����0�#�����0� ��;���-��;���-��;���-��;���-�1��1��1��1�,��!��,��!��,��!��,��!������-����-����-����-��2���2���2���2�#�����0�#�����0�#�����0�#�����0� ��%�����--����%�����--����%�����--����%�����--���1�1�1�1 ,��!��,��!��,��!��,��!��������--��������--��������--��������--����2���2���2���2�#�����0�#�����0�#�����0�#�����0� �����������������������������������������������������1��1��1��1�,��!��,��!��,��!��,��!��#����������#����������#����������#������������2���2���2���2�
<<<<
26© IBM Corporation
‘External’ calls into components
<<<<
#�����0�#�����0�#�����0�#�����0� ���,%�--���,%�--���,%�--���,%�--1111 ��--��--��--��--2����,��2���..��2����,��2���..��2����,��2���..��2����,��2���..����:��,��:��,��:��,��:��,����
#�����0�#�����0�#�����0�#�����0� ����-%�--����-%�--����-%�--����-%�--1111 ��--��--��--��--2�����-��2���..��2�����-��2���..��2�����-��2���..��2�����-��2���..����;���-��;���-��;���-��;���-����
#�����0�#�����0�#�����0�#�����0� ������--��%�--������--��%�--������--��%�--������--��%�--1111 ��--��--��--��--2�������--����2���..��2�������--����2���..��2�������--����2���..��2�������--����2���..����%�����--����%�����--����%�����--����%�����--������
#�����0�#�����0�#�����0�#�����0� #����������%�--#����������%�--#����������%�--#����������%�--1111 ��--��--��--��--2�#������������2���..2�#������������2���..2�#������������2���..2�#������������2���..������������������������������������������������������������
#�����0��#�����0��#�����0��#�����0������%�--����%�--����%�--����%�--�1�1�1�1 ��--��--��--��--2�2�2�2����� ��=-���� ��=-���� ��=-���� ��=-��2�����2�����2�����2���
<<<<
27© IBM Corporation
Compile warnings for illegal calls
<<<<
���-����,�����&����-����,�����&����-����,�����&����-����,�����&�1�1�1�1�������--��%�--������--��%�--������--��%�--������--��%�--�1��1��1��1�7777>����--�������������--��>����--�������������--��>����--�������������--��>����--�������������--��7�7�7�7�
���-����,�����&����-����,�����&����-����,�����&����-����,�����&�1�1�1�1����,%�--���,%�--���,%�--���,%�--�..���..���..���..����%�����--����%�����--����%�����--����%�����--���1��1��1��1�7777��-��������--���������--����,��-��������--���������--����,��-��������--���������--����,��-��������--���������--����,7�7�7�7�
���-����,�����&����-����,�����&����-����,�����&����-����,�����&�1�1�1�1�����-%�--����-%�--����-%�--����-%�--�..���..���..���..����%�����--����%�����--����%�����--����%�����--���33��33��33��33���:��,��:��,��:��,��:��,�1��1��1��1�7777��-�����,�����������--���������--�����-��-�����,�����������--���������--�����-��-�����,�����������--���������--�����-��-�����,�����������--���������--�����-7�7�7�7�
���-����,�����&����-����,�����&����-����,�����&����-����,�����&�1�1�1�1�#����������%�--#����������%�--#����������%�--#����������%�--�..���..���..���..����;���-��;���-��;���-��;���-�1��1��1��1�7777��-������-������������#�����������-������-������-������������#�����������-������-������-������������#�����������-������-������-������������#�����������-����7�7�7�7�
���-����,�����&���-����,�����&���-����,�����&���-����,�����& 1�1�1�1�����%�--����%�--����%�--����%�--�..���..���..���..�������������������������������������������������������1��1��1��1�7777������������-�����!���-����--����������������������-�����!���-����--����������������������-�����!���-����--����������������������-�����!���-����--����������7�7�7�7�
28© IBM Corporation
Auxiliary/Infrastructure
� Whole team awareness– developers know aspects are there– runtime dependency on aspectjrt.jar
� But only some developers – change working practices– change day-to-day tools
� Easily understood business case
29© IBM Corporation
Examples…
� Tracing, logging� Error and exception handling� Monitoring and statistics gathering� Transactions� Session management (for e.g. persistence)� Threading� Synchronization� Caching� Remote access� …
30© IBM Corporation
Building the Case
� Factors:– Number of source files in project– Average time spent per file over a release
� (e.g. on tracing code)
?@@���-��� �?A����0����/�)A�#������!�0��
A@@���-��� �?A����0����/�*�A�#������,��6�
?@@@���-��� �?A����0����/�+�#������,��6�
31© IBM Corporation
Building the Case
� You can certainly– write and test a simple tracing aspect in < 25
hours– write and test a good tracing aspect in < 50 hours– and save a lot of time
?@@���-��� �?A����0����/�)A�#������!�0��
A@@���-��� �?A����0����/�*�A�#������,��6�
?@@@���-��� �?A����0����/�+�#������,��6�
32© IBM Corporation
Core/Business Aspects
� Using aspects when implementing application’s core functionality (e.g. security)
� Requires full team buy-in to AspectJ� Changes to tool set (e.g. JDT -> AJDT)� You’ll be ready if you followed staged
approach– enough developers will understand technology– enough developers will be aware of value– management will be aware of value
33© IBM Corporation
[email protected]*� CapitalOne
� Virtusa Virtusa.com (a global provider of software development services)
� fcspartners.fi
� Siruis Software
� Cardinal Health, Dublin, OH 43017
� BerMark Dynamics Inc.
� Tavant Technologies
� Asset Group, part of SAIC - fortune 500. asset.com
� Haywood Associates
� SpinSoftware
� The Middleware Company
� atSistemas (atsistemas.com)
� Wells Fargo
� IntelliJ
� Perot Systems (trcinc.com)
� ESRI (GPS and mapping software specialists) esri.com
� Oracle
� ARINC "the world leader in transportation communications and systems engineering" Arinc.com
� MAN-Corporate
� BEA
� Bell Canada (sympatico.ca)
� Dimension Data http://www.didata.com/na/contactus.asp
� Husky Injection Molding Systems husky.ca
� Covansys Global Technology Services covansys.com
� OleCenter Addecco Group olecenter.com
� Bell South (.net)
� EDS
� Simtel Group Simtel.ie
� HP
� Data Transfer datatransfer.com.ar
� matrix-media.com
� MetCash (marketing an distribution) metcash.com
� SG Corporate & Investmen Banking (sgcib.com
� Siemens
� Computer Sciences Corporation csc.com
� hpsglobal.com
� CheckFree
� Allstate Insurance
� iceCentric (icecentric.com) - CRM company
� Intuit intuit.com
� Disney disney.com
� CrimsonLogic crimsonlogic.com
� Reflective Solutions reflective.com (J2EE consultancy)
� New Aspects of Software
� etracksolutions.com
� Nulogy Corporation nulogy.com
� SAS sas.com
� VMS vmsinfo.com
� Deloitte dc.com
� Global Net Services gnsi.com
� A G Edwards (finance company) AGEDWARDS.com
� Akzo Nobel (pharmaceuticals) AkzoNobel.com
*posters to AspectJ usermailing list, Jan-May 2004,where affiliation canbe determined.
34© IBM Corporation
Summary
� Object Oriented Development has some shortcomings for coping with certain kinds of feature/concern
– AOP can help solve this problem
� You need tools support
� Adopt AOP through managed phases– Get increasing benefit as your confidence and expertise grows
� Who’s doing it?– IBM is doing it– Other AspectJ users are doing it– You can do it too!
35© IBM Corporation
Beyond Java …
� Obviously– there are languages other than Java
� BPEL, WSDL, C, C++, etc, etc…
– there are artifacts other than code� Documentation, build scripts, UML models
� IBM is exploring this space– check out the CME project http://eclipse.org/cme
36© IBM Corporation
More information…
� AspectJ homepage– http://eclipse.org/aspectj
� AJDT homepage– http://eclipse.org/ajdt
� Books– ‘AspectJ in Action’
� ISBN 1930110936 – ‘Eclipse AspectJ’
� ISBN 0321245873
� Contact me!– Andy Clement– [email protected]
37© IBM Corporation
The End