title: Java: Inject code before and after method call
body: <p>I'm risking this question knowing quite a few other questions <a href="https://www.google.com/search?q=java%20annotations%20to%20inject%20code%20site:stackoverflow.com" rel="nofollow noreferrer">have been asked</a> that somehow touch the same issue. But they are either quite old, or very specific, or hard to follow, and the answers are also very specific and seem not applicable to my use case.</p>
  
  <p>What I want to achieve is quite simple to describe, it is about injecting code before and after a method call in Java. Here is a simple example. I want something like this line of code:</p>
  
  <pre class="lang-java prettyprint-override"><code>method(p1, @ANNOTATION(type=String) p2, p3);
  </code></pre>
  
  <p>to desugar to</p>
  
  <pre><code>Wrapper&lt;String&gt; w = new Wrapper&lt;&gt;(p2);
  method(p1, w, p3);
  p2 = w.get();
  </code></pre>
  
  <p>in the context of a larger block of statements, with a pre-defined <code>Wrapper</code> class. I have limited control over the build environment that is used to compile the code, so any solution should be realizable with Gradle plugins/dependencies.</p>
  
  <p>What I've looked at so far (without having a deep understanding of any of them):</p>
  
  <ul>
  <li><em>Java Annotations</em> : are never seriously considered in answers to similar questions, so I guess they are incapable of achieving that</li>
  <li><em>Aspect frameworks</em> like AspectJ or Spring Aspects: are mentioned most often, but they only seem to be able to wrap entire methods, not specific invocations of them</li>
  <li><em>Bytcode manipulation frameworks</em> like asm or cglib: seem generally capable of doing this, but are quite heavy-weight and require some plumbing, esp. on the build side, to achieve it</li>
  </ul>
  
  <p>So, what could be a solution for this?</p>