title: Calling @JSFunction from JavaScript, TypeError: Cannot find default value for object
body: <p>I'm calling a <code>@JSFunction</code> annotated method of a <code>ScriptableObject</code></p>
  
  <p>The JavaScript file</p>
  
  <pre><code>Target = Packages.com.acme.rhino.Target;
  
  function evaluate() {
      var t = Target();
      t.addModifier("foobar", 1);
      return t;
  }
  </code></pre>
  
  <p>The Java File</p>
  
  <pre><code>public class Target extends ScriptableObject {
      private static final long serialVersionUID = 1L;
      public List&lt;Modifier&gt; modifiers = new LinkedList&lt;&gt;();
  
      @JSConstructor
      public Target() {
      }
  
      @JSFunction
      public void addModifier(final String message, final int value) {
          modifiers.add(new Modifier(message, value));
      }
  
      public int getValue() {
          int sum = 0;
          for (final Modifier modifier : modifiers) {
              sum += modifier.getValue();
          }
          return sum;
      }
  
      @Override
      public String getClassName() {
          return "Target";
      }
  }
  </code></pre>
  
  <p>But I get</p>
  
  <pre><code>org.mozilla.javascript.EcmaError: TypeError: Cannot find default value for object.
      at org.mozilla.javascript.ScriptRuntime.constructError(ScriptRuntime.java:3687)
      at org.mozilla.javascript.ScriptRuntime.constructError(ScriptRuntime.java:3665)
      at org.mozilla.javascript.ScriptRuntime.typeError(ScriptRuntime.java:3693)
      at org.mozilla.javascript.ScriptRuntime.typeError1(ScriptRuntime.java:3705)
      at org.mozilla.javascript.ScriptableObject.getDefaultValue(ScriptableObject.java:976    )
      at org.mozilla.javascript.ScriptableObject.getDefaultValue(ScriptableObject.java:895    )
      at org.mozilla.javascript.ScriptRuntime.toString(ScriptRuntime.java:761)
      at org.mozilla.javascript.ScriptRuntime.notFunctionError(ScriptRuntime.java:3774)
      at org.mozilla.javascript.ScriptRuntime.getPropFunctionAndThisHelper(ScriptRuntime.    java:2269)
      at org.mozilla.javascript.ScriptRuntime.getPropFunctionAndThis(ScriptRuntime.    java:2251)
      at org.mozilla.javascript.optimizer.OptRuntime.callProp0(OptRuntime.java:83)
      at org.mozilla.javascript.gen.script_5._c_evaluate_1(script:6)
      at org.mozilla.javascript.gen.script_5.call(script)
      at org.mozilla.javascript.ContextFactory.doTopCall(ContextFactory.java:394)
      at org.mozilla.javascript.ScriptRuntime.doTopCall(ScriptRuntime.java:3091)
      at org.mozilla.javascript.gen.script_5.call(script)
  </code></pre>
  
  <p>and don't know where to go from there. When I don't call <code>addModifier</code> method the given code works, and given the error <code>notFunctionError</code> down in the stack trace I think that Rhino doesn't interpret the given method as a JavaScript Function.</p>
  
  <ul>
  <li>OSX 10.8.2</li>
  <li>Java 7</li>
  <li>Rhino 1.7R4</li>
  </ul>
  
  <p>Complete Maven project that reproduces the error can be found <a href="https://github.com/oschrenk/stackoverflow-q14561874">here</a></p>