title: Instrument Java 17 with AspectJ
body: <p>When I try to run an AspectJ instrumentation with Java 17, I always get errors like the following:</p>
  <pre><code>java.lang.reflect.InvocationTargetException
          at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
          at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
          at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
          at java.base/java.lang.reflect.Method.invoke(Method.java:568)
          at org.aspectj.weaver.loadtime.ClassLoaderWeavingAdaptor.initializeForJava11(ClassLoaderWeavingAdaptor.java:1069)
          at org.aspectj.weaver.loadtime.ClassLoaderWeavingAdaptor.defineClass(ClassLoaderWeavingAdaptor.java:1096)
          at org.aspectj.weaver.loadtime.ClassLoaderWeavingAdaptor.access$300(ClassLoaderWeavingAdaptor.java:66)
          at org.aspectj.weaver.loadtime.ClassLoaderWeavingAdaptor$SimpleGeneratedClassHandler.acceptClass(ClassLoaderWeavingAdaptor.java:150)
          at org.aspectj.weaver.tools.WeavingAdaptor$WeavingClassFileProvider$1.acceptResult(WeavingAdaptor.java:917)
          at org.aspectj.weaver.bcel.BcelWeaver.weaveAndNotify(BcelWeaver.java:1431)
          at org.aspectj.weaver.bcel.BcelWeaver.weave(BcelWeaver.java:1192)
          at org.aspectj.weaver.tools.WeavingAdaptor.getWovenBytes(WeavingAdaptor.java:549)
          at org.aspectj.weaver.tools.WeavingAdaptor.weaveClass(WeavingAdaptor.java:385)
          at org.aspectj.weaver.loadtime.Aj.preProcess(Aj.java:115)
          at org.aspectj.weaver.loadtime.ClassPreProcessorAgentAdapter.transform(ClassPreProcessorAgentAdapter.java:51)
          at java.instrument/java.lang.instrument.ClassFileTransformer.transform(ClassFileTransformer.java:244)
          at java.instrument/sun.instrument.TransformerManager.transform(TransformerManager.java:188)
          at java.instrument/sun.instrument.InstrumentationImpl.transform(InstrumentationImpl.java:541)
          at java.base/java.lang.ClassLoader.defineClass1(Native Method)
          at java.base/java.lang.ClassLoader.defineClass(ClassLoader.java:1012)
          at java.base/java.security.SecureClassLoader.defineClass(SecureClassLoader.java:150)
          at java.base/jdk.internal.loader.BuiltinClassLoader.defineClass(BuiltinClassLoader.java:862)
          at java.base/jdk.internal.loader.BuiltinClassLoader.findClassOnClassPathOrNull(BuiltinClassLoader.java:760)
          at java.base/jdk.internal.loader.BuiltinClassLoader.loadClassOrNull(BuiltinClassLoader.java:681)
          at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:639)
          at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
          at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:520)
          at java.base/java.lang.Class.forName0(Native Method)
          at java.base/java.lang.Class.forName(Class.java:467)
          at java.base/sun.launcher.LauncherHelper.loadMainClass(LauncherHelper.java:780)
          at java.base/sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:675)
  Caused by: java.lang.IllegalAccessException: module java.base does not open java.lang to unnamed module @6acbcfc0
          at java.base/java.lang.invoke.MethodHandles.privateLookupIn(MethodHandles.java:259)
          ... 31 more
  </code></pre>
  <p>You can reproduce this by running the example from <a href="https://github.com/DaGeRe/aspect-final-example" rel="nofollow noreferrer">https://github.com/DaGeRe/aspect-final-example</a> (<code>runMainWithoutError.sh</code>, since the MWE was originally to demonstrate a Java 11 problem). If you run this with Java 11, everything runs fine, if you run this with Java 17 you get the above error.</p>
  <p>Is there a known workaround for this, or any way to use AspectJ with Java 17?</p>
  <p><strong>EDIT</strong> The exact failing versions where:</p>
  <pre><code>reichelt@reichelt-desktop:~/nvme/workspaces/dissworkspace/aspect-final-example/woven-project$ java -version
  openjdk version &quot;17.0.1&quot; 2021-10-19
  OpenJDK Runtime Environment (build 17.0.1+12-Ubuntu-120.04)
  OpenJDK 64-Bit Server VM (build 17.0.1+12-Ubuntu-120.04, mixed mode, sharing)
  reichelt@reichelt-desktop:~/nvme/workspaces/dissworkspace/aspect-final-example/woven-project$ javac -version
  javac 17.0.1
  </code></pre>
  <p>The same happens with GraalVM CE:</p>
  <pre><code>reichelt@reichelt-desktop:~/nvme/workspaces/dissworkspace/aspect-final-example/woven-project$ java -version
  openjdk version &quot;17.0.1&quot; 2021-10-19
  OpenJDK Runtime Environment GraalVM CE 21.3.0 (build 17.0.1+12-jvmci-21.3-b05)
  OpenJDK 64-Bit Server VM GraalVM CE 21.3.0 (build 17.0.1+12-jvmci-21.3-b05, mixed mode, sharing)
  </code></pre>
  <p>Due to the error message, I would assume that this somehow is caused by AspectJ not (correctly?) using the modules.</p>