-
-
Save anonymous/2c87d8a88b3c1a449b54deab4ec3fe88 to your computer and use it in GitHub Desktop.
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
package app1; | |
import app1.pkg.Other; | |
import java.util.List; | |
public class MiscTest5 { | |
public static void main(String[] args) throws Exception { | |
System.out.println(System.getProperty("java.version")); | |
List<String> list = Other.getList(); | |
System.out.println("Normal: " + list.size()); | |
System.out.println("Reflective: " + list.getClass().getMethod("size").invoke(list)); | |
} | |
} |
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
package app1.pkg; | |
import java.util.ArrayList; | |
import java.util.List; | |
public class Other { | |
static class MyList extends ArrayList<String> { | |
public int size() { | |
return super.size(); | |
} | |
} | |
public static List<String> getList() { | |
return new MyList(); | |
} | |
} |
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
$ java app1.MiscTest5 | |
1.8.0_102 | |
Normal: 0 | |
Exception in thread "main" java.lang.IllegalAccessException: Class app1.MiscTest5 can not access a member of class app1.pkg.Other$MyList with modifiers "public" | |
at sun.reflect.Reflection.ensureMemberAccess(Reflection.java:102) | |
at java.lang.reflect.AccessibleObject.slowCheckMemberAccess(AccessibleObject.java:296) | |
at java.lang.reflect.AccessibleObject.checkAccess(AccessibleObject.java:288) | |
at java.lang.reflect.Method.invoke(Method.java:491) | |
at app1.MiscTest5.main(MiscTest5.java:11) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment