title: (De)referencing method variant in Java Nashorn
body: <p>Consider the code:</p>
  
  <p><strong>Example 1</strong></p>
  
  <pre><code>var Executors = java.util.concurrent.Executors;
  var executor = Executors.newCachedThreadPool();
  var fork = function (callable) {
      // Clarify Runnable versus Callable overloaded methods
      executor['submit(java.util.concurrent.Callable)'](callable);
  };
  
  fork(function(){ ... }); //ok
  </code></pre>
  
  <p>This works.</p>
  
  <p>But this does not work:</p>
  
  <p><strong>Example 2</strong></p>
  
  <pre><code>var Executors = java.util.concurrent.Executors;
  var executor = Executors.newCachedThreadPool();
  var fork = executor['submit(java.util.concurrent.Callable)'];
  
  fork(function(){ ... }); //fails, NullPointerException
  </code></pre>
  
  <p>I assume, it is because <code>fork</code> here is not a JS Function instance, it is actually an instance of <code>jdk.internal.dynalink.beans.SimpleDynamicMethod</code></p>
  
  <p>I tried to use <code>fork.apply(executor,function() { ... });</code> but natrually, SimpleDynamicMethod has no <code>apply</code>.</p>
  
  <p>Why is it, actually, that Example 2 does not work, while Example 1 does?</p>
  
  <p>Is it simply a perk of Nashorn? It there a better way to define fork() function than in Example 1?</p>
  
  <p><strong>Update</strong></p>
  
  <p>In example 2, </p>
  
  <p><code>print(typeof fork);</code> reports <code>function</code></p>
  
  <p><code>print(fork)</code> reports <code>[jdk.internal.dynalink.beans.SimpleDynamicMethod Future java.util.concurrent.AbstractExecutorService.submit(Callable)]</code></p>
  
  <p>and exception is (with line 13 reading <code>fork(function() {</code>)</p>
  
  <pre><code>Exception in thread "main" java.lang.NullPointerException
      at jdk.nashorn.internal.scripts.Script$\^eval\_._L5(&lt;eval&gt;:13)
      at jdk.nashorn.internal.scripts.Script$\^eval\_.runScript(&lt;eval&gt;:5)
      at jdk.nashorn.internal.runtime.ScriptFunctionData.invoke(ScriptFunctionData.java:527)
  </code></pre>