Skip to content

Instantly share code, notes, and snippets.

@tomraithel
Created March 9, 2012 14:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomraithel/2006848 to your computer and use it in GitHub Desktop.
Save tomraithel/2006848 to your computer and use it in GitHub Desktop.
Java: Call Method via Reflection
@Test
public void testRef() throws IllegalArgumentException, IllegalAccessException, InvocationTargetException,
SecurityException, NoSuchMethodException {
RefTest instance = new RefTest();
Class params[] = {};
Method thisMethod = RefTest.class.getDeclaredMethod("getA", params);
String s = thisMethod.invoke(instance, new Object[0]).toString();
Assert.assertEquals("b", s);
}
public class RefTest {
public String getA() {
return "a";
}
public String getB() {
return "b";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment