aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorjonmv <venstad@gmail.com>2022-10-26 09:19:50 +0200
committerjonmv <venstad@gmail.com>2022-10-26 09:19:50 +0200
commitb05406fb9cce3b9fe9dd7f4444d5d59eeee4e19f (patch)
treee06d312b0b06808dc4a5568b071032137ee69986
parent45a3477bbd1000daff7fb9a809062bcd0a06ae3f (diff)
Use weakly wrapped composite values
-rw-r--r--abi-check-plugin/src/main/java/com/yahoo/abicheck/mojo/AbiCheck.java1
-rw-r--r--container-core/src/main/java/com/yahoo/osgi/OsgiImpl.java2
-rw-r--r--vespajlib/src/main/java/com/yahoo/collections/MethodCache.java24
3 files changed, 17 insertions, 10 deletions
diff --git a/abi-check-plugin/src/main/java/com/yahoo/abicheck/mojo/AbiCheck.java b/abi-check-plugin/src/main/java/com/yahoo/abicheck/mojo/AbiCheck.java
index 1f22383db38..27b20701333 100644
--- a/abi-check-plugin/src/main/java/com/yahoo/abicheck/mojo/AbiCheck.java
+++ b/abi-check-plugin/src/main/java/com/yahoo/abicheck/mojo/AbiCheck.java
@@ -83,6 +83,7 @@ public class AbiCheck extends AbstractMojo {
.writeValue(writer, signatures);
}
}
+
// CLOVER:ON
private static boolean matchingClasses(String className, JavaClassSignature expected,
diff --git a/container-core/src/main/java/com/yahoo/osgi/OsgiImpl.java b/container-core/src/main/java/com/yahoo/osgi/OsgiImpl.java
index 1e171a19b05..2c7a0c2b86b 100644
--- a/container-core/src/main/java/com/yahoo/osgi/OsgiImpl.java
+++ b/container-core/src/main/java/com/yahoo/osgi/OsgiImpl.java
@@ -57,7 +57,7 @@ public class OsgiImpl implements Osgi {
return jdiscOsgi.getBundles(alwaysCurrentBundle);
}
- public Class<Object> resolveClass(BundleInstantiationSpecification spec) {
+ public Class<?> resolveClass(BundleInstantiationSpecification spec) {
Bundle bundle = getBundle(spec.bundle);
if (bundle != null) {
return resolveFromBundle(spec, bundle);
diff --git a/vespajlib/src/main/java/com/yahoo/collections/MethodCache.java b/vespajlib/src/main/java/com/yahoo/collections/MethodCache.java
index 1c081a0372f..9f5beb3a983 100644
--- a/vespajlib/src/main/java/com/yahoo/collections/MethodCache.java
+++ b/vespajlib/src/main/java/com/yahoo/collections/MethodCache.java
@@ -3,6 +3,7 @@ package com.yahoo.collections;
import com.yahoo.concurrent.CopyOnWriteHashMap;
+import java.lang.ref.WeakReference;
import java.lang.reflect.Method;
import java.util.function.Consumer;
@@ -16,7 +17,7 @@ import java.util.function.Consumer;
public final class MethodCache {
private final String methodName;
- private final CopyOnWriteHashMap<String, Method> cache = new CopyOnWriteHashMap<>();
+ private final CopyOnWriteHashMap<String, WeakReference<Pair<Class<?>, Method>>> cache = new CopyOnWriteHashMap<>();
public MethodCache(String methodName) {
this.methodName = methodName;
@@ -33,22 +34,27 @@ public final class MethodCache {
public Method get(Object object) {
return get(object, null);
}
+
public Method get(Object object, Consumer<String> onPut) {
- Method m = cache.get(object.getClass().getName());
- if ( ! m.getDeclaringClass().isAssignableFrom(object.getClass())) {
+ WeakReference<Pair<Class<?>, Method>> value = cache.get(object.getClass().getName());
+ Pair<Class<?>, Method> pair = value == null ? null : value.get();
+ if (pair == null || pair.getFirst() != object.getClass()) {
cache.clear();
- m = null;
+ pair = null;
}
- if (m == null) {
- m = lookupMethod(object);
- if (m != null) {
+ Method method = pair == null ? null : pair.getSecond();
+ if (method == null) {
+ method = lookupMethod(object);
+ if (method != null) {
+ pair = new Pair<>(object.getClass(), method);
if (onPut != null)
onPut.accept(object.getClass().getName());
- cache.put(object.getClass().getName(), m);
+ cache.put(object.getClass().getName(), new WeakReference<>(pair));
}
}
- return m;
+ return method;
}
+
private Method lookupMethod(Object object) {
try {
return object.getClass().getMethod(methodName);