Created
November 16, 2015 03:42
-
-
Save ethernet8023/cf8477f5a10d156e6499 to your computer and use it in GitHub Desktop.
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
| public class ChildClass extends ParentClass { | |
| // no interface to fooBar | |
| } |
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
| 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(); | |
| } | |
| } | |
| } |
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
| Function does not exist! | |
| java.lang.NoSuchMethodException: ParentClass.fooBar() | |
| at java.lang.Class.getDeclaredMethod(Class.java:2130) | |
| at Main.main(Main.java:8) |
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
| 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