Unable to iterate over Enums...

A. Sundararajan sundararajan.athijegannathan at oracle.com
Sun Jan 26 20:45:08 PST 2014

Nashorn does not treat enums specially. i.e., Nashorn does not 
automatically call array returning values() method. But, it is possible 
to adjust the script as follows:

var MaterialEnum = Packages.MaterialEnum;

var values = MaterialEnum.values();
for (var m in values) {
     if (values[m] &&

The above code works for older Javascript engine too.

Note that the above allows code to us cache array returned by values() 
method -- because for security reasons values() of any enum always 
returns a cloned array. Implicit values call in for..in would have to 
throw the cloned enum values array everytime.


On Saturday 25 January 2014 05:07 AM, Walter Higgins wrote:
> Say I have a Java Enum type, In previous versions of the Javascript engine,
> I could iterator over each value like this...
> for (var m in MaterialEnum){
>      if (MaterialEnum[m] &&
>          MaterialEnum[m].ordinal)
>      {
>          println(MaterialEnum[m]);
>      }
> }
> In Nashorn, this is no longer possible. Is this a bug or is it by design?

More information about the nashorn-dev mailing list