title: Is it possible to intercept array constructor with ByteBuddy?
body: <p>I have a regression test where I'm trying to count the number of instantiations of arrays. Specifically, I'm interested in counting how many times <code>new int[..]</code> is called. I have put together the following ByteBuddy advice-based instrumentation, which I believe should catch <em>all</em> constructors, but this is not working for arrays (of any kind):</p>
  <pre class="lang-java prettyprint-override"><code>public class InstanceCounter {
    public static void load() {
      ClassInjector.UsingUnsafe.ofBootLoader().injectRaw(Collections.singletonMap(&quot;foo.bar.BootstrapState&quot;, readBytes(InstanceCounter.class.getClassLoader().getResource(&quot;foo/bar/BootstrapState.class&quot;))));
      new AgentBuilder.Default()
        .ignore(none())
        .disableClassFormatChanges()
        .with(RedefinitionStrategy.RETRANSFORMATION)
        .with(InitializationStrategy.NoOp.INSTANCE)
        .with(TypeStrategy.Default.REDEFINE)
      .type(any())
        .transform(new Transformer() {
          @Override
          public Builder&lt;?&gt; transform(final Builder&lt;?&gt; builder, final TypeDescription typeDescription, final ClassLoader classLoader, final JavaModule module) {
            return builder.visit(Advice.to(InstanceCounter.class).on(any()));
          }})
      .installOn(ByteBuddyAgent.install());
    }
  
    @Advice.OnMethodEnter
    public static void enter(final @Advice.Origin Class&lt;?&gt; origin) {
      if (!BootstrapState.trace || BootstrapState.lock)
        return;
  
      BootstrapState.lock = true;
      System.out.println(&quot;&gt;&gt;&gt; &quot; + origin);
      BootstrapState.lock = false;
    }
  }
  
  public class InstanceCounterTest {
    @Test
    public void test() {
      InstanceCounter.load();
      System.out.println(&quot;Creating array...&quot;);
      BootstrapState.trace = true;
      final Object[] y = new Object[3];
      BootstrapState.trace = false;
      System.out.println(&quot;Printing array: &quot; + y);
    }
  }
  </code></pre>
  <p>When I run this test, I get the following results:</p>
  <pre><code>Creating array...
  Printing array: [Ljava.lang.Object;@7f5eae0f
  </code></pre>
  <p>If I change <code>final Object[] y = new Object[3]</code> to <code>new Object()</code>, then the output is:</p>
  <pre><code>Creating array...
  &gt;&gt;&gt; class java.lang.Object
  Printing array: [Ljava.lang.Object;@68562ad5
  </code></pre>
  <p>I've made the advice instrumentation as open as possible (at least as open as I think I can make it), but this is still not instrumenting construction of array objects.</p>
  <p>Is is even possible to instrument construction of array objects?</p>
  <p><strong>Update 2020-07-28</strong></p>
  <blockquote>
  <p>As per the context of the original question, the provided answers express that ByteBuddy does not have direct support for automatic instrumentation of <code>new int[]</code>. However, for those interested in an alternate solution, I was able to achieve this kind of instrumentation with <a href="https://downloads.jboss.org/byteman/latest/byteman-programmers-guide.html#at-new-after-new" rel="nofollow noreferrer">Byteman</a>.</p>
  </blockquote>