diff --git a/runtime/src/main/java/com/tns/Runtime.java b/runtime/src/main/java/com/tns/Runtime.java index e4651ef67..c63580ce1 100644 --- a/runtime/src/main/java/com/tns/Runtime.java +++ b/runtime/src/main/java/com/tns/Runtime.java @@ -80,9 +80,9 @@ void passUncaughtExceptionToJs(Throwable ex, String stackTrace) { "Primitive types need to be manually wrapped in their respective Object wrappers.\n" + "If you are creating an instance of an inner class, make sure to always provide reference to the outer `this` as the first argument."; - private final SparseArray strongInstances = new SparseArray(); + private final HashMap strongInstances = new HashMap<>(); - private final SparseArray> weakInstances = new SparseArray>(); + private final HashMap> weakInstances = new HashMap<>(); private final NativeScriptHashMap strongJavaObjectToID = new NativeScriptHashMap(); @@ -801,7 +801,7 @@ private void makeInstanceWeak(int javaObjectID, boolean keepAsWeak) { weakInstances.put(javaObjectID, new WeakReference(instance)); } - strongInstances.delete(javaObjectID); + strongInstances.remove(javaObjectID); strongJavaObjectToID.remove(instance); } @@ -829,17 +829,17 @@ private boolean makeInstanceWeakAndCheckIfAlive(int javaObjectID) { if (instance == null) { // The Java was moved from strong to weak, and then the Java instance was collected. weakInstances.remove(javaObjectID); - weakJavaObjectToID.remove(Integer.valueOf(javaObjectID)); + weakJavaObjectToID.remove(javaObjectID); return false; } else { return true; } } } else { - strongInstances.delete(javaObjectID); + strongInstances.remove(javaObjectID); strongJavaObjectToID.remove(instance); - weakJavaObjectToID.put(instance, Integer.valueOf(javaObjectID)); + weakJavaObjectToID.put(instance, javaObjectID); weakInstances.put(javaObjectID, new WeakReference(instance)); return true; @@ -862,7 +862,7 @@ private void checkWeakObjectAreAlive(ByteBuffer input, ByteBuffer output, int le if (instance == null) { isReleased = 1; - weakInstances.delete(javaObjectId); + weakInstances.remove(javaObjectId); } else { isReleased = 0; } @@ -880,7 +880,11 @@ private Object getJavaObjectByID(int javaObjectID) throws Exception { logger.write("Platform.getJavaObjectByID:" + javaObjectID); } - Object instance = strongInstances.get(javaObjectID, keyNotFoundObject); + Object instance = strongInstances.get(javaObjectID); + + if (instance == null) { + instance = keyNotFoundObject; + } if (instance == keyNotFoundObject) { WeakReference wr = weakInstances.get(javaObjectID);