Created
May 17, 2016 12:22
-
-
Save sagnitude/c59d57ecd6b9bca7b28b3670ecdad275 to your computer and use it in GitHub Desktop.
test reflection
This file contains hidden or 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
package com.test; | |
import java.lang.reflect.InvocationTargetException; | |
import java.lang.reflect.Method; | |
class Base { | |
void testReflection() { | |
try { | |
Method method = this.getClass().getDeclaredMethod("subMethod", Integer.TYPE); | |
method.setAccessible(true); | |
System.out.println(method.invoke(this, 1)); | |
} catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) { | |
e.printStackTrace(); | |
} | |
} | |
} |
This file contains hidden or 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
package com.test; | |
public class Runner { | |
public static void main(String[] args) { | |
Base instance = new Sub(); | |
instance.testReflection(); | |
} | |
} |
This file contains hidden or 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
package com.test; | |
class Sub extends Base { | |
private int subMethod(int a) { | |
System.out.println(a); | |
return a+1; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment