Archiviert
13
0

Call the correct get-method (not remove) in IntHashMap. Fixes #7

As it happens, there are two very similar methods in IntHashMap:
  Object get(int key);
  Object remove(int key);
  
I called the latter by mistake. Now, I distinguish between the two 
by performing a removal test.
Dieser Commit ist enthalten in:
Kristian S. Stangeland 2013-08-29 01:32:16 +02:00
Ursprung 3f8a48732e
Commit fc01a61290

Datei anzeigen

@ -20,6 +20,7 @@ public class WrappedIntHashMap {
private static Method PUT_METHOD; private static Method PUT_METHOD;
private static Method GET_METHOD; private static Method GET_METHOD;
private static Method REMOVE_METHOD;
private Object handle; private Object handle;
@ -78,15 +79,7 @@ public class WrappedIntHashMap {
* @param value - the value to insert. * @param value - the value to insert.
*/ */
private void putInternal(int key, Object value) { private void putInternal(int key, Object value) {
try { invokeMethod(PUT_METHOD, key, value);
PUT_METHOD.invoke(handle, key, value);
} catch (IllegalArgumentException e) {
throw new RuntimeException("Illegal argument.", e);
} catch (IllegalAccessException e) {
throw new RuntimeException("Cannot access method.", e);
} catch (InvocationTargetException e) {
throw new RuntimeException("Exception occured in " + PUT_METHOD + " for" + handle, e);
}
} }
/** /**
@ -96,31 +89,34 @@ public class WrappedIntHashMap {
*/ */
public Object get(int key) { public Object get(int key) {
initializeGetMethod(); initializeGetMethod();
return invokeMethod(GET_METHOD, key);
try {
return GET_METHOD.invoke(handle, key);
} catch (IllegalArgumentException e) {
throw new RuntimeException("Illegal argument.", e);
} catch (IllegalAccessException e) {
throw new RuntimeException("Cannot access method.", e);
} catch (InvocationTargetException e) {
throw new RuntimeException("Unable to invoke " + GET_METHOD + " on " + handle, e);
}
} }
/** /**
* Remove a mapping of a key to a value if it is present. * Remove a mapping of a key to a value if it is present.
* @param key - the key of the mapping to remove. * @param key - the key of the mapping to remove.
* @return TRUE if a key-value pair was removed, FALSE otherwise. * @return The object that was removed, or NULL if the key is not present.
*/ */
public boolean remove(int key) { public Object remove(int key) {
Object prev = get(key); return invokeMethod(REMOVE_METHOD, key);
}
if (prev != null) { /**
putInternal(key, null); * Invoke a particular method on the current handle
return true; * @param method - the current method.
* @param params - parameters.
* @return The return value, if any.
*/
private Object invokeMethod(Method method, Object... params) {
try {
return method.invoke(handle, params);
} catch (IllegalArgumentException e) {
throw new RuntimeException("Illegal argument.", e);
} catch (IllegalAccessException e) {
throw new RuntimeException("Cannot access method.", e);
} catch (InvocationTargetException e) {
throw new RuntimeException("Unable to invoke " + method + " on " + handle, e);
} }
return false;
} }
private void initializePutMethod() { private void initializePutMethod() {
@ -141,22 +137,26 @@ public class WrappedIntHashMap {
WrappedIntHashMap temp = WrappedIntHashMap.newMap(); WrappedIntHashMap temp = WrappedIntHashMap.newMap();
String expected = "hello"; String expected = "hello";
// Initialize a value
temp.put(1, expected);
// Determine which method to trust // Determine which method to trust
for (Method method : FuzzyReflection.fromClass(INT_HASH_MAP). for (Method method : FuzzyReflection.fromClass(INT_HASH_MAP).
getMethodListByParameters(Object.class, new Class<?>[] { int.class })) { getMethodListByParameters(Object.class, new Class<?>[] { int.class })) {
// Initialize a value
temp.put(1, expected);
// Skip static methods // Skip static methods
if (Modifier.isStatic(method.getModifiers())) if (Modifier.isStatic(method.getModifiers()))
continue; continue;
try { try {
boolean first = expected.equals(method.invoke(temp.getHandle(), 1));
boolean second = expected.equals(method.invoke(temp.getHandle(), 1));
// See if we found the method we are looking for // See if we found the method we are looking for
if (expected.equals(method.invoke(temp.getHandle(), 1))) { if (first && !second) {
REMOVE_METHOD = method;
} else if (first && second) {
GET_METHOD = method; GET_METHOD = method;
return;
} }
} catch (Exception e) { } catch (Exception e) {
// Suppress // Suppress