FazBrowse Wikipedia Viewer | Main Page |
Search Wikipedia:
|
Viewing: https://en.wikipedia.org/wiki/Aspect-oriented_programming

Aspect-oriented programming

Contents
    Other potential join point models
    Inter-type declarations
Comparison to other programming paradigms
Adoption issues
Implementations
See also
Notes and references

In computing, aspect-oriented programming (AOP) is a programming paradigm that aims to increase modularity by allowing the separation of cross-cutting concerns. It does so by adding behavior to existing code (an advice) without modifying the code, instead separately specifying which code is modified via a "pointcut" specification, such as "log all function calls when the function's name begins with 'setimportjava.util.logging.*; sealedclass BankingException extendsException permitsInsufficientFundsException,UnauthorisedUserException{ // ... } publicclass Bank{ privatestaticfinalLoggerlogger; privatefinalDatabasedatabase; publicvoidtransfer(AccountfromAcc,AccounttoAcc,intamount,Useruser)throwsBankingException{ logger.info("Transferring money..."); if(!isUserAuthorised(user,fromAcc)){ logger.log(Level.WARNING,"User has no permission."); thrownewUnauthorisedUserException(); } if(fromAcc.getBalance()<amount){ logger.log(Level.WARNING,"Insufficient funds."); thrownewInsufficientFundsException(); } fromAcc.withdraw(amount); toAcc.deposit(amount); database.commitChanges();// Atomic operation. logger.log(Level.INFO,"Transaction successful."); } }

In this example, other interests have become tangled with the basic functionality (sometimes called the business logic concern). Transactions, security, and logging all exemplify cross-cutting concerns.

Now consider what would happen if we suddenly need to change the security considerations for the application. In the program's current version, security-related operations appear scattered across numerous methods, and such a change would require major effort.

AOP tries to solve this problem by allowing the programmer to express cross-cutting concerns in stand-alone modules called aspects. Aspects can contain advice (code joined to specified points in the program) and inter-type declarations (structural members added to other classes). For example, a security module can include advice that performs a security check before accessing a bank account. The pointcut defines the times (join points) when one can access a bank account, and the code in the advice body defines how the security check is implemented. That way, both the check and the places can be maintained in one place. Further, a good pointcut can anticipate later program changes, so if another developer creates a new method to access the bank account, the advice will apply to the new method when it executes.

So for the example above implementing logging in an aspect:

set</code>\" and there is exactly one argument of any type.\n\n\"Dynamic\" PCDs check runtime types and bind variables. For example,\n<syntaxhighlight lang=\"aspectj\">\nthis(Point)\n</syntaxhighlight>\nThis pointcut matches when the currently executing object is an instance of class <code>Point</code>. Note that the unqualified name of a class can be used via Java's normal type lookup.\n\n\"Scope\" PCDs limit the lexical scope of the join point. For example:\n<syntaxhighlight lang=\"aspectj\">\nwithin(com.company.*)\n</syntaxhighlight>\nThis pointcut matches any join point in any type in the <code>com.company</code> package. The ''<code>*</code>'' is one form of the wildcards that can be used to match many things with one signature.\n\nPointcuts can be composed and named for reuse. For example:\n<syntaxhighlight lang=\"aspectj\">\npointcut set() : execution(* set*(*) ) && this(Point) && within(com.company.*);\n</syntaxhighlight>\nThis pointcut matches a method-execution join point, if the method name starts with \"<code>set</code>\" and <code>this</code> is an instance of type <code>Point</code> in the <code>com.company</code> package. It can be referred to using the name \"<code>set()</code>\".\n\n"},"3":{"wt":" Advice specifies to run at (before, after, or around) a join point (specified with a pointcut) certain code (specified like code in a method). The AOP runtime invokes Advice automatically when the pointcut matches the join point. For example:\n<syntaxhighlight lang=\"aspectj\">\nafter() : set() {\n Display.update();\n}\n</syntaxhighlight>\n\nThis effectively specifies: \"if the ''<code>set()</code>'' pointcut matches the join point, run the code <code>Display.update()</code> after the join point completes.\""}},"i":0}}]}'>
  • The join points in AspectJ include method or constructor call or execution, the initialization of a class or object, field read and write access, and exception handlers. They do not include loops, super calls, throws clauses, or multiple statements.
  • Pointcuts are specified by combinations of primitive pointcut designators (PCDs). "Kinded" PCDs match a particular kind of join point (e.g., method execution) and often take a Java-like signature as input. One such pointcut looks like this:
    after():set(){ Display.update(); }
    This effectively specifies: "if the set() pointcut matches the join point, run the code Display.update() after the join point completes."

Other potential join point models

edit

There are other kinds of JPMs. All advice languages can be defined in terms of their JPM. For example, a hypothetical aspect language for UML may have the following JPM:

Inter-type declarations

edit

Inter-type declarations provide a way to express cross-cutting concerns affecting the structure of modules. Also known as open classes and extension methods, this enables programmers to declare in one place members or parents of another class, typically to combine all the code related to a concern in one aspect. For example, if a programmer implemented the cross-cutting display-update concern using visitors, an inter-type declaration using the visitor pattern might look like this in AspectJ:

Even though most classes in an object-oriented model will perform a single, specific function, they often share common, secondary requirements with other classes. For example, we may want to add logging to classes within the data-access layer and also to classes in the UI layer whenever a thread enters or exits a method. Further concerns can be related to security such as access control or information flow control. Even though each class has a very different primary functionality, the code needed to perform the secondary functionality is often identical.
Advice
The combination of the pointcut and the advice is termed an aspect. In the example above, we add a logging aspect to our application by defining a pointcut and giving the correct advice.

Comparison to other programming paradigms

edit

Aspects emerged from object-oriented programming and reflective programming. AOP languages have functionality similar to, but more restricted than, metaobject protocols. Aspects relate closely to programming concepts like subjects, mixins, and delegation. Other ways to use aspect-oriented programming paradigms include Composition Filters and the hyperslices approach. Since at least the 1970s, developers have been using forms of interception and dispatch-patching that resemble some of the implementation methods for AOP, but these never had the semantics that the cross-cutting specifications provide in one place.

Adoption issues

edit

Programmers need to be able to read and understand code to prevent errors. Even with proper education, understanding cross-cutting concerns can be difficult without proper support for visualizing both static structure and the dynamic flow of a program. Starting in 2002, AspectJ began to provide IDE plug-ins to support the visualizing of cross-cutting concerns. Those features, as well as aspect code assist and refactoring, are now common.

Given the power of AOP, making a logical mistake in expressing cross-cutting can lead to widespread program failure. Conversely, another programmer may change the join points in a program, such as by renaming or moving methods, in ways that the aspect writer did not anticipate and with unforeseen consequences. One advantage of modularizing cross-cutting concerns is enabling one programmer to easily affect the entire system. As a result, such problems manifest as a conflict over responsibility between two or more developers for a given failure. AOP can expedite solving these problems, as only the aspect must be changed. Without AOP, the corresponding problems can be much more spread out.main(){ inputx print(result(x)) } inputresult(intx){ returnx } around(intx):call(result(int))&&args(x){ inttemp=proceed(x) returntemp*temp }

Indeed, the pointcut may depend on runtime condition and thus not be statically deterministic. This can be mitigated but not solved by static analysis and IDE support showing which advices potentially match.

General criticisms are that AOP purports to improve "both modularity and the structure of code", but some counter that it instead undermines these goals and impedes "independent development and understandability of programs". Specifically, quantification by pointcuts breaks modularity: "one must, in general, have whole-program knowledge to reason about the dynamic execution of an aspect-oriented program." Further, while its goals (modularizing cross-cutting concerns) are well understood, its actual definition is unclear and not clearly distinguished from other well-established techniques. Cross-cutting concerns potentially cross-cut each other, requiring some resolution mechanism, such as ordering. Indeed, aspects can apply to themselves, leading to problems such as the liar paradox.

Technical criticisms include that the quantification of pointcuts (defining where advices are executed) is "extremely sensitive to changes in the program", which is known as the fragile pointcut problem. The problems with pointcuts are deemed intractable. If one replaces the quantification of pointcuts with explicit annotations, one obtains attribute-oriented programming instead, which is simply an explicit subroutine call and suffers the identical problem of scattering, which AOP was designed to solve.

Implementations

edit

Many programming languages have implemented AOP, within the language, or as an external library, including:

See also

edit

Notes and references

edit
Don Box; Chris Sells (4 November 2002). Essential.NET: The common language runtime. Addison-Wesley Professional. p. 206. ISBN 978-0-201-73411-9. Retrieved 4 October 2011.
  • "Archived copy" (PDF). Archived from the original (PDF) on 8 October 2005. Retrieved 19 June 2005.{{cite web}}: CS1 maint: archived copy as title (link)
  • B. De Win, B. Vanhaute and B. De Decker. "Security through aspect-oriented programming". In Advances in Network and Distributed Systems Security (2002).
  • T. Pasquier, J. Bacon and B. Shand. "FlowR: Aspect Oriented Programming for Information Flow Control in Ruby". In ACM Proceedings of the 13th international conference on Modularity (Aspect Oriented Software Development) (2014).
  • Edsger Dijkstra, Notes on Structured Programming Archived 2006-10-12 at the Wayback Machine, pg. 1-2
  • 1 2 3 4 "More Modular Reasoning for Aspect-Oriented Programs". Archived from the original on 12 August 2015. Retrieved 11 August 2015.
  • "Ada2012 Rationale" (PDF). adacore.com. Archived (PDF) from the original on 18 April 2016. Retrieved 5 May 2018.
  • "Cobble". vub.ac.be. Retrieved 5 May 2018."ColdSpring Framework: Welcome". 5 November 2005. Archived from the original on 5 November 2005. Retrieved 5 May 2018.{{cite web}}: CS1 maint: bot: original URL status unknown (link)
  • "meaop – MeSDK: MeObjects, MeRTTI, MeAOP – Delphi AOP(Aspect Oriented Programming), MeRemote, MeService... – Google Project Hosting". Archived from the original on 10 September 2015. Retrieved 11 August 2015.
  • "Emacs Advice Functions". GNU Project. Archived from the original on 24 October 2011. Retrieved 5 May 2018.
  • Monads allow program semantics to be altered by changing the type of the program without altering its code: "MAKAO, re(verse)-engineering build systems". Retrieved 11 August 2015.{{cite web}}: CS1 maint: deprecated archival service (link)
  • "nemerle/README.md at master · rsdn/nemerle". GitHub. Retrieved 22 March 2018.
  • "Aspect-Oriented Programming in Prolog". bigzaphod.org. 14 December 2005. Archived from the original on 3 March 2012. Retrieved 5 May 2018.
  • Several: PEAK Archived 2005-04-09 at the Wayback Machine, Aspyct AOP, Lightweight Python AOP Archived 2004-10-09 at the Wayback Machine, Logilab's aspect module Archived 2005-03-09 at the Wayback Machine, Pythius Archived 2005-04-08 at the Wayback Machine, Spring Python's AOP module Archived 2016-03-04 at the Wayback Machine, Pytilities' AOP module Archived 2011-08-25 at the Wayback Machine, aspectlib Archived 2014-11-05 at the Wayback Machine
  • dutchyn > aspectscheme.plt"},"access-date":{"wt":"11 August 2015"},"url-status":{"wt":"live"},"archive-url":{"wt":"https://web.archive.org/web/20150905062740/http://planet.racket-lang.org/display.ss?package=aspectscheme.plt&owner=dutchyn"},"archive-date":{"wt":"5 September 2015"}},"i":0}}]}'/>"PLaneT Package Repository : PLaneT > dutchyn > aspectscheme.plt". Archived from the original on 5 September 2015. Retrieved 11 August 2015.
  • "gcao/aspector". GitHub. Archived from the original on 4 January 2015. Retrieved 11 August 2015.
  • "WEAVR". iit.edu. Archived from the original on 12 December 2008. Retrieved 5 May 2018.
  • Laddad, Ramnivas (2003). AspectJ in Action: Practical Aspect-Oriented Programming. Manning. ISBN 978-1-930110-93-9.
  • Raghu Yedduladoddi (2009). Aspect Oriented Software Development: An Approach to Composing UML Design Models. VDM. ISBN 978-3-639-12084-4.
  • "Adaptive Object-Oriented Programming Using Graph-Based Customization" – Lieberherr, Silva-Lepe, et al. – 1994

  • View original on Wikipedia | Back | FazBrowse Home