Java Code Examples for org.objectweb.asm.tree.AbstractInsnNode#accept()

The following examples show how to use org.objectweb.asm.tree.AbstractInsnNode#accept() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MethodWeaver.java    From Concurnas with MIT License 6 votes vote down vote up
private void invokeDynamicOnLambda(MethodVisitor mv, AbstractInsnNode ain) {
    InvokeDynamicInsnNode indy = (InvokeDynamicInsnNode)ain;
    Object[]bsmArgs = indy.bsmArgs;
    // Is it a lambda conversion
    if (indy.bsm.getOwner().equals("java/lang/invoke/LambdaMetafactory")) {
        Handle lambdaBody = (Handle)bsmArgs[1];
        Detector detector = this.methodFlow.detector();
        String desc = lambdaBody.getDesc();
        //if (detector.isPausable(lambdaBody.getOwner(), lambdaBody.getName(), desc)) {
        //if(!lambdaBody.getName().equals("<init>")) {
        	bsmArgs[0] = addFiberType((org.objectweb.asm.Type)bsmArgs[0]);
            bsmArgs[1] = new Handle(lambdaBody.getTag(), 
                                    lambdaBody.getOwner(), 
                                    lambdaBody.getName(), 
                                    desc.replace(")", D_FIBER + ")"));
            bsmArgs[2] = addFiberType((org.objectweb.asm.Type)bsmArgs[2]);
        //}
            
        //}
    }
    ain.accept(mv);
}
 
Example 2
Source File: InsnListUtils.java    From maple-ir with GNU General Public License v3.0 5 votes vote down vote up
public static String insnToString(AbstractInsnNode insn) {
	insn.accept(mp);
	StringWriter sw = new StringWriter();
	printer.print(new PrintWriter(sw));
	printer.getText().clear();
	return sw.toString();
}
 
Example 3
Source File: MergeClassVisitor.java    From EasyRouter with Apache License 2.0 5 votes vote down vote up
private void insertMethod(MethodNode methodNode, MethodVisitor mv){
    AbstractInsnNode insnNode = methodNode.instructions.getFirst();
    while (insnNode!=null && insnNode.getOpcode() != Opcodes.RETURN){
        insnNode.accept(mv);
        insnNode = insnNode.getNext();
    }
}
 
Example 4
Source File: JClassPatcher.java    From rscplus with GNU General Public License v3.0 5 votes vote down vote up
private String decodeInstruction(AbstractInsnNode insnNode) {
  insnNode.accept(mp);
  StringWriter sw = new StringWriter();
  printer.print(new PrintWriter(sw));
  printer.getText().clear();
  return sw.toString();
}
 
Example 5
Source File: BytecodeEmitterContext.java    From Despector with MIT License 4 votes vote down vote up
@SuppressWarnings("unchecked")
public <T extends TypeEntry> void emitOuterType(T ast) {
    ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES);
    this.cw = writer;
    if (VERIFY_EMITTED_BYTECODE) {
        this.cw = new CheckClassAdapter(this.cw);
    }
    AstEmitter<AbstractEmitterContext, T> emitter = (AstEmitter<AbstractEmitterContext, T>) this.set.getAstEmitter(ast.getClass());
    if (emitter == null) {
        throw new IllegalArgumentException("No emitter for ast entry " + ast.getClass().getName());
    }
    emitter.emit(this, ast);
    this.cw.visitEnd();
    byte[] clazz = writer.toByteArray();
    if (DUMP_INSTRUCTIONS_AFTER_WRITE) {
        ClassReader cr = new ClassReader(clazz);
        ClassNode cn = new ClassNode();
        cr.accept(cn, 0);
        List<MethodNode> methods = cn.methods;
        for (MethodNode mn : methods) {
            System.out.println("Method: " + mn.name + mn.desc);
            Printer printer = new Textifier();
            TraceMethodVisitor mp = new TraceMethodVisitor(printer);
            for (Iterator<AbstractInsnNode> it = mn.instructions.iterator(); it.hasNext();) {
                AbstractInsnNode insn = it.next();
                insn.accept(mp);
            }
            StringWriter sw = new StringWriter();
            printer.print(new PrintWriter(sw));
            String s = sw.toString();
            if (s.endsWith("\n")) {
                s = s.substring(0, s.length() - 1);
            }
            System.out.println(s);
            mn.instructions.accept(mp);
        }
    }
    try {
        this.out.write(clazz);
    } catch (IOException e) {
        Throwables.propagate(e);
    }
}
 
Example 6
Source File: Instrumenter.java    From coroutines with GNU Lesser General Public License v3.0 4 votes vote down vote up
private void verifyClassIntegrity(ClassNode classNode) {
    // Do not COMPUTE_FRAMES. If you COMPUTE_FRAMES and you pop too many items off the stack or do other weird things that mess up the
    // stack map frames, it'll crash on classNode.accept(cw).
    ClassWriter cw = new SimpleClassWriter(ClassWriter.COMPUTE_MAXS/* | ClassWriter.COMPUTE_FRAMES*/, classRepo);
    classNode.accept(cw);
    
    byte[] classData = cw.toByteArray();

    ClassReader cr = new ClassReader(classData);
    classNode = new SimpleClassNode();
    cr.accept(classNode, 0);

    for (MethodNode methodNode : classNode.methods) {
        Analyzer<BasicValue> analyzer = new Analyzer<>(new SimpleVerifier(classRepo));
        try {
            analyzer.analyze(classNode.name, methodNode);
        } catch (AnalyzerException e) {
            // IF WE DID OUR INSTRUMENTATION RIGHT, WE SHOULD NEVER GET AN EXCEPTION HERE!!!!
            StringWriter writer = new StringWriter();
            PrintWriter printWriter = new PrintWriter(writer);
            
            printWriter.append(methodNode.name + " encountered " + e);
            
            Printer printer = new Textifier();
            TraceMethodVisitor traceMethodVisitor = new TraceMethodVisitor(printer);
            
            AbstractInsnNode insn = methodNode.instructions.getFirst();
            while (insn != null) {
                if (insn == e.node) {
                    printer.getText().add("----------------- BAD INSTRUCTION HERE -----------------\n");
                }
                insn.accept(traceMethodVisitor);
                insn = insn.getNext();
            }
            printer.print(printWriter);
            printWriter.flush(); // we need this or we'll get incomplete results
            
            throw new IllegalStateException(writer.toString(), e);
        }
    }
}