title: How to use javax.tools.JavaCompiler to create a function that calls a list of methods of child class filtered by custom annotation?
body: <p>I have this unusual (I think?) situation</p>
  
  <pre><code>public abstract class Base {
  
     public Base() {
         // code
     }
  
     public final void executeAll() {
        final Foo foo = new Foo();
        final Bar bar = new Bar();
        // now execute all method of "this" that: 
        //   - have annotation mark
        //   - have return type void
        //   - accept (foo, bar) has parameters
        //   - name can be whatever you want
        //   - protected
     }
  
     @Retention(RetentionPolicy.RUNTIME)
     @Target({ElementType.METHOD})
     protected @interface Mark{
     }
  }
  </code></pre>
  
  <p>Now I have a class that extends Base and has some methods with the signature as explained in the executeAll method, like this:</p>
  
  <pre><code>public class Test extends Base {
  
     @Mark
     protected void willBeCalled(Foo a, Bar b) {
     }
  
     @Mark
     protected void alsoThisOne(Foo a, Bar b) {
     }
  
     @Mark
     protected void yep(Foo a, Bar b) {
     }
  
  }
  </code></pre>
  
  <p>I know that this can be easily achieved my creating an "protected abstract <code>Collection&lt;BiConsumer&lt;Foo, Bar&gt;&gt; getToBeCalled()</code>" make every child to extend it and write the executeAll() method as:</p>
  
  <pre><code>   protected abstract Collection&lt;BiConsumer&lt;Foo, Bar&gt;&gt; getToBeCalled();
  
     public final void executeAll() {
        final Foo foo = new Foo();
        final Bar bar = new Bar();
        getToBeCalled().stream.forEach(bic-&gt; bic.accept(foo, bar));
     }
  </code></pre>
  
  <p>But this is <strong>not</strong> what I want to achieve. I need a way to get the methods to execute from the child class without any major intervention from the developer (that why I thought about an annotation to mark the method desired to be executed and nothing more).</p>
  
  <p>Right now I have a working implementation that inside Base empty constructor scan for every available methods with <code>this.getClass().getMethods()</code>, filter them (must have Mark annotation, accept only a Foo parameter and a Bar parameter in that order and return void, be protected) and save them in a <code>private List&lt;Method&gt; methods;</code> later used by executeAll to call each method.</p>
  
  <p>Recently I also read this: <a href="https://www.optaplanner.org/blog/2018/01/09/JavaReflectionButMuchFaster.html" rel="nofollow noreferrer">https://www.optaplanner.org/blog/2018/01/09/JavaReflectionButMuchFaster.html</a> ...</p>
  
  <p>... and just-for-fun implemented a MethodHandle version (I think that maybe in my use case will be faster), and planning to make a LambdaMetafactory version... but I'm really intrigued by the javax.tools.JavaCompiler solution. I'm still trying to figuring out how to do it tho... I keep my original method name search and filtering, I keep only the names... and now? <strong>How can I convert a list of method names</strong> (which I know I have access to, since I'm calling from "this" and are declared protected in the child class, and also have a specific signature) <strong>to something usable, like a single <code>private final BiConsumer&lt;Foo, Bar&gt; callAll;</code></strong> computed and saved by the Base() constructor and called by the executeAll function like this:</p>
  
  <pre><code>public abstract class Base {
  
     private final BiConsumer&lt;Foo, Bar&gt; callAll;
  
     protected Base() {
         // get all methods &lt;&lt;&lt; already done
         Method[] methods = ;
         // filter them by annotation, visibility and signature &lt;&lt;&lt; already done
         List&lt;Method&gt; filtered&gt; = ;
         // save only the names &lt;&lt;&lt; already done
         List&lt;String&gt; names = ;
         // use javax.tools.JavaCompiler somehow &lt;&lt;&lt; I miss this
         callAll = ???; 
     }
  
     public final void executeAll() {
        final Foo foo = new Foo();
        final Bar bar = new Bar();
        callAll.accept(foo, bar);
     }
  
     @Retention(RetentionPolicy.RUNTIME)
     @Target({ElementType.METHOD})
     protected @interface Mark{
     }
  }
  </code></pre>
  
  <p>Of course the idea to use a BiConsumer as callAll is just an idea if there is something better I'm perfectly fine with suggestion.</p>