Skip to content

Commit

Permalink
fix(#1237): polyfill android Map.putIfAbsent
Browse files Browse the repository at this point in the history
Remove usage of putIfAbsent() in the Hashmap class. This method does not exist on many old Android releases and there's no real downside to removing it.

Fixes #1237
  • Loading branch information
naijun0403 authored Aug 6, 2022
1 parent e360e55 commit 0497ce6
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
7 changes: 4 additions & 3 deletions src/org/mozilla/javascript/Hashtable.java
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,10 @@ public int size() {

public void put(Object key, Object value) {
final Entry nv = new Entry(key, value);
final Entry ev = map.putIfAbsent(nv, nv);
if (ev == null) {

if (!map.containsKey(nv)) {
// New value -- insert to end of doubly-linked list
map.put(nv, nv);
if (first == null) {
first = last = nv;
} else {
Expand All @@ -133,7 +134,7 @@ public void put(Object key, Object value) {
}
} else {
// Update the existing value and keep it in the same place in the list
ev.value = value;
map.get(nv).value = value;
}
}

Expand Down
4 changes: 3 additions & 1 deletion src/org/mozilla/javascript/JavaMembers.java
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,9 @@ void discoverPublicMethods(Class<?> clazz, Map<MethodSignature, Method> map) {
static void registerMethod(Map<MethodSignature, Method> map, Method method) {
MethodSignature sig = new MethodSignature(method);
// Array may contain methods with same signature but different return value!
map.putIfAbsent(sig, method);
if (!map.containsKey(sig)) {
map.put(sig, method);
}
}

static final class MethodSignature {
Expand Down

0 comments on commit 0497ce6

Please sign in to comment.