1 package hudson.remoting; 2 3 import org.objectweb.asm.ClassReader; 4 import org.objectweb.asm.ClassWriter; 5 6 import java.io.IOException ; 7 import java.io.InputStream ; 8 import java.io.File ; 9 import java.io.OutputStream ; 10 import java.io.FileOutputStream ; 11 import java.net.URL ; 12 13 19 class DummyClassLoader extends ClassLoader { 20 public DummyClassLoader(ClassLoader parent) { 21 super(parent); 22 } 23 24 25 protected Class <?> findClass(String name) throws ClassNotFoundException { 26 if(name.equals("hudson.remoting.test.TestCallable")) { 27 try { 29 byte[] bytes = loadTransformedClassImage(name); 30 return defineClass(name,bytes,0,bytes.length); 31 } catch (IOException e) { 32 throw new ClassNotFoundException ("Bytecode manipulation failed",e); 33 } 34 } 35 36 return super.findClass(name); 37 } 38 39 private byte[] loadTransformedClassImage(final String name) throws IOException { 40 InputStream in = getResourceAsStream("hudson/remoting/TestCallable.class"); 41 42 ClassReader cr = new ClassReader(in); 44 ClassWriter w = new ClassWriter(cr,true) { 45 public void visit(int version, int access, String _name, String sig, String superName, String [] interfaces) { 46 super.visit(version, access, name.replace('.','/'), sig, superName, interfaces); 47 } 48 }; 49 cr.accept(w,false); 50 51 return w.toByteArray(); 52 } 53 54 55 protected URL findResource(String name) { 56 if(name.equals("hudson/remoting/test/TestCallable.class")) { 57 try { 58 File f = File.createTempFile("rmiTest","class"); 59 OutputStream os = new FileOutputStream (f); 60 os.write(loadTransformedClassImage("hudson.remoting.test.TestCallable")); 61 os.close(); 62 f.deleteOnExit(); 63 return f.toURL(); 64 } catch (IOException e) { 65 return null; 66 } 67 } 68 return super.findResource(name); 69 } 70 } 71 | Popular Tags |