summaryrefslogtreecommitdiffstats
path: root/vespajlib
diff options
context:
space:
mode:
authorHenning Baldersheim <balder@yahoo-inc.com>2022-08-25 06:33:04 +0200
committerHenning Baldersheim <balder@yahoo-inc.com>2022-08-25 06:52:13 +0200
commitaa90841e3e36242d286f79a1f53ae84a0e9ff216 (patch)
treeef7189a75612a6da71ddbaa0d2d01150bae3432e /vespajlib
parente3dd94868062b6673889df4dc9994710706fde1b (diff)
If you get a ClassCastException when doing clone by reflection it might be that you are using a
staale method reference that has been replaced by new code from a new/reloaded bundle. If so we clear the method cache and gives it a new try.
Diffstat (limited to 'vespajlib')
-rw-r--r--vespajlib/src/main/java/com/yahoo/collections/MethodCache.java14
-rw-r--r--vespajlib/src/main/java/com/yahoo/lang/PublicCloneable.java11
2 files changed, 23 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 ea8060e203b..4a4331b1c6e 100644
--- a/vespajlib/src/main/java/com/yahoo/collections/MethodCache.java
+++ b/vespajlib/src/main/java/com/yahoo/collections/MethodCache.java
@@ -3,10 +3,13 @@ package com.yahoo.collections;
import com.yahoo.concurrent.CopyOnWriteHashMap;
-import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
/**
+ * This will cache methods solved by reflection as reflection is expensive.
+ * Note that if the bundle from which the method is removed/changed you might have
+ * a problem... A ClassCastException might be one indication. Then clearing the cache and retrying it
+ * once to see if it goes away might be a solution.
* @author baldersheim
*/
public final class MethodCache {
@@ -18,7 +21,14 @@ public final class MethodCache {
this.methodName = methodName;
}
- public final Method get(Object object) {
+ /*
+ Clear all cached methods. Might be a wise thing to do, if you have cached some methods
+ that have changed due to new bundles being reloaded.
+ */
+ public void clear() {
+ cache.clear();
+ }
+ public Method get(Object object) {
Method m = cache.get(object.getClass().getName());
if (m == null) {
m = lookupMethod(object);
diff --git a/vespajlib/src/main/java/com/yahoo/lang/PublicCloneable.java b/vespajlib/src/main/java/com/yahoo/lang/PublicCloneable.java
new file mode 100644
index 00000000000..57a4bacdb5a
--- /dev/null
+++ b/vespajlib/src/main/java/com/yahoo/lang/PublicCloneable.java
@@ -0,0 +1,11 @@
+package com.yahoo.lang;
+
+/**
+ * This interface publicly exposes the clone method.
+ * Implement this to allow faster clone.
+ *
+ * @author bratseth
+ */
+public interface PublicCloneable<T> extends Cloneable {
+ T clone();
+}