AspectJ weaving for private methods












0















I've got an AspectJ weaving annotation that works for public methods, but private method are being ignored.
The purpose of this method is to simply log the time it took to run the function.



@Aspect
@Slf4j
public class TimedLogAspect {

@Pointcut("@annotation(timedLogVar)")
public void annotationPointCutDefinition(TimedLog timedLogVar) {}

@Pointcut("execution(* *(..))")
public void atExecution() {}

@Around(value = "annotationPointCutDefinition(timedLogVar) && atExecution()", argNames = "joinPoint,timedLogVar")
public Object around(ProceedingJoinPoint joinPoint, TimedLog timedLogVar) throws Throwable {
Stopwatch stopwatch = Stopwatch.createStarted();
Object returnValue = joinPoint.proceed();
stopwatch.stop();

MessageBuilder messageBuilder = new MessageBuilder(joinPoint.toShortString(), stopwatch.elapsed(TimeUnit.MILLISECONDS))
.attachMessage(timedLogVar.message())
.attachMethodArgs(timedLogVar.shouldAttachMethodArgs(), Stream.of(joinPoint.getArgs()).collect(Collectors.toList()))
.attachReturnValue(timedLogVar.shouldAttachReturnValue(), returnValue);

log.info(messageBuilder.build(), messageBuilder.getArgs().toArray());

return returnValue;
}
}


with this being the actual interface:



@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface TimedLog {
boolean shouldAttachMethodArgs() default false;
boolean shouldAttachReturnValue() default false;
String message() default "";
}


I've seen a lot of answer, being adding private before the first * in the execution portion, I've seen privileged which isn't supported for annotations, and I'm using AspectJ with no SpringAOP.



any ideas?










share|improve this question



























    0















    I've got an AspectJ weaving annotation that works for public methods, but private method are being ignored.
    The purpose of this method is to simply log the time it took to run the function.



    @Aspect
    @Slf4j
    public class TimedLogAspect {

    @Pointcut("@annotation(timedLogVar)")
    public void annotationPointCutDefinition(TimedLog timedLogVar) {}

    @Pointcut("execution(* *(..))")
    public void atExecution() {}

    @Around(value = "annotationPointCutDefinition(timedLogVar) && atExecution()", argNames = "joinPoint,timedLogVar")
    public Object around(ProceedingJoinPoint joinPoint, TimedLog timedLogVar) throws Throwable {
    Stopwatch stopwatch = Stopwatch.createStarted();
    Object returnValue = joinPoint.proceed();
    stopwatch.stop();

    MessageBuilder messageBuilder = new MessageBuilder(joinPoint.toShortString(), stopwatch.elapsed(TimeUnit.MILLISECONDS))
    .attachMessage(timedLogVar.message())
    .attachMethodArgs(timedLogVar.shouldAttachMethodArgs(), Stream.of(joinPoint.getArgs()).collect(Collectors.toList()))
    .attachReturnValue(timedLogVar.shouldAttachReturnValue(), returnValue);

    log.info(messageBuilder.build(), messageBuilder.getArgs().toArray());

    return returnValue;
    }
    }


    with this being the actual interface:



    @Retention(RetentionPolicy.RUNTIME)
    @Target(ElementType.METHOD)
    public @interface TimedLog {
    boolean shouldAttachMethodArgs() default false;
    boolean shouldAttachReturnValue() default false;
    String message() default "";
    }


    I've seen a lot of answer, being adding private before the first * in the execution portion, I've seen privileged which isn't supported for annotations, and I'm using AspectJ with no SpringAOP.



    any ideas?










    share|improve this question

























      0












      0








      0








      I've got an AspectJ weaving annotation that works for public methods, but private method are being ignored.
      The purpose of this method is to simply log the time it took to run the function.



      @Aspect
      @Slf4j
      public class TimedLogAspect {

      @Pointcut("@annotation(timedLogVar)")
      public void annotationPointCutDefinition(TimedLog timedLogVar) {}

      @Pointcut("execution(* *(..))")
      public void atExecution() {}

      @Around(value = "annotationPointCutDefinition(timedLogVar) && atExecution()", argNames = "joinPoint,timedLogVar")
      public Object around(ProceedingJoinPoint joinPoint, TimedLog timedLogVar) throws Throwable {
      Stopwatch stopwatch = Stopwatch.createStarted();
      Object returnValue = joinPoint.proceed();
      stopwatch.stop();

      MessageBuilder messageBuilder = new MessageBuilder(joinPoint.toShortString(), stopwatch.elapsed(TimeUnit.MILLISECONDS))
      .attachMessage(timedLogVar.message())
      .attachMethodArgs(timedLogVar.shouldAttachMethodArgs(), Stream.of(joinPoint.getArgs()).collect(Collectors.toList()))
      .attachReturnValue(timedLogVar.shouldAttachReturnValue(), returnValue);

      log.info(messageBuilder.build(), messageBuilder.getArgs().toArray());

      return returnValue;
      }
      }


      with this being the actual interface:



      @Retention(RetentionPolicy.RUNTIME)
      @Target(ElementType.METHOD)
      public @interface TimedLog {
      boolean shouldAttachMethodArgs() default false;
      boolean shouldAttachReturnValue() default false;
      String message() default "";
      }


      I've seen a lot of answer, being adding private before the first * in the execution portion, I've seen privileged which isn't supported for annotations, and I'm using AspectJ with no SpringAOP.



      any ideas?










      share|improve this question














      I've got an AspectJ weaving annotation that works for public methods, but private method are being ignored.
      The purpose of this method is to simply log the time it took to run the function.



      @Aspect
      @Slf4j
      public class TimedLogAspect {

      @Pointcut("@annotation(timedLogVar)")
      public void annotationPointCutDefinition(TimedLog timedLogVar) {}

      @Pointcut("execution(* *(..))")
      public void atExecution() {}

      @Around(value = "annotationPointCutDefinition(timedLogVar) && atExecution()", argNames = "joinPoint,timedLogVar")
      public Object around(ProceedingJoinPoint joinPoint, TimedLog timedLogVar) throws Throwable {
      Stopwatch stopwatch = Stopwatch.createStarted();
      Object returnValue = joinPoint.proceed();
      stopwatch.stop();

      MessageBuilder messageBuilder = new MessageBuilder(joinPoint.toShortString(), stopwatch.elapsed(TimeUnit.MILLISECONDS))
      .attachMessage(timedLogVar.message())
      .attachMethodArgs(timedLogVar.shouldAttachMethodArgs(), Stream.of(joinPoint.getArgs()).collect(Collectors.toList()))
      .attachReturnValue(timedLogVar.shouldAttachReturnValue(), returnValue);

      log.info(messageBuilder.build(), messageBuilder.getArgs().toArray());

      return returnValue;
      }
      }


      with this being the actual interface:



      @Retention(RetentionPolicy.RUNTIME)
      @Target(ElementType.METHOD)
      public @interface TimedLog {
      boolean shouldAttachMethodArgs() default false;
      boolean shouldAttachReturnValue() default false;
      String message() default "";
      }


      I've seen a lot of answer, being adding private before the first * in the execution portion, I've seen privileged which isn't supported for annotations, and I'm using AspectJ with no SpringAOP.



      any ideas?







      java spring aspectj






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 25 '18 at 9:21









      Dor MunisDor Munis

      133




      133
























          1 Answer
          1






          active

          oldest

          votes


















          0














          The answer is simple: use native AspectJ syntax. You mentioned it by yourself. You even said that you use full AspectJ, not Spring AOP. So switching should not be a problem.



          You have lots of advantages:




          • more power (some features are unavailable in annotation syntax, as you noticed),

          • syntax highlighting and code completion in IDEs,

          • more expressive syntax (less verbose and more elegant, i.e. no need to bind thisJoinPoint in advices and easier use of if(), no need for fully-qualified class names because you can just import them).


          The annotation-based syntax IMO is just terribly difficult to read, everything is in a single string inside an annotation parameter. I only use it if I have no other choice or when answering questions about it here.






          share|improve this answer
























          • But I couldn't find a way to use the syntax and pointcut to every method (instead of just 1). Do you have any examples of working code?

            – Dor Munis
            Nov 25 '18 at 14:04













          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53466151%2faspectj-weaving-for-private-methods%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          The answer is simple: use native AspectJ syntax. You mentioned it by yourself. You even said that you use full AspectJ, not Spring AOP. So switching should not be a problem.



          You have lots of advantages:




          • more power (some features are unavailable in annotation syntax, as you noticed),

          • syntax highlighting and code completion in IDEs,

          • more expressive syntax (less verbose and more elegant, i.e. no need to bind thisJoinPoint in advices and easier use of if(), no need for fully-qualified class names because you can just import them).


          The annotation-based syntax IMO is just terribly difficult to read, everything is in a single string inside an annotation parameter. I only use it if I have no other choice or when answering questions about it here.






          share|improve this answer
























          • But I couldn't find a way to use the syntax and pointcut to every method (instead of just 1). Do you have any examples of working code?

            – Dor Munis
            Nov 25 '18 at 14:04


















          0














          The answer is simple: use native AspectJ syntax. You mentioned it by yourself. You even said that you use full AspectJ, not Spring AOP. So switching should not be a problem.



          You have lots of advantages:




          • more power (some features are unavailable in annotation syntax, as you noticed),

          • syntax highlighting and code completion in IDEs,

          • more expressive syntax (less verbose and more elegant, i.e. no need to bind thisJoinPoint in advices and easier use of if(), no need for fully-qualified class names because you can just import them).


          The annotation-based syntax IMO is just terribly difficult to read, everything is in a single string inside an annotation parameter. I only use it if I have no other choice or when answering questions about it here.






          share|improve this answer
























          • But I couldn't find a way to use the syntax and pointcut to every method (instead of just 1). Do you have any examples of working code?

            – Dor Munis
            Nov 25 '18 at 14:04
















          0












          0








          0







          The answer is simple: use native AspectJ syntax. You mentioned it by yourself. You even said that you use full AspectJ, not Spring AOP. So switching should not be a problem.



          You have lots of advantages:




          • more power (some features are unavailable in annotation syntax, as you noticed),

          • syntax highlighting and code completion in IDEs,

          • more expressive syntax (less verbose and more elegant, i.e. no need to bind thisJoinPoint in advices and easier use of if(), no need for fully-qualified class names because you can just import them).


          The annotation-based syntax IMO is just terribly difficult to read, everything is in a single string inside an annotation parameter. I only use it if I have no other choice or when answering questions about it here.






          share|improve this answer













          The answer is simple: use native AspectJ syntax. You mentioned it by yourself. You even said that you use full AspectJ, not Spring AOP. So switching should not be a problem.



          You have lots of advantages:




          • more power (some features are unavailable in annotation syntax, as you noticed),

          • syntax highlighting and code completion in IDEs,

          • more expressive syntax (less verbose and more elegant, i.e. no need to bind thisJoinPoint in advices and easier use of if(), no need for fully-qualified class names because you can just import them).


          The annotation-based syntax IMO is just terribly difficult to read, everything is in a single string inside an annotation parameter. I only use it if I have no other choice or when answering questions about it here.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 25 '18 at 12:00









          kriegaexkriegaex

          31.7k366101




          31.7k366101













          • But I couldn't find a way to use the syntax and pointcut to every method (instead of just 1). Do you have any examples of working code?

            – Dor Munis
            Nov 25 '18 at 14:04





















          • But I couldn't find a way to use the syntax and pointcut to every method (instead of just 1). Do you have any examples of working code?

            – Dor Munis
            Nov 25 '18 at 14:04



















          But I couldn't find a way to use the syntax and pointcut to every method (instead of just 1). Do you have any examples of working code?

          – Dor Munis
          Nov 25 '18 at 14:04







          But I couldn't find a way to use the syntax and pointcut to every method (instead of just 1). Do you have any examples of working code?

          – Dor Munis
          Nov 25 '18 at 14:04






















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53466151%2faspectj-weaving-for-private-methods%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          404 Error Contact Form 7 ajax form submitting

          How to know if a Active Directory user can login interactively

          Refactoring coordinates for Minecraft Pi buildings written in Python