Created
March 9, 2012 14:54
-
-
Save tomraithel/2006848 to your computer and use it in GitHub Desktop.
Java: Call Method via Reflection
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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