title: AspectJ AOP LTW not working with dynamic loading of javaagent
body: <p><a href="https://github.com/pankajsinghal/aop-example" rel="nofollow noreferrer">Here is my sample non-working project</a>.</p>
  
  <p>It contains 2 modules: </p>
  
  <ul>
  <li><strong>aop-lib</strong> - Aspects used as lib. It contains the following classes
  
  <ol>
  <li><strong>Wrap.java</strong> - It's the annotation used to attach advice</li>
  <li><strong>WrapDef.java</strong> - It is the definition of the above mentioned <code>Wrap</code> annotation.</li>
  </ol></li>
  <li><strong>aop-app</strong> - Uses the above aspect lib 
  
  <ol>
  <li><strong>DynamicLoad.java</strong> - class to load javaagent dynamically</li>
  <li><strong>Main.java</strong> - Main class which uses <code>Wrap</code> annotation.</li>
  </ol></li>
  </ul>
  
  <p>Dir structure is as follows:</p>
  
  <pre><code>.
  ├── README.md
  ├── aop-app
  │   ├── pom.xml
  │   └── src
  │       └── main
  │           └── java
  │               └── com
  │                   └── aop
  │                       └── app
  │                           ├── DynamicLoad.java
  │                           └── Main.java
  └── aop-lib
      ├── pom.xml
      └── src
          └── main
              └── java
                  └── com
                      └── aop
                          └── app
                              └── lib
                                  ├── Wrap.java
                                  └── WrapDef.java
  </code></pre>
  
  <p>I'm trying to use the aspect lib <code>aop-lib</code> (an AOP advice lib) inside <code>aop-app</code> through Load Time Weaving (LTW) by dynamically loading the javaagent as mentioned in the <a href="https://www.eclipse.org/aspectj/doc/released/README-187.html" rel="nofollow noreferrer">official docs</a>. But it's not working. </p>
  
  <p>Following are the contents of <a href="https://github.com/pankajsinghal/aop-ltw/blob/master/aop-lib/src/main/java/com/aop/app/lib/Wrap.java" rel="nofollow noreferrer">Wrap.java</a></p>
  
  <pre><code>@Target({ElementType.METHOD, ElementType.TYPE})
  @Retention(value = RetentionPolicy.RUNTIME)
  public @interface Wrap { }
  </code></pre>
  
  <p>Following are the contents of <a href="https://github.com/pankajsinghal/aop-ltw/blob/master/aop-lib/src/main/java/com/aop/app/lib/WrapDef.java" rel="nofollow noreferrer">WrapDef.java</a></p>
  
  <pre><code>@Aspect
  public class WrapDef {
      private static final Logger logger = LoggerFactory.getLogger(WrapDef.class);
  
      public static boolean loaded = false;
  
      @Around("@annotation( wrapAnnotation ) &amp;&amp; execution(* *(..))")
      public Object processSystemRequest(final ProceedingJoinPoint pjp, Wrap wrapAnnotation)
              throws Throwable {
          logger.debug("before wrap");
          Object o = pjp.proceed();
          logger.debug("after wrap");
          return o;
      }
  
      static {
          System.out.println("Loading");
          WrapDef.loaded = true;
      }
      public static void reportLoaded() {
          System.out.println("loaded : " + loaded);
      }
  }
  </code></pre>
  
  <p>Following are the contents of <a href="https://github.com/pankajsinghal/aop-ltw/blob/master/aop-app/src/main/java/com/aop/app/Main.java" rel="nofollow noreferrer">Main.java</a>:</p>
  
  <pre><code>public class Main {
  
      private static final Logger logger = LoggerFactory.getLogger(Main.class);
  
      @Wrap
      public void myFunc(){
          logger.debug("inside myFunc");
      }
  
      public static void main(String[] args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
  
          boolean dynamicLoad = Boolean.getBoolean("dynamicLoad");
          if(dynamicLoad){
              Main.isAdviceClassLoaded();           //To see if WrapDef.java is loaded or not.
              if(!DynamicLoad.isAspectJAgentLoaded()) {
                  logger.error("AspectJ Not Loaded. Existing.");
                  System.exit(0);
              }
              Main.isAdviceClassLoaded();           //To see if WrapDef.java is loaded or not.
          }
  
          new Main().myFunc();
      }
  
      private static void isAdviceClassLoaded() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
          java.lang.reflect.Method m = ClassLoader.class.getDeclaredMethod("findLoadedClass", String.class);
          m.setAccessible(true);
          ClassLoader cl = ClassLoader.getSystemClassLoader();
          Object test1 = m.invoke(cl, "com.aop.app.lib.WrapDef");
          boolean loaded = test1 != null;
          System.out.println("com.aop.app.lib.WrapDef Loaded : " + loaded);
      }
  
  }
  </code></pre>
  
  <p>With <code>javaagent</code> as cmd line arg, it works perfectly fine:</p>
  
  <pre><code>$ java -javaagent:deploy/lib/aspectjweaver-1.9.1.jar -classpath aop-app-1.0.jar:deploy/lib/* com.aop.app.Main
  14:02:45.384 [main] DEBUG com.aop.app.lib.WrapDef - before wrap
  14:02:45.391 [main] DEBUG com.aop.app.Main - inside myFunc
  14:02:45.391 [main] DEBUG com.aop.app.lib.WrapDef - after wrap
  </code></pre>
  
  <p>But, with Dynamic loading of <code>javaagent</code>, it gives the following output:</p>
  
  <pre><code>$ java -DdynamicLoad=true -DAGENT_PATH=deploy/lib/aspectjweaver-1.9.1.jar -classpath aop-app-1.0.jar:deploy/lib/* com.aop.app.Main
  com.aop.app.lib.WrapDef Loaded : false                   //The WrapDef is NOT loaded before JAVAAGENT is Loaded - which is correct
  java.lang.UnsupportedOperationException: AspectJ weaving agent was neither started via '-javaagent' (preMain) nor attached via 'VirtualMachine.loadAgent' (agentMain)
  loading javaAgent deploy/lib/aspectjweaver-1.9.1.jar
  loaded javaAgent deploy/lib/aspectjweaver-1.9.1.jar      //The JAVAAGENT is Dynamically Loaded - which is correct
  com.aop.app.lib.WrapDef Loaded : false                   //The WrapDef is STILL NOT loaded even AFTER JAVAAGENT is Loaded - THIS IS THE ISSUE
  15:53:08.543 [main] DEBUG com.aop.app.Main - inside myFunc
  </code></pre>
  
  <p>The <a href="https://www.eclipse.org/aspectj/doc/released/README-187.html" rel="nofollow noreferrer">official docs</a> does say that <code>any classes loaded before attachment will not be woven</code>. But, on the contrary as you can see in the above output, the <code>WrapDef</code> class is not loaded at all.</p>
  
  <p>Also, note that I'm using <code>aspectj-maven-plugin</code> in my <a href="https://github.com/pankajsinghal/aop-ltw/blob/master/aop-lib/pom.xml" rel="nofollow noreferrer">aop-lib/pom.xml</a>, with the following options:</p>
  
  <pre><code>&lt;outxml&gt;true&lt;/outxml&gt;                           //creates META-INF/aop-ajc.xml
  &lt;showWeaveInfo&gt;true&lt;/showWeaveInfo&gt;             //supposed to create &lt;weaver options="-showWeaveInfo"/&gt; BUT DOES NOT WORK
  &lt;verbose&gt;true&lt;/verbose&gt;                         //supposed to create &lt;weaver options="-verbose"/&gt; BUT DOES NOT WORK
  </code></pre>
  
  <p>So, it creates <code>META-INF/aop-ajc.xml</code> inside <code>aop-lib-1.0.jar</code> with the following content:</p>
  
  <pre><code>&lt;aspectj&gt;
  &lt;aspects&gt;
  &lt;aspect name="com.aop.app.lib.WrapDef"/&gt;
  &lt;/aspects&gt;
  &lt;/aspectj&gt;
  </code></pre>
  
  <p>but the other tags corresponding to <code>showWeaveInfo</code> &amp; <code>verbose</code> are not created in <code>META-INF/aop-ajc.xml</code>. This is another thing that is not working here.</p>
  
  <p>If you require any other info - I shall provide it.</p>
  
  <p>Any help is appreciated.</p>