Last active
December 14, 2016 10:42
-
-
Save chalos/169fb42d00c0ddffeb1a1611545d9182 to your computer and use it in GitHub Desktop.
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.io.File; | |
import java.lang.reflect.*; | |
import java.net.URL; | |
import java.net.URLClassLoader; | |
import org.apache.bcel.classfile.ClassParser; | |
import org.apache.bcel.classfile.JavaClass; | |
import org.apache.bcel.generic.ClassGen; | |
import org.apache.bcel.util.ClassLoaderRepository; | |
import org.apache.bcel.util.ClassPath; | |
public class VerboseRun extends URLClassLoader { | |
protected VerboseRun(URL[] urls, ClassLoader parent) { | |
super(urls, parent); | |
} | |
@Override | |
public Class loadClass(String name) | |
throws ClassNotFoundException { | |
Class klass = super.loadClass(name); | |
return klass; | |
} | |
@Override | |
protected Class findClass(String name) | |
throws ClassNotFoundException { | |
intercept(name); | |
Class klass = super.findClass(name); | |
return klass; | |
} | |
public void intercept(String name) { | |
if(!name.equals("TestObject")) return; | |
System.out.println("intercept loading of: " + name); | |
ClassPath cp = new ClassPath(); | |
String classFilePath = null; | |
try { | |
classFilePath = cp.getClassFile(name, ".class").getPath(); | |
addSerializable(classFilePath); | |
} catch(Exception e) { | |
e.printStackTrace(System.out); | |
return; | |
} | |
} | |
public void addSerializable(String classFile) throws Exception { | |
ClassParser cp = new ClassParser(classFile); | |
JavaClass jc = cp.parse(); | |
System.out.println("Classname: " + jc.getClassName()); | |
String[] ifNames = jc.getInterfaceNames(); | |
for(String ifName: ifNames) { | |
System.out.println("Interface before: " + ifName); | |
if(ifName.equals("java.io.Serializable")) return; | |
} | |
ClassGen cg = new ClassGen(jc); | |
cg.addInterface("java.io.Serializable"); | |
JavaClass jc2 = cg.getJavaClass(); | |
jc2.dump(classFile); | |
} | |
public static void main(String[] args) { | |
if (args.length >= 1) { | |
try { | |
// load the target class to be run | |
ClassLoader baseCl = ClassLoader.getSystemClassLoader(); | |
URL[] urls; | |
if(baseCl instanceof URLClassLoader) { | |
urls = ((URLClassLoader)baseCl).getURLs(); | |
} else { | |
urls = new URL[] { | |
new File(".").toURI().toURL() | |
}; | |
} | |
System.out.println("Loading from paths:"); | |
for (int i = 0; i < urls.length; i++) { | |
System.out.println(" " + urls[i]); | |
} | |
VerboseRun loader = new VerboseRun(urls, baseCl.getParent()); | |
Class klass = loader.loadClass(args[0]); | |
Class[] ptypes = new Class[] { args.getClass() }; | |
Method main = klass.getDeclaredMethod("main", ptypes); | |
String[] pargs = new String[args.length-1]; | |
System.arraycopy(args, 1, pargs, 0, pargs.length); | |
Thread.currentThread().setContextClassLoader(loader); | |
main.invoke(null, new Object[] { pargs }); | |
} catch(Exception e) { | |
e.printStackTrace(); | |
} | |
} else { | |
System.out.println | |
("Usage: Run main-class args..."); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment