From dd9c6384ea2fb5de509ab5547aac239c10ad2e71 Mon Sep 17 00:00:00 2001 From: Roland Praml Date: Sat, 7 Oct 2023 01:23:47 +0200 Subject: [PATCH] javascript 'Set' cannot handle wrapped java objects properly (#1387) * Set cannot handle wrapped java objects properly * Satisfy spotbugs --- src/org/mozilla/javascript/NativeJavaArray.java | 12 ++++++++++++ src/org/mozilla/javascript/NativeJavaClass.java | 10 ++++++++++ src/org/mozilla/javascript/NativeJavaList.java | 10 ++++++++++ src/org/mozilla/javascript/NativeJavaMap.java | 10 ++++++++++ src/org/mozilla/javascript/NativeJavaObject.java | 13 +++++++++++++ testsrc/jstests/es6/set.js | 9 ++++++++- 6 files changed, 63 insertions(+), 1 deletion(-) diff --git a/src/org/mozilla/javascript/NativeJavaArray.java b/src/org/mozilla/javascript/NativeJavaArray.java index 0195c115c4..2253ba9dc6 100644 --- a/src/org/mozilla/javascript/NativeJavaArray.java +++ b/src/org/mozilla/javascript/NativeJavaArray.java @@ -7,6 +7,7 @@ package org.mozilla.javascript; import java.lang.reflect.Array; +import java.util.Objects; /** * This class reflects Java arrays into the JavaScript environment. @@ -143,6 +144,17 @@ public Scriptable getPrototype() { return prototype; } + @Override + public boolean equals(Object obj) { + return (obj instanceof NativeJavaArray) + && Objects.equals(((NativeJavaArray) obj).array, array); + } + + @Override + public int hashCode() { + return array == null ? 0 : array.hashCode(); + } + Object array; int length; Class cls; diff --git a/src/org/mozilla/javascript/NativeJavaClass.java b/src/org/mozilla/javascript/NativeJavaClass.java index a90becbac3..7f41b07243 100644 --- a/src/org/mozilla/javascript/NativeJavaClass.java +++ b/src/org/mozilla/javascript/NativeJavaClass.java @@ -278,4 +278,14 @@ private static Class findNestedClass(Class parentClass, String name) { } private Map staticFieldAndMethods; + + @Override + public boolean equals(Object obj) { + return super.equals(obj); + } + + @Override + public int hashCode() { + return super.hashCode(); + } } diff --git a/src/org/mozilla/javascript/NativeJavaList.java b/src/org/mozilla/javascript/NativeJavaList.java index 464dbfc7df..0fb3b69968 100644 --- a/src/org/mozilla/javascript/NativeJavaList.java +++ b/src/org/mozilla/javascript/NativeJavaList.java @@ -183,4 +183,14 @@ public Object[] getIds() { private boolean isWithValidIndex(int index) { return index >= 0 && index < list.size(); } + + @Override + public boolean equals(Object obj) { + return super.equals(obj); + } + + @Override + public int hashCode() { + return super.hashCode(); + } } diff --git a/src/org/mozilla/javascript/NativeJavaMap.java b/src/org/mozilla/javascript/NativeJavaMap.java index d6acf6d6b3..752f2c0edf 100644 --- a/src/org/mozilla/javascript/NativeJavaMap.java +++ b/src/org/mozilla/javascript/NativeJavaMap.java @@ -142,6 +142,16 @@ public Object[] getIds() { return super.getIds(); } + @Override + public boolean equals(Object obj) { + return super.equals(obj); + } + + @Override + public int hashCode() { + return super.hashCode(); + } + private static Callable symbol_iterator = (Context cx, Scriptable scope, Scriptable thisObj, Object[] args) -> { if (!(thisObj instanceof NativeJavaMap)) { diff --git a/src/org/mozilla/javascript/NativeJavaObject.java b/src/org/mozilla/javascript/NativeJavaObject.java index 94ae344cf0..39d7e4e249 100644 --- a/src/org/mozilla/javascript/NativeJavaObject.java +++ b/src/org/mozilla/javascript/NativeJavaObject.java @@ -17,6 +17,7 @@ import java.util.Date; import java.util.Iterator; import java.util.Map; +import java.util.Objects; /** * This class reflects non-Array Java objects into the JavaScript environment. It reflect fields @@ -986,4 +987,16 @@ protected String getTag() { } } } + + @Override + public boolean equals(Object obj) { + return obj != null + && obj.getClass().equals(getClass()) + && Objects.equals(((NativeJavaObject) obj).javaObject, javaObject); + } + + @Override + public int hashCode() { + return javaObject == null ? 0 : javaObject.hashCode(); + } } diff --git a/testsrc/jstests/es6/set.js b/testsrc/jstests/es6/set.js index cf3d2e4f1b..ae45ed9dda 100644 --- a/testsrc/jstests/es6/set.js +++ b/testsrc/jstests/es6/set.js @@ -7,7 +7,7 @@ load("testsrc/assert.js"); res = ""; function logElement(value, key) { - res += "set[" + key + "] (" + this + ") "; + res += "set[" + key + "] (" + this + ") "; } (function TestForEach() { @@ -81,4 +81,11 @@ function logElement(value, key) { } })(); +(function TestSetAddJavaEnums() { + var mySet = new Set(); + mySet.add(java.nio.file.AccessMode.READ); + mySet.add(java.nio.file.AccessMode.READ); + assertEquals(1, mySet.size); +})(); + "success"; \ No newline at end of file