diff options
Diffstat (limited to 'vespajlib')
-rw-r--r-- | vespajlib/src/main/java/com/yahoo/collections/MethodCache.java | 2 | ||||
-rw-r--r-- | vespajlib/src/test/java/com/yahoo/collections/MethodCacheTest.java | 13 |
2 files changed, 13 insertions, 2 deletions
diff --git a/vespajlib/src/main/java/com/yahoo/collections/MethodCache.java b/vespajlib/src/main/java/com/yahoo/collections/MethodCache.java index ead1d109fba..d0da7617b58 100644 --- a/vespajlib/src/main/java/com/yahoo/collections/MethodCache.java +++ b/vespajlib/src/main/java/com/yahoo/collections/MethodCache.java @@ -47,7 +47,7 @@ public final class MethodCache { method = lookupMethod(object); pair = new Pair<>(object.getClass(), method); cache.put(object.getClass().getName(), new WeakReference<>(pair)); - if (method != null && onPut != null) + if (onPut != null) onPut.accept(object.getClass().getName()); } return method; diff --git a/vespajlib/src/test/java/com/yahoo/collections/MethodCacheTest.java b/vespajlib/src/test/java/com/yahoo/collections/MethodCacheTest.java index 7b3d3e98cec..02efd10e91d 100644 --- a/vespajlib/src/test/java/com/yahoo/collections/MethodCacheTest.java +++ b/vespajlib/src/test/java/com/yahoo/collections/MethodCacheTest.java @@ -9,6 +9,7 @@ import java.util.concurrent.atomic.AtomicBoolean; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotSame; +import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertSame; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -60,11 +61,21 @@ class MethodCacheTest { cachedCustomMethod.invoke(customDummy); assertThrows(IllegalArgumentException.class, () -> applicationMethod.invoke(customDummy)); assertThrows(IllegalArgumentException.class, () -> customMethod.invoke(applicationDummy)); + + Object noDummy = new NoDummy(); + Method noMethod = methods.get(noDummy, __ -> updatedCache.set(true)); + assertTrue(updatedCache.getAndSet(false), "cache was updated"); + assertNull(noMethod); + + Method cachedNoMethod = methods.get(noDummy, __ -> updatedCache.set(true)); + assertFalse(updatedCache.getAndSet(false), "cache was updated"); + assertNull(cachedNoMethod); } } + public static class NoDummy implements Cloneable { } + public static class Dummy implements Cloneable { - public Dummy() { } public Object clone() throws CloneNotSupportedException { return super.clone(); } } |