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 GET_METHOD;
private static Method REMOVE_METHOD;
private Object handle;
@ -78,15 +79,7 @@ public class WrappedIntHashMap {
* @param value - the value to insert.
*/
private void putInternal(int key, Object value) {
try {
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);
}
invokeMethod(PUT_METHOD, key, value);
}
/**
@ -96,31 +89,34 @@ public class WrappedIntHashMap {
*/
public Object get(int key) {
initializeGetMethod();
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);
}
return invokeMethod(GET_METHOD, key);
}
/**
* Remove a mapping of a key to a value if it is present.
* @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) {
Object prev = get(key);
if (prev != null) {
putInternal(key, null);
return true;
public Object remove(int key) {
return invokeMethod(REMOVE_METHOD, key);
}
/**
* Invoke a particular method on the current handle
* @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() {
@ -141,22 +137,26 @@ public class WrappedIntHashMap {
WrappedIntHashMap temp = WrappedIntHashMap.newMap();
String expected = "hello";
// Initialize a value
temp.put(1, expected);
// Determine which method to trust
for (Method method : FuzzyReflection.fromClass(INT_HASH_MAP).
getMethodListByParameters(Object.class, new Class<?>[] { int.class })) {
// Initialize a value
temp.put(1, expected);
// Skip static methods
if (Modifier.isStatic(method.getModifiers()))
continue;
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
if (expected.equals(method.invoke(temp.getHandle(), 1))) {
if (first && !second) {
REMOVE_METHOD = method;
} else if (first && second) {
GET_METHOD = method;
return;
}
} catch (Exception e) {
// Suppress