Invoke private method with interface as argument
Posted
by Stephanie
on Stack Overflow
See other posts from Stack Overflow
or by Stephanie
Published on 2010-06-07T19:19:40Z
Indexed on
2010/06/07
19:22 UTC
Read the original article
Hit count: 325
Hi,
I've been attempting to invoke a private method whose argument is a parameter and I can't quite seem to get it right.
Here's kind of how the code looks so far:
public class TestClass {
public TestClass(){
}
private void simpleMethod( Map<String, Integer> testMap) {
//code logic
}
}
Then I attempt to use this to invoke the private method:
//Hashmap
Map <String, Integer> testMap = new HashMap <String, Integer>();
//method I want to invoke
Method simpleMethod = TestClass.class.getDeclaredMethod("simpleMethod", Map.class);
simpleMethod.setAccessible(true);
simpleMethod.invoke(testClassObject, testMap); //Throws an IllegalArgumentException
As you can see, it throws an IllegalArgumentException. I've attempted to cast the hashmap back to a map, but that didn't work.
What am I doing wrong?
© Stack Overflow or respective owner