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,33 +89,36 @@ public class WrappedIntHashMap {
*/ */
public Object get(int key) { public Object get(int key) {
initializeGetMethod(); initializeGetMethod();
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 The object that was removed, or NULL if the key is not present.
*/
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 { try {
return GET_METHOD.invoke(handle, key); return method.invoke(handle, params);
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
throw new RuntimeException("Illegal argument.", e); throw new RuntimeException("Illegal argument.", e);
} catch (IllegalAccessException e) { } catch (IllegalAccessException e) {
throw new RuntimeException("Cannot access method.", e); throw new RuntimeException("Cannot access method.", e);
} catch (InvocationTargetException e) { } catch (InvocationTargetException e) {
throw new RuntimeException("Unable to invoke " + GET_METHOD + " on " + handle, e); throw new RuntimeException("Unable to invoke " + method + " on " + handle, e);
} }
} }
/**
* 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.
*/
public boolean remove(int key) {
Object prev = get(key);
if (prev != null) {
putInternal(key, null);
return true;
}
return false;
}
private void initializePutMethod() { private void initializePutMethod() {
if (PUT_METHOD == null) { if (PUT_METHOD == null) {
// Fairly straight forward // Fairly straight forward
@ -140,23 +136,27 @@ public class WrappedIntHashMap {
if (GET_METHOD == null) { if (GET_METHOD == null) {
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
@ -165,7 +165,7 @@ public class WrappedIntHashMap {
throw new IllegalStateException("Unable to find appropriate GET_METHOD for IntHashMap."); throw new IllegalStateException("Unable to find appropriate GET_METHOD for IntHashMap.");
} }
} }
/** /**
* Retrieve the underlying IntHashMap object. * Retrieve the underlying IntHashMap object.
* @return The underlying object. * @return The underlying object.