Skip to content

Instantly share code, notes, and snippets.

@jdpage
Created March 27, 2012 08:32
Show Gist options
  • Save jdpage/2214010 to your computer and use it in GitHub Desktop.
Save jdpage/2214010 to your computer and use it in GitHub Desktop.
Evil Java File
import java.lang.reflect.Method;
import java.lang.reflect.Field;
public class FooTest {
static class Foo {
private int hehe = 0;
private void doSomething() {
System.out.println("Foo!");
}
}
public static void main(String[] args) throws Exception {
Foo foo = new Foo();
Method m = Foo.class.getDeclaredMethod("doSomething", new Class[]{});
Field f = Foo.class.getDeclaredField("hehe");
m.setAccessible(true);
f.setAccessible(true);
m.invoke(foo, new Object[]{});
System.out.println(f.getInt(foo));
f.set(foo, 10);
System.out.println(f.getInt(foo));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment