title: Reflection type inference on Java 8 Lambdas
body: <p>I was experimenting with the new Lambdas in Java 8, and I am looking for a way to use reflection on the lambda classes to get the return type of a lambda function. I am especially interested in cases where the lambda implements a generic superinterface. In the code example below, <code>MapFunction&lt;F, T&gt;</code> is the generic superinterface, and I am looking for a way to find out what type binds to the generic parameter <code>T</code>.</p>
  
  <p>While Java throws away a lot of generic type information after the compiler, subclasses (and anonymous subclasses) of generic superclasses and generic superinterfaces did preserve that type information. Via reflection, these types were accessible. In the example below <em>(case 1)</em>, reflection tells my that the <code>MyMapper</code> implementation of <code>MapFunction</code> binds <code>java.lang.Integer</code> to the generic type parameter <code>T</code>.</p>
  
  <p>Even for subclasses that are themselves generic, there are certain means to find out what binds to a generic parameter, if some others are known. Consider <em>case 2</em> in the example below, the <code>IdentityMapper</code> where both <code>F</code> and <code>T</code> bind to the same type. When we know that, we know the type <code>F</code> if we know the parameter type <code>T</code> (which in my case we do).</p>
  
  <p>The question is now, how can I realize something similar for the Java 8 lambdas? Since they are actually not regular subclasses of the generic superinterface, the above described method does not work.
  Specifically, can I figure out that the <code>parseLambda</code> binds <code>java.lang.Integer</code> to <code>T</code>, and the <code>identityLambda</code> binds the same to <code>F</code> and <code>T</code>?</p>
  
  <p>PS: In theory it should possible to decompile the lambda code and then use an embedded compiler (like the JDT) and tap into its type inference. I hope that there is a simpler way to do this ;-)</p>
  
  <pre class="lang-java prettyprint-override"><code>/**
   * The superinterface.
   */
  public interface MapFunction&lt;F, T&gt; {
  
      T map(F value);
  }
  
  /**
   * Case 1: A non-generic subclass.
   */
  public class MyMapper implements MapFunction&lt;String, Integer&gt; {
  
      public Integer map(String value) {
          return Integer.valueOf(value);
      }
  }
  
  /**
   * A generic subclass
   */
  public class IdentityMapper&lt;E&gt; implements MapFunction&lt;E, E&gt; {
  
      public E map(E value) {
          return value;
      }
  
  }
  
  /**
   * Instantiation through lambda
   */
  
  public MapFunction&lt;String, Integer&gt; parseLambda = (String str) -&gt; { return Integer.valueOf(str); }
  
  public MapFunction&lt;E, E&gt; identityLambda = (value) -&gt; { return value; }
  
  
  public static void main(String[] args)
  {
      // case 1
      getReturnType(MyMapper.class);    // -&gt; returns java.lang.Integer
  
      // case 2
      getReturnTypeRelativeToParameter(IdentityMapper.class, String.class);    // -&gt; returns java.lang.String
  }
  
  private static Class&lt;?&gt; getReturnType(Class&lt;?&gt; implementingClass)
  {
      Type superType = implementingClass.getGenericInterfaces()[0];
  
      if (superType instanceof ParameterizedType) {
          ParameterizedType parameterizedType = (ParameterizedType) superType;
          return (Class&lt;?&gt;) parameterizedType.getActualTypeArguments()[1];
      }
      else return null;
  }
  
  private static Class&lt;?&gt; getReturnTypeRelativeToParameter(Class&lt;?&gt; implementingClass, Class&lt;?&gt; parameterType)
  {
      Type superType = implementingClass.getGenericInterfaces()[0];
  
      if (superType instanceof ParameterizedType) {
          ParameterizedType parameterizedType = (ParameterizedType) superType;
          TypeVariable&lt;?&gt; inputType = (TypeVariable&lt;?&gt;) parameterizedType.getActualTypeArguments()[0];
          TypeVariable&lt;?&gt; returnType = (TypeVariable&lt;?&gt;) parameterizedType.getActualTypeArguments()[1];
  
          if (inputType.getName().equals(returnType.getName())) {
              return parameterType;
          }
          else {
              // some logic that figures out composed return types
          }
      }
  
      return null;
  }
  </code></pre>