Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDennis Wagelaar2016-02-22 10:33:22 -0500
committerDennis Wagelaar2016-02-22 10:35:39 -0500
commita7c1363c4d2944f67f5b05a5e72b3823fd869c23 (patch)
tree0a3d671a7fbf09cd09f4d82aaa5a2ce21e41446f /plugins/org.eclipse.m2m.atl.emftvm
parentf20666ccd8e6de068703c0ec0c022b7b5a8fe5d6 (diff)
downloadorg.eclipse.atl-a7c1363c4d2944f67f5b05a5e72b3823fd869c23.tar.gz
org.eclipse.atl-a7c1363c4d2944f67f5b05a5e72b3823fd869c23.tar.xz
org.eclipse.atl-a7c1363c4d2944f67f5b05a5e72b3823fd869c23.zip
488223: EMFTVMUtil#getMethodSignature() causes method lookup collisions
Task-Url: https://bugs.eclipse.org/bugs/show_bug.cgi?id=488223
Diffstat (limited to 'plugins/org.eclipse.m2m.atl.emftvm')
-rw-r--r--plugins/org.eclipse.m2m.atl.emftvm/src/org/eclipse/m2m/atl/emftvm/util/EMFTVMUtil.java46
-rw-r--r--plugins/org.eclipse.m2m.atl.emftvm/src/org/eclipse/m2m/atl/emftvm/util/MethodSignature.java118
2 files changed, 140 insertions, 24 deletions
diff --git a/plugins/org.eclipse.m2m.atl.emftvm/src/org/eclipse/m2m/atl/emftvm/util/EMFTVMUtil.java b/plugins/org.eclipse.m2m.atl.emftvm/src/org/eclipse/m2m/atl/emftvm/util/EMFTVMUtil.java
index 7b2ada7e..b17436a0 100644
--- a/plugins/org.eclipse.m2m.atl.emftvm/src/org/eclipse/m2m/atl/emftvm/util/EMFTVMUtil.java
+++ b/plugins/org.eclipse.m2m.atl.emftvm/src/org/eclipse/m2m/atl/emftvm/util/EMFTVMUtil.java
@@ -157,7 +157,7 @@ public final class EMFTVMUtil {
* @author <a href="mailto:mikael.barbero@obeo.fr">Mikael Barbero</a>
* @author <a href="mailto:dennis.wagelaar@vub.ac.be">Dennis Wagelaar</a>
*/
- private static final Map<Class<?>, Map<Integer, Method>> METHOD_CACHE = new WeakHashMap<Class<?>, Map<Integer, Method>>();
+ private static final Map<Class<?>, Map<MethodSignature, Method>> METHOD_CACHE = new WeakHashMap<Class<?>, Map<MethodSignature, Method>>();
/**
* Cache used to store the found root methods for native Java methods.
@@ -1393,7 +1393,7 @@ public final class EMFTVMUtil {
return null; // Java methods cannot be invoked on null, or defined on Void
}
- final Integer sig = getMethodSignature(opname, argTypes, isStatic);
+ final MethodSignature sig = getMethodSignature(opname, argTypes, isStatic);
Method ret = findCachedMethod(context, sig);
if (ret != null || hasCachedMethod(context, sig)) {
return ret;
@@ -1467,7 +1467,7 @@ public final class EMFTVMUtil {
return null; // Java methods cannot be invoked on null, or defined on Void
}
- final Integer sig = getMethodSignature(opname, argType, isStatic);
+ final MethodSignature sig = getMethodSignature(opname, argType, isStatic);
Method ret = findCachedMethod(context, sig);
if (ret != null || hasCachedMethod(context, sig)) {
return ret;
@@ -1538,7 +1538,7 @@ public final class EMFTVMUtil {
return null; // Java methods cannot be invoked on null, or defined on Void
}
- final Integer sig = getMethodSignature(opname, isStatic);
+ final MethodSignature sig = getMethodSignature(opname, isStatic);
Method ret = findCachedMethod(context, sig);
if (ret != null || hasCachedMethod(context, sig)) {
return ret;
@@ -1936,8 +1936,8 @@ public final class EMFTVMUtil {
* The method signature
* @return <code>true</code> if the method cache contains the given caller and signature
*/
- private static boolean hasCachedMethod(final Class<?> caller, final Integer signature) {
- final Map<Integer, Method> sigMap = METHOD_CACHE.get(caller);
+ private static boolean hasCachedMethod(final Class<?> caller, final MethodSignature signature) {
+ final Map<MethodSignature, Method> sigMap = METHOD_CACHE.get(caller);
return (sigMap != null) && sigMap.containsKey(signature);
}
@@ -1954,8 +1954,8 @@ public final class EMFTVMUtil {
* @author <a href="mailto:mikael.barbero@obeo.fr">Mikael Barbero</a>
* @author <a href="mailto:dennis.wagelaar@vub.ac.be">Dennis Wagelaar</a>
*/
- private static Method findCachedMethod(final Class<?> caller, final Integer signature) {
- final Map<Integer, Method> sigMap = METHOD_CACHE.get(caller);
+ private static Method findCachedMethod(final Class<?> caller, final MethodSignature signature) {
+ final Map<MethodSignature, Method> sigMap = METHOD_CACHE.get(caller);
return (sigMap != null) ? sigMap.get(signature) : null;
}
@@ -1973,11 +1973,11 @@ public final class EMFTVMUtil {
* @author <a href="mailto:mikael.barbero@obeo.fr">Mikael Barbero</a>
* @author <a href="mailto:dennis.wagelaar@vub.ac.be">Dennis Wagelaar</a>
*/
- private static void cacheMethod(final Class<?> caller, final Integer signature, final Method method) {
+ private static void cacheMethod(final Class<?> caller, final MethodSignature signature, final Method method) {
synchronized (METHOD_CACHE) {
- Map<Integer, Method> sigMap = METHOD_CACHE.get(caller);
+ Map<MethodSignature, Method> sigMap = METHOD_CACHE.get(caller);
if (sigMap == null) {
- sigMap = new HashMap<Integer, Method>();
+ sigMap = new HashMap<MethodSignature, Method>();
METHOD_CACHE.put(caller, sigMap);
}
sigMap.put(signature, method);
@@ -1985,7 +1985,7 @@ public final class EMFTVMUtil {
}
/**
- * Generates an int signature to store methods.
+ * Generates a signature to store methods.
*
* @param name
* @param argumentTypes
@@ -1996,16 +1996,13 @@ public final class EMFTVMUtil {
* @author <a href="mailto:mikael.barbero@obeo.fr">Mikael Barbero</a>
* @author <a href="mailto:dennis.wagelaar@vub.ac.be">Dennis Wagelaar</a>
*/
- private static int getMethodSignature(final String name, final Class<?>[] argumentTypes, final boolean isStatic) {
- int sig = (isStatic ? 31 : 0) + name.hashCode();
- for (int i = 0; i < argumentTypes.length; i++) {
- sig = sig * 31 + argumentTypes[i].hashCode();
- }
- return sig;
+ private static MethodSignature getMethodSignature(final String name, final Class<?>[] argumentTypes,
+ final boolean isStatic) {
+ return new MethodSignature(name, argumentTypes, isStatic);
}
/**
- * Generates an int signature to store methods.
+ * Generates a signature to store methods.
*
* @param name
* @param argumentType
@@ -2016,12 +2013,13 @@ public final class EMFTVMUtil {
* @author <a href="mailto:mikael.barbero@obeo.fr">Mikael Barbero</a>
* @author <a href="mailto:dennis.wagelaar@vub.ac.be">Dennis Wagelaar</a>
*/
- private static int getMethodSignature(final String name, final Class<?> argumentType, final boolean isStatic) {
- return ((isStatic ? 31 : 0) + name.hashCode()) * 31 + argumentType.hashCode();
+ private static MethodSignature getMethodSignature(final String name, final Class<?> argumentType,
+ final boolean isStatic) {
+ return new MethodSignature(name, new Class<?>[] { argumentType }, isStatic);
}
/**
- * Generates an int signature to store methods.
+ * Generates a signature to store methods.
*
* @param name
* @param isStatic
@@ -2031,8 +2029,8 @@ public final class EMFTVMUtil {
* @author <a href="mailto:mikael.barbero@obeo.fr">Mikael Barbero</a>
* @author <a href="mailto:dennis.wagelaar@vub.ac.be">Dennis Wagelaar</a>
*/
- private static int getMethodSignature(final String name, final boolean isStatic) {
- return (isStatic ? 31 : 0) + name.hashCode();
+ private static MethodSignature getMethodSignature(final String name, final boolean isStatic) {
+ return new MethodSignature(name, null, isStatic);
}
/**
diff --git a/plugins/org.eclipse.m2m.atl.emftvm/src/org/eclipse/m2m/atl/emftvm/util/MethodSignature.java b/plugins/org.eclipse.m2m.atl.emftvm/src/org/eclipse/m2m/atl/emftvm/util/MethodSignature.java
new file mode 100644
index 00000000..6c44da01
--- /dev/null
+++ b/plugins/org.eclipse.m2m.atl.emftvm/src/org/eclipse/m2m/atl/emftvm/util/MethodSignature.java
@@ -0,0 +1,118 @@
+/*******************************************************************************
+ * Copyright (c) 2016 Dennis Wagelaar.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Dennis Wagelaar - initial API and
+ * implementation and/or initial documentation
+ *******************************************************************************/
+package org.eclipse.m2m.atl.emftvm.util;
+
+import java.lang.reflect.Method;
+import java.util.Arrays;
+
+/**
+ * {@link Method} signature for EMFTVM method caching.
+ *
+ * @author <a href="mailto:dwagelaar@gmail.com">Dennis Wagelaar</a>
+ */
+public final class MethodSignature {
+
+ private final String name;
+ private final Class<?>[] argumentTypes;
+ private final boolean isStatic;
+
+ /**
+ * Creates a new {@link MethodSignature}.
+ *
+ * @param name
+ * the method name
+ * @param argumentTypes
+ * the method argument types
+ * @param isStatic
+ * whether the method is static
+ */
+ public MethodSignature(String name, Class<?>[] argumentTypes, boolean isStatic) {
+ super();
+ this.name = name;
+ this.argumentTypes = argumentTypes;
+ this.isStatic = isStatic;
+ }
+
+ /**
+ * Returns the method name.
+ *
+ * @return the name
+ */
+ public String getName() {
+ return name;
+ }
+
+ /**
+ * Returns the method argument types.
+ *
+ * @return the argumentTypes
+ */
+ public Class<?>[] getArgumentTypes() {
+ return argumentTypes;
+ }
+
+ /**
+ * Returns whether the method is static.
+ *
+ * @return the isStatic
+ */
+ public boolean isStatic() {
+ return isStatic;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public int hashCode() {
+ final int prime = 31;
+ int result = 1;
+ result = prime * result + Arrays.hashCode(argumentTypes);
+ result = prime * result + (isStatic ? 1231 : 1237);
+ result = prime * result + ((name == null) ? 0 : name.hashCode());
+ return result;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public boolean equals(final Object obj) {
+ if (this == obj)
+ return true;
+ if (obj == null)
+ return false;
+ if (getClass() != obj.getClass())
+ return false;
+ MethodSignature other = (MethodSignature) obj;
+ if (!Arrays.equals(argumentTypes, other.argumentTypes))
+ return false;
+ if (isStatic != other.isStatic)
+ return false;
+ if (name == null) {
+ if (other.name != null)
+ return false;
+ } else if (!name.equals(other.name))
+ return false;
+ return true;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public String toString() {
+ return "MethodSignature [name=" + name + ", argumentTypes=" + Arrays.toString(argumentTypes) + ", isStatic="
+ + isStatic + "]";
+ }
+
+}

Back to the top