Inconsistent missing property handling with java objects
mike.rettig at gmail.com
Wed Sep 3 15:29:45 UTC 2014
There are a couple reasons why it is problematic for me. The java objects
are not dynamic objects. The interface is static. Allowing silent failures
leads to all sorts of time consuming and frustrating bugs. Nashorn doesn't
wrap the java objects, so it should treat them like java objects.
Referencing an unknown property on a java object should fail (as it did in
Rhino). The current behavior is the worst of both worlds. The java object
is not wrapped so there are no extension points (e.g. __noSuchProperty___),
but in the case of a missing property it behaves like a js object by
Anyone know the reasoning behind this design decision?
On Wed, Sep 3, 2014 at 10:00 AM, Hannes Wallnoefer <
hannes.wallnoefer at oracle.com> wrote:
> Hi Mike,
> It was just a design decision to handle missing Java properties this way
> in Nashorn.
> The behaviour is implemented in NashornBottomLinker.linkBean method.
> Unfortunately there's currently no way to configure Nashorn to behave the
> way you want it to.
> Am 2014-09-03 um 16:13 schrieb Mike Rettig:
> With nashorn, it seems that the assignment and accessing of missing
>> properties on java objects is handled as 'undefined'.
>> String engine = "nashorn";
>> ScriptEngineManager engineManager =
>> new ScriptEngineManager();
>> final ScriptEngine nashorn = engineManager.getEngineByName(
>> nashorn.eval("var d=new java.util.Date();");
>> This will print 'undefined'. It doesn't fail on the 'badProp' on the Date
>> object. With Rhino, this would fail on the assignment of the bad property
>> with the following error:
>> "Exception in thread "main" javax.script.ScriptException:
>> "java.util.Date" has no public instance field or method named "badProp".
>> (<Unknown source>#1) in <Unknown source> at line number 1"
>> I want the script to fail when the bad property is referenced. I've tried
>> 'use strict', the rhino compatibility script, and attempting to define
>> on the java object. I can create a wrapper object that simply delegates to
>> the java object and throws if a property is undefined, but that seems
>> Is this the correct behavior? Is there a way to work around this behavior
>> in nashorn?
More information about the nashorn-dev