From a54a6c6b6a6b91db9a2aa175843b749fb4f5822a Mon Sep 17 00:00:00 2001 From: Hannes Wellmann Date: Sat, 30 Dec 2023 11:33:40 +0100 Subject: [PATCH] Fix removal of bindings in ProvisionListenerCallbackStore and simplify Bindings have never been removed from the cache in ProvisionListenerCallbackStore.remove() because the key of the cache were KeyBinding objects, while it was attempted to remove Binding objects. Additionally use a standard Java ConcurrentHashMap instead of a simple Guava Cache, which also allows to remove the now unnecessary KeyBinding helper class. Instead of a new KeyBinding object a new new lambda object is created for each cache lookup. --- .../ProvisionListenerCallbackStore.java | 47 ++++--------------- 1 file changed, 8 insertions(+), 39 deletions(-) diff --git a/core/src/com/google/inject/internal/ProvisionListenerCallbackStore.java b/core/src/com/google/inject/internal/ProvisionListenerCallbackStore.java index 063a4f4fd9..7db147cd5b 100644 --- a/core/src/com/google/inject/internal/ProvisionListenerCallbackStore.java +++ b/core/src/com/google/inject/internal/ProvisionListenerCallbackStore.java @@ -16,9 +16,6 @@ package com.google.inject.internal; -import com.google.common.cache.CacheBuilder; -import com.google.common.cache.CacheLoader; -import com.google.common.cache.LoadingCache; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableSet; import com.google.common.collect.Lists; @@ -29,6 +26,8 @@ import com.google.inject.spi.ProvisionListener; import com.google.inject.spi.ProvisionListenerBinding; import java.util.List; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; import java.util.logging.Logger; /** @@ -45,15 +44,7 @@ final class ProvisionListenerCallbackStore { private final ImmutableList listenerBindings; - private final LoadingCache> cache = - CacheBuilder.newBuilder() - .build( - new CacheLoader>() { - @Override - public ProvisionListenerStackCallback load(KeyBinding key) { - return create(key.binding); - } - }); + private final Map, ProvisionListenerStackCallback> cache = new ConcurrentHashMap<>(); ProvisionListenerCallbackStore(List listenerBindings) { this.listenerBindings = ImmutableList.copyOf(listenerBindings); @@ -63,14 +54,12 @@ public ProvisionListenerStackCallback load(KeyBinding key) { * Returns a new {@link ProvisionListenerStackCallback} for the key or {@code null} if there are * no listeners */ - @SuppressWarnings( - "unchecked") // the ProvisionListenerStackCallback type always agrees with the passed type + @SuppressWarnings("unchecked") // the ProvisionListenerStackCallback type always agrees with the passed type public ProvisionListenerStackCallback get(Binding binding) { // Never notify any listeners for internal bindings. if (!INTERNAL_BINDINGS.contains(binding.getKey())) { - ProvisionListenerStackCallback callback = - (ProvisionListenerStackCallback) - cache.getUnchecked(new KeyBinding(binding.getKey(), binding)); + ProvisionListenerStackCallback callback = (ProvisionListenerStackCallback) cache + .computeIfAbsent(binding.getKey(), k -> create(binding)); return callback.hasListeners() ? callback : null; } return null; @@ -86,7 +75,7 @@ public ProvisionListenerStackCallback get(Binding binding) { *

Returns true if the type was stored in the cache, false otherwise. */ boolean remove(Binding type) { - return cache.asMap().remove(type) != null; + return cache.remove(type.getKey()) != null; } /** @@ -107,27 +96,7 @@ private ProvisionListenerStackCallback create(Binding binding) { // no listeners. return ProvisionListenerStackCallback.emptyListener(); } - return new ProvisionListenerStackCallback(binding, listeners); + return new ProvisionListenerStackCallback<>(binding, listeners); } - /** A struct that holds key and binding but uses just key for equality/hashcode. */ - private static class KeyBinding { - final Key key; - final Binding binding; - - KeyBinding(Key key, Binding binding) { - this.key = key; - this.binding = binding; - } - - @Override - public boolean equals(Object obj) { - return obj instanceof KeyBinding && key.equals(((KeyBinding) obj).key); - } - - @Override - public int hashCode() { - return key.hashCode(); - } - } }