Skip to content

Instantly share code, notes, and snippets.

@sagnitude
Created May 17, 2016 12:22
Show Gist options
  • Save sagnitude/c59d57ecd6b9bca7b28b3670ecdad275 to your computer and use it in GitHub Desktop.
Save sagnitude/c59d57ecd6b9bca7b28b3670ecdad275 to your computer and use it in GitHub Desktop.
test reflection
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();
}
}
}
package com.test;
public class Runner {
public static void main(String[] args) {
Base instance = new Sub();
instance.testReflection();
}
}
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