Created
September 26, 2018 10:17
-
-
Save artem-smotrakov/61458c68d38e6b890a55ad74947fcb4e to your computer and use it in GitHub Desktop.
Accessing private fields with synthetic methods in Java, see also https://blog.gypsyengineer.com/en/security/accessing-private-fields-with-synthetic-methods-in-java.html
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 com.gypsyengineer.innerclass.field; | |
public class AccessPrivateField { | |
public static void test01() throws Exception { | |
System.out.println("Test #1: try to modify a private field with the same classloader"); | |
System.out.println(" (no exception is expected, 'oops' should be printed out)"); | |
Outer outer = new Outer(); | |
go(outer); | |
outer.check(); | |
} | |
// Modify a private field by calling a synthetic method | |
public static void go(Object t) throws Exception { | |
Method m = t.getClass().getDeclaredMethod( | |
"access$002", t.getClass(), int.class); | |
m.invoke(null, t, -1); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment