4950515253545556
@Override public void test() throws Exception { ClassReader cr = new ClassReader(is); ClassWriter cw = new ClassWriter(0); cr.accept(new CheckClassAdapter(cw), 0); assertEquals(cr, new ClassReader(cw.toByteArray())); } }
7071727374757677787980
try { ClassReader cr = new ClassReader(is); ClassWriter cw = new ClassWriter(0); ClassVisitor cv = new TraceFieldClassAdapter(cw); cr.accept(cv, 0); b = cw.toByteArray(); } catch (Exception e) { throw new ClassNotFoundException(name, e); } // optional: stores the adapted class on disk
9596979899100101102103104105
mv.visitInsn(IRETURN); // max stack and max locals automatically computed mv.visitMaxs(0, 0); mv.visitEnd(); return cw.toByteArray(); } /* * Compile this expression. This method must append to the given code writer * the byte code that evaluates and pushes on the stack the value of this
130131132133134135136137138139140
@Override public Class<?> loadClass(final String name) throws ClassNotFoundException { if (name.equals(n)) { byte[] b = cw.toByteArray(); return defineClass(name, b, 0, b.length); } return super.loadClass(name); } }.loadClass(n);
// variables mw.visitMaxs(2, 2); mw.visitEnd(); // gets the bytecode of the Example class, and loads it dynamically byte[] code = cw.toByteArray(); FileOutputStream fos = new FileOutputStream("Example.class"); fos.write(code); fos.close();
140141142143144145146147148149150
mg.returnValue(); mg.endMethod(); cw.visitEnd(); code = cw.toByteArray(); loader = new Helloworld(); exampleClass = loader.defineClass("Example", code, 0, code.length); // uses the dynamically generated class to print 'Helloworld' exampleClass.getMethods()[0].invoke(null, new Object[] { null });
7576777879808182838485
} r.close(); FileOutputStream os = new FileOutputStream(className + ".class"); os.write(cw.toByteArray()); os.flush(); os.close(); } }
534535536537538539540541542543544
ClassWriter.COMPUTE_MAXS); return new ASMContentHandler(cw) { @Override public void endDocument() throws SAXException { try { os.write(cw.toByteArray()); } catch(IOException e) { throw new SAXException(e); } } };
113114115116117118119120121122123
cr.accept(co, ClassReader.SKIP_DEBUG); Set<Constant> constants = new TreeSet<Constant>(new ConstantComparator()); constants.addAll(cp.values()); cr = new ClassReader(cw.toByteArray()); cw = new ClassWriter(0); Iterator<Constant> i = constants.iterator(); while (i.hasNext()) { Constant c = i.next(); c.write(cw);
133134135136137138139140141142143
if (!g.getParentFile().exists() && !g.getParentFile().mkdirs()) { throw new IOException("Cannot create directory " + g.getParentFile()); } OutputStream os = new FileOutputStream(g); try { os.write(cw.toByteArray()); } finally { os.close(); } } }