Example usage for org.objectweb.asm.tree InsnList get

List of usage examples for org.objectweb.asm.tree InsnList get

Introduction

In this page you can find the example usage for org.objectweb.asm.tree InsnList get.

Prototype

public AbstractInsnNode get(final int index) 

Source Link

Document

Returns the instruction whose index is given.

Usage

From source file:edu.mit.streamjit.util.bytecode.MethodUnresolver.java

License:Open Source License

/**
 * Removes "xLOAD N xSTORE N"./*from w  w w  .java2  s.  co m*/
 * @return true iff changes were made
 */
private boolean removeLoadStore() {
    InsnList insns = methodNode.instructions;
    for (int i = 0; i < insns.size() - 1; ++i) {
        AbstractInsnNode first = insns.get(i);
        int index = LOADS.indexOf(first.getOpcode());
        if (index == -1)
            continue;
        AbstractInsnNode second = insns.get(i + 1);
        if (second.getOpcode() != STORES.get(index))
            continue;
        if (((VarInsnNode) first).var != ((VarInsnNode) second).var)
            continue;
        insns.remove(first);
        insns.remove(second);
        return true;
    }
    return false;
}

From source file:edu.mit.streamjit.util.bytecode.MethodUnresolver.java

License:Open Source License

/**
 * Removes goto instructions that go to the label immediately following
 * them./*from  w  ww .  j  av a2 s.  c om*/
 * @return true iff changes were made
 */
private boolean removeUnnecessaryGotos() {
    InsnList insns = methodNode.instructions;
    for (int i = 0; i < insns.size() - 1; ++i) {
        AbstractInsnNode first = insns.get(i);
        if (first.getOpcode() != Opcodes.GOTO)
            continue;
        AbstractInsnNode second = insns.get(i + 1);
        if (!(second instanceof LabelNode))
            continue;
        if (((JumpInsnNode) first).label != second)
            continue;
        insns.remove(first);
        return true;
    }
    return false;
}

From source file:gemlite.core.internal.admin.measurement.CheckPointService.java

License:Apache License

private void analyzeMethod(CheckPointContext ctx, ClassNode cn, MethodNode mn, ScannedMethodItem caller) {
    // ???/*from w w w. ja va2  s .c  o  m*/
    Set<ScannedMethodItem> methods = new HashSet<ScannedMethodItem>();
    GemliteSibingsLoader loader = ctx.loader;
    InsnList insnList = mn.instructions;
    for (int j = 0; j < insnList.size(); j++) {
        AbstractInsnNode insn = insnList.get(j);

        if (insn.getType() == AbstractInsnNode.METHOD_INSN) {
            MethodInsnNode methodInsn = (MethodInsnNode) insn;
            if (CONSTRUCT_NAME.equals(methodInsn.name))
                continue;
            URL url = loader.findResource(methodInsn.owner + ".class");
            boolean blCurrentModule = url != null;
            if (blCurrentModule) {
                ScannedMethodItem item = new ScannedMethodItem(methodInsn.owner, methodInsn.name,
                        methodInsn.desc);
                LogUtil.getCoreLog().trace("I={} owner={} name={} op={}", j, methodInsn.owner, methodInsn.name,
                        methodInsn.getOpcode());

                // ???
                if (methods.contains(item))
                    continue;

                caller.children.add(item);

                // methods
                methods.add(item);

                if (!methodInsn.owner.equals(cn.name))
                    analazyClass(ctx, item.className, item.methodName, item);
                else
                    analazyClass(ctx, cn, item.methodName, item);
            }
        }
    }
}

From source file:gemlite.core.internal.measurement.MeasureHelper.java

License:Apache License

public final static void instrumentCheckPoint(String className, MethodNode mn) {
    if (LogUtil.getCoreLog().isTraceEnabled())
        LogUtil.getCoreLog().trace("Found check point, class:" + className + " method:" + mn.name);

    InsnList insn = mn.instructions;
    List<AbstractInsnNode> returnIndex = new ArrayList<>();
    // return/*  w  w w. ja  v  a 2  s  . c  om*/
    int localVarCount = mn.localVariables.size();
    for (int i = 0; i < insn.size(); i++) {
        AbstractInsnNode insnNode = (AbstractInsnNode) insn.get(i);
        switch (insnNode.getOpcode()) {
        case Opcodes.ARETURN:
        case Opcodes.IRETURN:
        case Opcodes.DRETURN:
        case Opcodes.LRETURN:
        case Opcodes.FRETURN:
            returnIndex.add(insnNode.getPrevious());
            break;
        case Opcodes.RETURN:
            returnIndex.add(insnNode);
            break;
        }
    }
    // 
    insn.insert(new VarInsnNode(Opcodes.LSTORE, localVarCount + 2));
    insn.insert(
            new MethodInsnNode(Opcodes.INVOKESTATIC, "java/lang/System", "currentTimeMillis", "()J", false));
    // ?
    for (AbstractInsnNode insnNode : returnIndex) {
        insn.insertBefore(insnNode, new MethodInsnNode(Opcodes.INVOKESTATIC, "java/lang/System",
                "currentTimeMillis", "()J", false));
        insn.insertBefore(insnNode, new VarInsnNode(Opcodes.LSTORE, localVarCount + 4));

        insn.insertBefore(insnNode, new LdcInsnNode(className));
        insn.insertBefore(insnNode, new LdcInsnNode(mn.name));
        insn.insertBefore(insnNode, new VarInsnNode(Opcodes.LLOAD, localVarCount + 2));
        insn.insertBefore(insnNode, new VarInsnNode(Opcodes.LLOAD, localVarCount + 4));
        insn.insertBefore(insnNode,
                new MethodInsnNode(Opcodes.INVOKESTATIC, "gemlite/core/internal/measurement/MeasureHelper",
                        "recordCheckPoint", "(Ljava/lang/String;Ljava/lang/String;JJ)V", false));
    }
}

From source file:ht.misc.injectsocks.InjectSockstTransformerImpl.java

License:Apache License

public byte[] inject(byte[] classfileBuffer) {
    try {//from w ww  .  ja v  a2 s.  co m
        ClassReader cr = new ClassReader(classfileBuffer);
        ClassNode cn = new ClassNode();
        cr.accept(cn, 0);

        ArrayList<AbstractInsnNode> injectPos = new ArrayList<AbstractInsnNode>();

        @SuppressWarnings("unchecked")
        List<MethodNode> methods = (List<MethodNode>) cn.methods;
        for (int i = 0; i < methods.size(); ++i) {
            MethodNode method = methods.get(i);
            InsnList instructions = method.instructions;
            if (instructions.size() <= 0)
                continue;

            //System.out.println("Method: "+method.name+" ");
            for (int j = 0; j < instructions.size(); ++j) {
                AbstractInsnNode insn = (AbstractInsnNode) instructions.get(j);
                //System.out.println("\tInsn: opc="+OpcodeUtil.getOpcode(insn.getOpcode())+", type="+insn.getType());
                if (insn.getType() == AbstractInsnNode.METHOD_INSN) {
                    MethodInsnNode min = (MethodInsnNode) insn;
                    //System.out.printf("\t\towner=%s, name=%s, desc=%s\n", min.owner, min.name, min.desc);

                    if (min.owner.equals("java/net/Socket") && min.name.equals("<init>")
                            && min.desc.equals("()V")) {
                        min.desc = "(Ljava/net/Proxy;)V";
                        injectPos.add(min);
                    }
                }
            }

            for (int k = 0; k < injectPos.size(); k++) {
                AbstractInsnNode pos = injectPos.get(k);
                MethodInsnNode newMin = new MethodInsnNode(Opcodes.INVOKESTATIC,
                        "ht/misc/injectsocks/ProxyManager", "getProxy", "()Ljava/net/Proxy;", false);
                instructions.insertBefore(pos, newMin);
            }

        }

        ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
        cn.accept(cw);
        byte[] injectedClassfileBuffer = cw.toByteArray();
        System.out.printf("INFO: classfileBuffer.legnth=%d, injectedClassfileBuffer.length=%d\n",
                classfileBuffer.length, injectedClassfileBuffer.length);

        return injectedClassfileBuffer;
    } catch (Throwable e) {
        e.printStackTrace();
        return classfileBuffer;
    }
}

From source file:ht.misc.injectsocks.InjectSockstTransformerImpl.java

License:Apache License

public void printClassByteCode(byte code[]) {
    ClassReader cr = new ClassReader(code);
    ClassNode cn = new ClassNode();
    cr.accept(cn, 0);/*from   www.j  ava2 s  .c  o  m*/

    @SuppressWarnings("unchecked")
    List<MethodNode> methods = (List<MethodNode>) cn.methods;
    for (int i = 0; i < methods.size(); ++i) {
        MethodNode method = methods.get(i);
        InsnList instructions = method.instructions;
        if (instructions.size() <= 0)
            continue;

        System.out.println("Method: " + method.name + " ");
        for (int j = 0; j < instructions.size(); ++j) {
            AbstractInsnNode insn = (AbstractInsnNode) instructions.get(j);
            System.out.println(
                    "\tInsn: opc=" + OpcodeUtil.getOpcode(insn.getOpcode()) + ", type=" + insn.getType());
            if (insn.getType() == AbstractInsnNode.METHOD_INSN) {
                MethodInsnNode min = (MethodInsnNode) insn;
                System.out.printf("\t\towner=%s, name=%s, desc=%s\n", min.owner, min.name, min.desc);
            }
        }
    }
}

From source file:ivorius.ivtoolkit.asm.IvNodeFinder.java

License:Apache License

public static AbstractInsnNode findNode(IvSingleNodeMatcher matcher, InsnList insnList) {
    for (int i = 0; i < insnList.size(); i++) {
        AbstractInsnNode node = insnList.get(i);

        if (matcher.matchNode(node)) {
            return node;
        }//from  w  w  w. j av a 2 s  .com
    }

    return null;
}

From source file:ivorius.ivtoolkit.asm.IvNodeFinder.java

License:Apache License

public static AbstractInsnNode findNodeList(IvMultiNodeMatcher matcher, InsnList insnList) {
    for (int i = 0; i < insnList.size(); i++) {
        AbstractInsnNode node = insnList.get(i);

        if (matcher.matchFromNodeInList(insnList, node)) {
            return node;
        }//w  w  w  .j ava 2 s .  com
    }

    return null;
}

From source file:ivorius.ivtoolkit.asm.IvNodeFinder.java

License:Apache License

public static List<AbstractInsnNode> findNodes(IvSingleNodeMatcher matcher, InsnList insnList) {
    List<AbstractInsnNode> nodes = new ArrayList<AbstractInsnNode>();

    for (int i = 0; i < insnList.size(); i++) {
        AbstractInsnNode node = insnList.get(i);

        if (matcher.matchNode(node)) {
            nodes.add(node);// www  . jav a  2  s  . c  om
        }
    }

    return nodes;
}

From source file:ivorius.ivtoolkit.asm.IvNodeFinder.java

License:Apache License

public static List<AbstractInsnNode> findNodeLists(IvMultiNodeMatcher matcher, InsnList insnList) {
    List<AbstractInsnNode> nodes = new ArrayList<AbstractInsnNode>();

    for (int i = 0; i < insnList.size(); i++) {
        AbstractInsnNode node = insnList.get(i);

        if (matcher.matchFromNodeInList(insnList, node)) {
            nodes.add(node);/*  w ww .j av a  2  s.  c om*/
        }
    }

    return nodes;
}