protected byte[] transform(final String pName, final InputStream pClassStream) throws IOException { System.err.println("Transforming " + pName); final byte[] oldClass = IOUtils.toByteArray(pClassStream); final byte[] newClass = transformer.transform(oldClass); // CheckClassAdapter.verify(new ClassReader(newClass), true); new ClassReader(oldClass).accept( new ASMifierClassVisitor( new PrintWriter( new FileOutputStream( transformer.getClass().getSimpleName() + pName + ".old"))), 0); new ClassReader(newClass).accept( new ASMifierClassVisitor( new PrintWriter( new FileOutputStream( transformer.getClass().getSimpleName() + pName + ".new"))), 0); return newClass; }
protected byte[] transform(final String pName, final InputStream pClassStream) throws IOException { final byte[] oldClass = IOUtils.toByteArray(pClassStream); final byte[] newClass = transformer.transform(oldClass); // CheckClassAdapter.verify(new ClassReader(newClass), true); new ClassReader(oldClass).accept( new ASMifierClassVisitor( new PrintWriter( new FileOutputStream( transformer.getClass().getSimpleName() + pName + ".old"))), 0); new ClassReader(newClass).accept( new ASMifierClassVisitor( new PrintWriter( new FileOutputStream( transformer.getClass().getSimpleName() + pName + ".new"))), 0); return newClass; }
@Test @Ignore public void test() throws IOException { for (final Class clazz : new Class[] { HandTypesArrayList.class, HandTypesAllocation.class, HandTypesListElement.class, HandTypesValueImpl.class }) { // for (Class clazz : new Class[]{HandTypesArrayList.class}) { final ClassReader cr = new ClassReader(clazz.getName()); final StringWriter sw = new StringWriter(); final ASMifierClassVisitor cv = new ASMifierClassVisitor(new PrintWriter(sw)); cr.accept(cv, 0); final String text = sw.toString(); System.out.println(text.replaceAll("\"com/weibo/lodil/mmap/hand/HandTypes", "name + \"") .replaceAll("Lcom/weibo/lodil/mmap/hand/HandTypes", "L\" + name + \"") .replaceAll("\"com/weibo/lodil/mmap/HandTypes\"", "name") .replaceAll("Lcom/weibo/lodil/mmap/HandTypes;", "L\" + name + \";") .replaceAll("\"com/weibo/lodil/mmap/", "collections + \"") .replaceAll("Lcom/weibo/lodil/mmap/", "L\" + collections + \"")); } }
public void test() throws Exception { String n = "org.objectweb.asm.attrs.StackMapTableSample"; InputStream is = StackMapTableAttributeTest.class.getResourceAsStream(StackMapTableAttributeTest.TEST_CLASS); ClassReader cr = new ClassReader(is); StringWriter sw = new StringWriter(); ASMifierClassVisitor cv = new ASMifierClassVisitor(new PrintWriter(sw)); cr.accept(cv, AbstractVisitor.getDefaultAttributes(), false); String generated = sw.toString(); byte[] generatorClassData; try { generatorClassData = ASMifierTest.COMPILER.compile(n, generated); System.err.println(generated); } catch (Exception ex) { System.err.println(generated); System.err.println("------------------"); throw ex; } Class c = ASMifierTest.LOADER.defineClass("asm." + n + "Dump", generatorClassData); Method m = c.getMethod("dump", new Class[0]); byte[] b = (byte[]) m.invoke(null, new Object[0]); assertEquals(cr, new ClassReader(b)); }