title: How to get the name of the method resulting from a lambda
body: <p>From a simple point of view, lambda expressions are compiled into <code>static</code> methods, since they don’t capture <code>this</code> (i.e. do not access instance members). </p>
  
  <pre><code>public class App {
    public static void foo(){
        Consumer&lt;Integer&gt; c1 = n -&gt; {};
        Consumer&lt;Integer&gt; c2 = n -&gt; {};
    }
  
    public static void main(String [] args) {
      Supplier&lt;String&gt; sup1 = () -&gt; "I am sup1";
      Supplier&lt;String&gt; sup2 = () -&gt; "I am sup2";
      Supplier&lt;String&gt; sup3 = () -&gt; "I am sup3";
  
      Stream.of(App.class.getDeclaredMethods()).forEach(System.out::println);
    }
  }
  </code></pre>
  
  <p>Running the previous example and checking the generated bytecodes with the <code>javap</code> tool, we can see that lambdas referred by variables <code>c1</code>, <code>c2</code>, <code>sup1</code>, <code>sup2</code>, <code>sup3</code> are compiled to methods with names: </p>
  
  <ul>
  <li><code>lambda$foo$0</code></li>
  <li><code>lambda$foo$1</code></li>
  <li><code>lambda$main$2</code></li>
  <li><code>lambda$main$3</code></li>
  <li><code>lambda$main$4</code></li>
  </ul>
  
  <p>However, if we print the <code>getClass()</code> for each object referred by those variables (<code>c1</code>, <code>c2</code>, <code>sup1</code>, <code>sup2</code>, <code>sup3</code>) we will get, respectively: </p>
  
  <ul>
  <li><code>class App$$Lambda$5/1705736037</code></li>
  <li><code>class App$$Lambda$6/455659002</code></li>
  <li><code>class App$$Lambda$1/791452441</code></li>
  <li><code>class App$$Lambda$2/531885035</code></li>
  <li><code>class App$$Lambda$3/1418481495</code></li>
  </ul>
  
  <p>So, how can we make the correspondence between the runtime class names and the name of the methods resulting from the lambdas? </p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>None of the solutions pointed in the following duplicated questions helps me to solve the problem that I am asking in my question:</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/q/19845213/1140754">How to get the MethodInfo of a Java 8 method reference?</a></li>
  <li><a href="https://stackoverflow.com/q/21860875/1140754">Printing debug info on errors with java 8 lambda expressions 1 answer</a></li>
  <li><a href="https://stackoverflow.com/q/44074809/1140754">Get MethodHandle from lambda object</a></li>
  </ul>
  
  <p>The only way that I found was to adapt the solution of @Holger from its answer (not the accepted one) to the question <a href="https://stackoverflow.com/q/34925524/1140754">Java 8: convert lambda to a Method instance with clousure included</a>. However, in my case, instead of a <code>Method</code> object I am just looking for the name of the method. So, adapting that <a href="https://stackoverflow.com/a/34929760/1140754"><code>lambdaToMethod()</code></a> to the following <code>methodNameFromLambda()</code> I got a solution:</p>
  
  <pre><code>static String methodNameFromLambda(Serializable lambda) {
    try {
      Method m = lambda.getClass().getDeclaredMethod("writeReplace");
      m.setAccessible(true);
      SerializedLambda sl=(SerializedLambda)m.invoke(lambda);
      return sl.getImplMethodName();
    } catch(ReflectiveOperationException ex) {
      throw new RuntimeException(ex);
    }
  }
  </code></pre>
  
  <p>Now, lambdas of the example must be cast to <code>Consumer&lt;Integer&gt;&amp;Serializable</code> or <code>Supplier&lt;String&gt;&amp;Serializable</code> as describe in @Holger <a href="https://stackoverflow.com/a/34929760/1140754">answer</a>.</p>
  
  <p>I am not aware if this solution is correct, but it works for my example and for the cases that I am working on.</p>