title: AspectJ does not execute advice on reflection
body: <p>I have a class:</p>
  
  <pre><code>class MyClass {
      @MyAnnotation
      public void someMethod() {
  
      }
  }
  </code></pre>
  
  <p>In the advice for the pointcut, I am basically extracting the 'Method' that the annotation is for. Then, the method is delegated to one of the many handlers for it depending on some configuration.</p>
  
  <p>At this point, I must point out, that the source of all trouble is that I can't really do a <code>joinPoint.proceed()</code> in the advice. <code>Method.invoke()</code> kind of needs to happen on the method because there is another delegate that in a parallel thread keeps on invoking that said method, and the execution of that doesn't have a <code>ProceedingJoinPoint</code>.</p>
  
  <p>The problem is that, whenever I do a <code>Method.invoke()</code> on <code>someMethod</code>, the advice runs again (of course), since it has the annotation on it. This causes it to run again and again and again.</p>
  
  <p>I need to way to either:</p>
  
  <ol>
  <li>Don't run the advice if invoked by reflections or Method.invoke (I don't really think it is possible).</li>
  <li>Detect, within the advice, if the call is from one of the delegates.</li>
  </ol>
  
  <p>For #2, I considered setting up a flag or something, but that doesn't keep it thread-safe anymore.</p>
  
  <p>Any suggestions?</p>
  
  <p>A more clarified code example:</p>
  
  <p>I have a defined Aspect:</p>
  
  <pre><code>@Pointcut("execution(@com.rohanprabhu.annotation.Process * *(..)) "
          + "&amp;&amp; @annotation(process)")
  public void processPointcut(Process process) {
  }
  
  @Around("processPointcut(process)")
  public Object process(final ProceedingJoinPoint joinPoint, final Process process)
          throws Throwable {
      final Object callingObject = joinPoint.getThis();
      final Method method = ((MethodSignature)joinPoint.getSignature()).getMethod();
      final MethodContext methodContext = new MethodContext(callingObject, method);
  
      //The below mentioned delegate will be picked
      //depending on some validation performed on 'callingObject'
      Object retValue = delegate.process(methodContext, joinPoint.getArgs());
      return retValue;
  }
  </code></pre>
  
  <p>And a delegate looks like this:</p>
  
  <pre><code>class SimpleDelegate extends Delegate {
      @Override
      public Object process(final MethodContext methodContext, final Object[] args) {
          // The problem is that following invoke call calls
          // the @Around aspect to run again, which calls the delegate again,
          // which causes execution of the next line, which causes the aspect to run
          // again and so on...
          methodContext.getMethod().invoke(
              methodContext.getCallingObject(),
              args
          );
      }
  }
  </code></pre>
  
  <p>It would be great if I could do something like (I really don't think there is something like <code>calledByReflection</code>:</p>
  
  <pre><code>@Pointcut("execution(@com.rohanprabhu.annotation.Process * *(..)) "
          + "&amp;&amp; @annotation(process)" + " &amp;&amp; !calledByReflection")
  public void processPointcut(Process process) {
  }
  </code></pre>
  
  <p>Or something like:</p>
  
  <pre><code>@Around("processPointcut(process)")
  public Object process(final ProceedingJoinPoint joinPoint, final Process process,
                 final boolean processAnnotation)
          throws Throwable {
      if(processAnnotation) {
          // Do the regular stuff
      }
  }
  </code></pre>
  
  <p>and there was some way for me to supply the value to <code>processAnnotation</code> to the Aspect. I hope this clarified my question.
      }</p>