{"id":686,"date":"2020-09-27T19:36:34","date_gmt":"2020-09-27T16:36:34","guid":{"rendered":"https:\/\/aliyargunes.space\/blog\/?p=686"},"modified":"2020-10-07T01:04:22","modified_gmt":"2020-10-06T22:04:22","slug":"aspect-oriented-programming-notes-3-some-additional-info","status":"publish","type":"post","link":"https:\/\/aliyargunes.com.tr\/blog\/aspect-oriented-programming-notes-3-some-additional-info\/","title":{"rendered":"Aspect Oriented Programming Notes #3 &#8211; Some Additional Info"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Reuse Aspect Pointcut Definitions<\/h2>\n\n\n\n<p><code>@Pointcut<\/code> annotation is used for defining a pointcut independently to be reused in multiple advices.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; title: ; notranslate\" title=\"\">\nimport org.aspectj.lang.JoinPoint;\nimport org.aspectj.lang.ProceedingJoinPoint;\nimport org.aspectj.lang.annotation.AfterReturning;\nimport org.aspectj.lang.annotation.AfterThrowing;\nimport org.aspectj.lang.annotation.Around;\nimport org.aspectj.lang.annotation.Aspect;\nimport org.aspectj.lang.annotation.Before;\nimport org.aspectj.lang.annotation.Pointcut;\nimport org.springframework.stereotype.Component;\n\n@Aspect\n@Component\npublic class ReuseCalculatorLoggingAspect {\n\n\t@Pointcut(&quot;execution(* *.*(..))&quot;)\n\tprivate void loggingOperation() {\n\t\t\n\t}\n\t\n\t@Before(&quot;loggingOperation()&quot;)\n\tpublic void logBefore(JoinPoint joinPoint) {\n\t\n\t}\n\t\n\t@AfterReturning(pointcut = &quot;loggingOperation()&quot;, returning = &quot;result&quot;)\n\tpublic void logAfterReturning(JoinPoint joinPoint, Object result) {\n\t\n\t}\n\t\n\t@AfterThrowing(pointcut = &quot;loggingOperation()&quot;,\tthrowing = &quot;e&quot;)\n\tpublic void logAfterThrowing(JoinPoint joinPoint, IllegalArgumentException e) {\n\t\n\t}\n\n\t@Around(&quot;loggingOperation()&quot;)\n\tpublic Object logAround(ProceedingJoinPoint joinPoint) throws Throwable {\n\t\treturn null;\n\t}\n\n}\n<\/pre><\/div>\n\n\n<p>Generally, if our pointcuts are shared between multiple aspects, it is better to centralize them in a common class. In this case, they must be declared as public.  When we refer to this pointcut, we have to include the class name as well. If the class is not located in<br>the same package as the aspect, we have to include the package name also.<\/p>\n\n\n\n<!--more-->\n\n\n\n<h2 class=\"wp-block-heading\">Use Custom Annotation for Matching Join Point<\/h2>\n\n\n\n<p>Sometimes we may not be able to find any common characteristics (e.g., modifiers, return types, method name patterns, or arguments) for the methods we want to match. In such cases, we can consider providing a custom annotation for them. For instance, defining the following marker annotation. This annotation can be applied to both method level and type level.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; title: ; notranslate\" title=\"\">\n@Target( { ElementType.METHOD, ElementType.TYPE })\n@Retention(RetentionPolicy.RUNTIME)\n@Documented\npublic @interface LoggingRequired {\n\n}\n<\/pre><\/div>\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; title: ; notranslate\" title=\"\">\n@LoggingRequired\npublic class ArithmeticCalculatorImpl implements ArithmeticCalculator {\n   public double add(double a, double b) {\n    ...\n   }\n   public double sub(double a, double b) {\n    ...\n   }\n}\n<\/pre><\/div>\n\n\n<p>Then we can write a pointcut expression to match a class or methods with the <code>@LoggingRequired<\/code> annotation using the annotation keyword on the <code>@Pointcut<\/code> annotation.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; title: ; notranslate\" title=\"\">\n@Aspect\npublic class CalculatorPointcuts {\n    @Pointcut(&quot;annotation(com.aopexample.LoggingRequired)&quot;)\n    public void loggingOperation() {\n\n    }\n}\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">Use Type Signature Patterns<\/h2>\n\n\n\n<p>Another kind of pointcut expression matches all join points within certain types. When applied to Spring AOP, the scope of these pointcuts will be narrowed to matching all method executions within the types.<\/p>\n\n\n\n<p>The following pointcut matches all the method execution join points within the <code>com.aopexample<\/code> package:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; title: ; notranslate\" title=\"\">\nwithin(com.aopexample.*)\n<\/pre><\/div>\n\n\n<p>To match the join points within a package and its subpackage, we have to add one more dot before the wildcard.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; title: ; notranslate\" title=\"\">\nwithin(com.aopexample..*)\n<\/pre><\/div>\n\n\n<p>The following pointcut expression matches the method execution join points within a particular class:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; title: ; notranslate\" title=\"\">\nwithin(com.aopexample.ArithmeticCalculatorImpl)\n<\/pre><\/div>\n\n\n<p>If the target class is located in the same package as this aspect, the package name can be omitted:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; title: ; notranslate\" title=\"\">\nwithin(ArithmeticCalculatorImpl)\n<\/pre><\/div>\n\n\n<p>We can match the method execution join points within all classes that implement the<br>ArithmeticCalculator interface by adding a plus symbol.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; title: ; notranslate\" title=\"\">\nwithin(ArithmeticCalculator+)\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">Combine (Conditional) Pointcut Expressions<\/h2>\n\n\n\n<p>In AspectJ, pointcut expressions can be combined with the operators &amp;&amp; (and), || (or), and ! (not).<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; title: ; notranslate\" title=\"\">\n@Aspect\npublic class CalculatorPointcuts {\n\t@Pointcut(&quot;within(ArithmeticCalculator+)&quot;)\n\tpublic void arithmeticOperation() {\n\t}\n\n\t@Pointcut(&quot;within(UnitCalculator+)&quot;)\n\tpublic void unitOperation() {\n\t}\n\n\t@Pointcut(&quot;arithmeticOperation() || unitOperation()&quot;)\n\tpublic void loggingOperation() {\n\t}\n}\n\n<\/pre><\/div>\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Reuse Aspect Pointcut Definitions @Pointcut annotation is used for defining a pointcut independently to be reused in multiple advices. Generally, if our pointcuts are shared between multiple aspects, it is better to centralize them in a common class. In this &hellip; <a href=\"https:\/\/aliyargunes.com.tr\/blog\/aspect-oriented-programming-notes-3-some-additional-info\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[94,8],"tags":[97,109,107,106,108],"class_list":["post-686","post","type-post","status-publish","format-standard","hentry","category-spring-framework","category-works","tag-aspect-oriented-programming","tag-combine-pointcut-expressions","tag-custom-annotation-for-matching-join-point","tag-reuse-aspect-point-defibnitions","tag-type-signature-patterns"],"_links":{"self":[{"href":"https:\/\/aliyargunes.com.tr\/blog\/wp-json\/wp\/v2\/posts\/686"}],"collection":[{"href":"https:\/\/aliyargunes.com.tr\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/aliyargunes.com.tr\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/aliyargunes.com.tr\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/aliyargunes.com.tr\/blog\/wp-json\/wp\/v2\/comments?post=686"}],"version-history":[{"count":18,"href":"https:\/\/aliyargunes.com.tr\/blog\/wp-json\/wp\/v2\/posts\/686\/revisions"}],"predecessor-version":[{"id":747,"href":"https:\/\/aliyargunes.com.tr\/blog\/wp-json\/wp\/v2\/posts\/686\/revisions\/747"}],"wp:attachment":[{"href":"https:\/\/aliyargunes.com.tr\/blog\/wp-json\/wp\/v2\/media?parent=686"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/aliyargunes.com.tr\/blog\/wp-json\/wp\/v2\/categories?post=686"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/aliyargunes.com.tr\/blog\/wp-json\/wp\/v2\/tags?post=686"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}