private ClassNode loadClass(String name) { return Try.apply(() -> { String cname = name.replace("/", "."); ClassNode node = cache.get(cname); if (node == null) { String rname = cname.replace(".", "/") + ".class"; try (InputStream in = loader.getResourceAsStream(rname)) { if (in == null) { throw new FileNotFoundException(rname + " using " + loader); } ClassReader reader = new ClassReader(ByteStreams.toByteArray(in)); node = new ClassNode(); reader.accept(node, 0); cache.put(cname, node); if (log.isDebugEnabled()) { log.info("Source: {}; Class: {}", node.sourceFile, node.name); reader.accept( new TraceClassVisitor(null, new ASMifier(), new PrintWriter(writer(log, name))), 0); } } } return node; }).get(); }
public Class<?> loadClass(ClassCompilationContext ctx) { // generate bytecode ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES); ctx.getClazz().accept(cw); // load class Class<?> cls; byte[] bb = cw.toByteArray(); String className = Type.getObjectType(ctx.getClazz().name).getClassName(); // try { // Files.write(bb, new File("target/compiledRecipes/" + ctx.clazz.name // + ".class")); // } catch (IOException e2) { // throw new SaltaException("Error while writing generated class", e2); // } try { cls = getLoader().defineClass(className, bb); ctx.initFields(cls); } catch (Throwable e) { System.out.println("Error while loading compiled recipe class"); ClassReader cr = new ClassReader(bb); cr.accept(new TraceClassVisitor(null, new ASMifier(), new PrintWriter(System.out)), 0); CheckClassAdapter.verify(cr, false, new PrintWriter(System.err)); throw new SaltaException("Error while loading compiled recipe", e); } // return result return cls; }
private Printer newPrinter(final String s) { Printer printer=new ASMifier(Opcodes.ASM5, "mv", 0) { // or Textifier(ASM5) @Override public void visitMethodEnd() { System.out.println("======================== "+s+" =================="); PrintWriter p=new PrintWriter(System.out); print(p); p.flush(); // don't forget that! } }; return printer; }
public static void main(String[] args) throws Exception { ImmutableList<Class<?>> classes = ImmutableList.of( HelloWorld.class ); for (Class<?> aClass : classes) { System.out.println("----------------------------------------------------------------------------------------------------------------------------------"); ASMifier.main(new String[] { aClass.getCanonicalName() }); System.out.println("----------------------------------------------------------------------------------------------------------------------------------"); } }
@Test public void printClass(){ try { ASMifier.main(new String[]{AccountVO1Wrapper.class.getName()}); } catch (Exception e) { Assert.fail("asmifier", e); } }
@Test public void gen(){//得到wrapper的样本 try { ASMifier.main(new String[]{AccountVO.class.getName()}); } catch (Exception e) { Assert.fail("gen class", e); } }
public static void main(String[] args) throws Exception { ASMifier.main(new String[]{"target/classes/groovy/swing/SwingBuilder.class"}); // ASMifierClassVisitor.main(new String[]{"target/classes/org/codehaus/groovy/tools/shell/util/HelpFormatter.class"}); // ASMifierClassVisitor.main(new String[]{"target/classes/org/codehaus/groovy/runtime/callsite/PogoMetaMethodSite.class"}); // ASMifierClassVisitor.main(new String[]{"target/test-classes/spectralnorm.class"}); // ASMifierClassVisitor.main(new String[]{"target/test-classes/groovy/bugs/CustomMetaClassTest.class"}); }
private static String toASM(byte[] transformedBytes) { StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); ClassReader cr = new ClassReader(transformedBytes); TraceClassVisitor tcv = new TraceClassVisitor(null, new ASMifier(), pw); cr.accept(tcv, ClassReader.EXPAND_FRAMES); pw.close(); return sw.toString(); }
public String dumpASM(byte[] bytecode) { if (bytecode == null) { throw new NullPointerException("bytecode must not be null"); } return writeBytecode(bytecode, new ASMifier()); }
@Override public ASMifier visitMethod(final int access, final String name, final String desc, final String signature, final String[] exceptions) { buf.setLength(0); buf.append("{\n"); buf.append("mg = new TestGeneratorAdapter("); buf.append(access); buf.append(", "); buf.append(getMethod(name, desc)); buf.append(", "); if (signature == null) { buf.append("null"); } else { buf.append('"').append(signature).append('"'); } buf.append(", "); if (exceptions != null && exceptions.length > 0) { buf.append("new Type[] {"); for (int i = 0; i < exceptions.length; ++i) { buf.append(i == 0 ? " " : ", "); buf.append(getType(exceptions[i])); } buf.append(" }"); } else { buf.append("null"); } buf.append(", cw);\n"); text.add(buf.toString()); GASMifier av = new GASMifier(access, desc); text.add(av.getText()); text.add("}\n"); return av; }
@Override public Printer visitLocalVariableAnnotation(int typeRef, TypePath typePath, Label[] start, Label[] end, int[] index, String desc, boolean visible) { buf.setLength(0); buf.append("{\n").append("av0 = ").append(name) .append(".getNext().visitLocalVariableAnnotation("); buf.append(typeRef); buf.append(", TypePath.fromString(\"").append(typePath).append("\"), "); buf.append("new Label[] {"); for (int i = 0; i < start.length; ++i) { buf.append(i == 0 ? " " : ", "); appendLabel(start[i]); } buf.append(" }, new Label[] {"); for (int i = 0; i < end.length; ++i) { buf.append(i == 0 ? " " : ", "); appendLabel(end[i]); } buf.append(" }, new int[] {"); for (int i = 0; i < index.length; ++i) { String idx = index[i] < firstLocal ? new Integer(index[i]) .toString() : "local" + locals.get(new Integer(index[i])); buf.append(i == 0 ? " " : ", ").append(idx); } buf.append(" }, "); appendConstant(desc); buf.append(", ").append(visible).append(");\n"); text.add(buf.toString()); ASMifier a = createASMifier("av", 0); text.add(a.getText()); text.add("}\n"); return a; }
@Override public ASMifier visitMethod( final int access, final String name, final String desc, final String signature, final String[] exceptions) { buf.setLength(0); buf.append("{\n"); buf.append("mg = new GeneratorAdapter("); buf.append(access); buf.append(", "); buf.append(getMethod(name, desc)); buf.append(", "); if (signature == null) { buf.append("null"); } else { buf.append('"').append(signature).append('"'); } buf.append(", "); if (exceptions != null && exceptions.length > 0) { buf.append("new Type[] {"); for (int i = 0; i < exceptions.length; ++i) { buf.append(i == 0 ? " " : ", "); buf.append(getType(exceptions[i])); } buf.append(" }"); } else { buf.append("null"); } buf.append(", cw);\n"); text.add(buf.toString()); GASMifier av = new GASMifier(access, desc); text.add(av.getText()); text.add("}\n"); return av; }
public static void main(String[] args) throws Exception { ASMifier.main(new String[]{"com.devexperts.dlcheck.tests.base.Test$B"}); }
public static void main(String[] args) throws Exception { // Runnable r = new MyClassLoader().load().newInstance(); // r.run(); ASMifier.main(new String[]{Test.class.getCanonicalName()}); }
public static void main(String[] args) throws Exception { // "Hi.class" 对应的字节码通过ASM工具怎样一步一步生成出来呢? 通过调用ASMifier.main, 可以得到通过ASM工具生成bytes的详细源码. // 解释太绕了, 运行一下便知 ASMifier.main(new String[]{ "/mywork/myosgi/osgi_workspace/fastquery/target/test-classes/org/fastquery/bean/StudentDBServiceProxyImpl.class"} ); }
@Test public void generateASM() throws Exception { ASMifier.main( new String[]{ SomeMixin_Stubx.class.getName() } ); }
protected void printClass(final byte[] bs) { final ClassReader reader = new ClassReader(bs); reader.accept(new TraceClassVisitor(null, new ASMifier(), new PrintWriter( System.out)), ClassReader.EXPAND_FRAMES); }
protected void printMutant(final Mutant mutant) { final ClassReader reader = new ClassReader(mutant.getBytes()); reader.accept(new TraceClassVisitor(null, new ASMifier(), new PrintWriter( System.out)), ClassReader.EXPAND_FRAMES); }
@Override protected ASMifier createASMifier(final String name, final int id) { return new GASMifier(name, id); }
@Override public String toString() { Printer printer = new ASMifier(); TraceMethodVisitor visitor = new TraceMethodVisitor(printer); node.accept(visitor); return printer.getText().toString(); }
/** * @param args */ public static void main(String[] args) throws Exception { // TODO Auto-generated method stub ASMifier.main(new String[] { "org.beetl.performance.lab.asm.PrivateTestGet" }); }
/** * @param args */ public static void main(String[] args) throws Exception { // TODO Auto-generated method stub ASMifier.main(new String[] { "com.beetl.performance.lab.asm.field.UserIdFiledAccess" }); }