Skip to content

Instantly share code, notes, and snippets.

@artem-smotrakov
Created September 26, 2018 10:17
Show Gist options
  • Save artem-smotrakov/61458c68d38e6b890a55ad74947fcb4e to your computer and use it in GitHub Desktop.
Save artem-smotrakov/61458c68d38e6b890a55ad74947fcb4e to your computer and use it in GitHub Desktop.
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