Skip to content

Instantly share code, notes, and snippets.

@ethernet8023
Created November 16, 2015 03:42
Show Gist options
  • Select an option

  • Save ethernet8023/cf8477f5a10d156e6499 to your computer and use it in GitHub Desktop.

Select an option

Save ethernet8023/cf8477f5a10d156e6499 to your computer and use it in GitHub Desktop.
public class ChildClass extends ParentClass {
// no interface to fooBar
}
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class Main {
public static void main(String[] args) {
ChildClass c = new ChildClass();
try {
Method fooBar = c.getClass().getSuperclass().getDeclaredMethod("fooBar");
fooBar.setAccessible(true);
fooBar.invoke(c, true);
} catch (NoSuchMethodException e) {
System.out.println("Function does not exist!");
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
Function does not exist!
java.lang.NoSuchMethodException: ParentClass.fooBar()
at java.lang.Class.getDeclaredMethod(Class.java:2130)
at Main.main(Main.java:8)
public class ParentClass {
private int fooBar(boolean test) {
return 1;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment