Created
March 27, 2012 08:32
-
-
Save jdpage/2214012 to your computer and use it in GitHub Desktop.
Evil Java File
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
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