Skip to content

Instantly share code, notes, and snippets.

@mcy
Created March 21, 2014 15:28
Show Gist options
  • Save mcy/9688797 to your computer and use it in GitHub Desktop.
Save mcy/9688797 to your computer and use it in GitHub Desktop.
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.lang.reflect.Method;
import com.xorinc.foo.Foo;
public class Main {
public static void main(String... args) throws Exception{
Foo.mash();
InputStream classIn = new FileInputStream(new File("foo/Foo.class"));
ByteArrayOutputStream bais = new ByteArrayOutputStream();
Method define = ClassLoader.class.getDeclaredMethod("defineClass", String.class, byte[].class, int.class, int.class);
define.setAccessible(true);
while(true){
int datum = classIn.read();
if(datum == -1)
break;
bais.write(datum);
}
classIn.close();
byte[] data = bais.toByteArray();
define.invoke(ClassLoader.getSystemClassLoader(), "com.xorinc.foo.Foo", data, 0, data.length);
Foo.mash();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment