8173480: in operator should work on java objects and classes
authorhannesw
Tue, 31 Jan 2017 10:48:49 +0100
changeset 43352 1e536b9ddf07
parent 43351 75a27e55eb8a
child 43353 2498f60f2ef1
8173480: in operator should work on java objects and classes Reviewed-by: jlaskey, sundar
nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/ScriptRuntime.java
nashorn/test/script/basic/JDK-8173480.js
--- a/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/ScriptRuntime.java	Thu Jan 26 21:20:32 2017 +0000
+++ b/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/ScriptRuntime.java	Tue Jan 31 10:48:49 2017 +0100
@@ -45,6 +45,7 @@
 import java.util.Map;
 import java.util.NoSuchElementException;
 import java.util.Objects;
+import jdk.dynalink.beans.BeansLinker;
 import jdk.dynalink.beans.StaticClass;
 import jdk.nashorn.api.scripting.JSObject;
 import jdk.nashorn.api.scripting.ScriptObjectMirror;
@@ -56,6 +57,7 @@
 import jdk.nashorn.internal.objects.Global;
 import jdk.nashorn.internal.objects.NativeObject;
 import jdk.nashorn.internal.parser.Lexer;
+import jdk.nashorn.internal.runtime.arrays.ArrayIndex;
 import jdk.nashorn.internal.runtime.linker.Bootstrap;
 import jdk.nashorn.internal.runtime.linker.InvokeByName;
 
@@ -1039,7 +1041,30 @@
                 return ((JSObject)obj).hasMember(Objects.toString(property));
             }
 
-            return false;
+            final Object key = JSType.toPropertyKey(property);
+
+            if (obj instanceof StaticClass) {
+                final Class<?> clazz = ((StaticClass) obj).getRepresentedClass();
+                return BeansLinker.getReadableStaticPropertyNames(clazz).contains(Objects.toString(key))
+                    || BeansLinker.getStaticMethodNames(clazz).contains(Objects.toString(key));
+            } else {
+                if (obj instanceof Map && ((Map) obj).containsKey(key)) {
+                    return true;
+                }
+
+                final int index = ArrayIndex.getArrayIndex(key);
+                if (index >= 0) {
+                    if (obj instanceof List && index < ((List) obj).size()) {
+                        return true;
+                    }
+                    if (obj.getClass().isArray() && index < Array.getLength(obj)) {
+                        return true;
+                    }
+                }
+
+                return BeansLinker.getReadableInstancePropertyNames(obj.getClass()).contains(Objects.toString(key))
+                    || BeansLinker.getInstanceMethodNames(obj.getClass()).contains(Objects.toString(key));
+            }
         }
 
         throw typeError("in.with.non.object", rvalType.toString().toLowerCase(Locale.ENGLISH));
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/nashorn/test/script/basic/JDK-8173480.js	Tue Jan 31 10:48:49 2017 +0100
@@ -0,0 +1,87 @@
+/*
+ * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ * 
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ * 
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ * 
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ * 
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/**
+ * JDK-8173480: in operator should work on java objects and classes
+ *
+ * @test
+ * @run
+ */
+
+var hash = "hash"; // for testing ConsString keys
+
+var obj = new java.lang.Object();
+Assert.assertTrue("hashCode" in obj);
+Assert.assertTrue(hash + "Code" in obj);
+Assert.assertTrue("class" in obj);
+Assert.assertFalse("x" in obj);
+Assert.assertFalse(1 in obj);
+Assert.assertFalse("1" in obj);
+
+var map = new java.util.HashMap();
+map["k"] = true;
+Assert.assertTrue(map["k"]);
+Assert.assertTrue("k" in map);
+Assert.assertTrue("hashCode" in map);
+Assert.assertTrue(hash + "Code" in map);
+Assert.assertTrue("class" in map);
+Assert.assertFalse("x" in map);
+Assert.assertFalse(1 in map);
+Assert.assertFalse("1" in map);
+
+var list = new java.util.ArrayList();
+list.add(true);
+Assert.assertTrue(list[0]);
+Assert.assertTrue(list["0"]);
+Assert.assertTrue(0 in list);
+Assert.assertTrue("0" in list);
+Assert.assertTrue("hashCode" in list);
+Assert.assertTrue(hash + "Code" in list);
+Assert.assertTrue("class" in list);
+Assert.assertFalse("x" in list);
+Assert.assertFalse(1 in list);
+Assert.assertFalse("1" in list);
+
+var objectArray = new (Java.type("java.lang.Object[]"))(1);
+objectArray[0] = true;
+Assert.assertTrue(objectArray[0]);
+Assert.assertTrue(objectArray["0"]);
+Assert.assertTrue(0 in objectArray);
+Assert.assertTrue("0" in objectArray);
+Assert.assertTrue("hashCode" in objectArray);
+Assert.assertTrue(hash + "Code" in objectArray);
+Assert.assertTrue("class" in objectArray);
+Assert.assertFalse("x" in objectArray);
+Assert.assertFalse(1 in objectArray);
+Assert.assertFalse("1" in objectArray);
+
+var RuntimeClass = Java.type("java.lang.Runtime");
+Assert.assertTrue("getRuntime" in RuntimeClass);
+Assert.assertTrue("runtime" in RuntimeClass);
+Assert.assertTrue("class" in RuntimeClass);
+Assert.assertFalse("hashCode" in RuntimeClass);
+Assert.assertFalse(hash + "Code" in RuntimeClass);
+Assert.assertFalse("x" in RuntimeClass);
+Assert.assertFalse(1 in RuntimeClass);
+Assert.assertFalse("1" in RuntimeClass);
+