Pointcut
In aspect-oriented computer programming, a pointcut is a set of join points. Pointcut allows where exactly to apply advice, this allows separation of concerns and helps in modularizing business logic.[1] Pointcuts are often specified using class names or method names in some cases using regular expressions that match class or method name. Different frameworks support different Pointcut expressions, AspectJ syntax is considered as de facto standard. Frameworks are available for various programming languages like Java, Perl, Ruby, and many more which support pointcut.
Background
Due to limitations in various programming languages, cross cutting concerns have not modularized. Cross cutting concerns refers to parts of software that logically belong to one module and affect the whole system, this could be security, logging or many other.[2] Aspect oriented programming tries to solve these cross cutting concerns in the following way, It allows programmer to write module called as aspects. Aspects contains piece of code which is executed at particular point. Expression required to select particular point led to creation of Pointcut Expressions.
Execution
Whenever the program execution reaches one of the join points described in the pointcut, a piece of code associated with the pointcut (called advice) is executed. This allows a programmer to describe where and when additional code should be executed in addition to an already defined behavior. This permits the addition of aspects to existing software, or the design of software with a clear separation of concerns, wherein the programmer weaves (merges) different aspects into a complete application.
Suppose there is an application where we can modify records in database. Whenever users modify the database and we want to have a log of information, regarding who is modifying the records. Traditional way to log is to call log method just before modify database method. With the Aspect oriented programming, we can apply pointcut to modify database method and have an advice that is called to log the required information.[3]
Expressions
Some of the important Pointcut expressions supported by AspectJ. These expressions can be combined using logical operators.[4]
execution(void User.setPassword(password))
This pointcut matches execution of Users.setPassword method.
call(void User.getPassword())
When a Users.getPassword is called, this pointcut is matched.
handler(ArrayIndexOutOfBounds)
Pointcut will match when there is ArrayIndexOutOfBounds exception
this(UserType)
Pointcut will match when object currently executing is of UserType
target(UserType)
Pointcut will match when target object is of UserType
within(UserType)
Pointcut will match when code executing belongs to UserType.
Criticisms
Pointcut languages impacts important software properties like evolvability and comprehensibility, in a negative way. There might be a possibility where there is a need to perform refactoring to define a correct aspect, which in general should not happen since refactoring is to make code cleaner. It is also not scalable when there are multiple aspects to be applied on the same code and each aspect requiring a different refactoring.[5] In general every aspect will be tightly coupled with an application’s structure as the pointcuts explicitly contain a method’s signature. So when an application changes the pointcut needs to be changed as well. This is quite problematic for a developer.[5]
References
- ^ "A Classification of Pointcut Language Constructs" (PDF). Retrieved 13 September 2016.
- ^ "Introduction to AspectJ". Retrieved 14 September 2016.
- ^ "JBoss AOP - User Guide". docs.jboss.org. Retrieved 2016-09-14.
- ^ "Join Points and Pointcuts". Retrieved 14 September 2016.
- ^ a b "Inductively generated PointCuts to support refactoring to Aspects". Retrieved 14 September 2016.
External links
- Paper "Back to the Future: Pointcuts as Predicates over Traces" by Karl Klose and Klaus Ostermann
- Paper "Remote Pointcut - A Language Construct for Distributed AOP" by Muga Nishizawa, Shigeru Chiba and Michiaki Tatsubori
- Paper "Datalog as a Pointcut Language in Aspect-Oriented Programming"
- Paper "Expressiveness and Complexity of Crosscut Languages" by Karl J. Lieberherr, Jeffrey Palm and Ravi Sundaram