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

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

Introduction

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

Prototype

public void add(final InsnList insnList) 

Source Link

Document

Adds the given instructions to the end of this list.

Usage

From source file:org.brutusin.instrumentation.Instrumentator.java

License:Apache License

private InsnList getVoidReturnTraceInstructions() {
    InsnList il = new InsnList();
    addGetCallback(il);/*from   w  ww .j av a 2  s.  c  o  m*/
    il.add(new VarInsnNode(Opcodes.ALOAD, this.methodVarIndex));
    il.add(new VarInsnNode(Opcodes.ALOAD, this.executionIdIndex));
    il.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, "org/brutusin/instrumentation/Callback", "onVoidFinish",
            "(Ljava/lang/Object;Ljava/lang/String;)V", false));

    return il;
}

From source file:org.brutusin.instrumentation.Instrumentator.java

License:Apache License

private InsnList getThrowTraceInstructions() {
    InsnList il = new InsnList();

    int exceptionVariablePosition = getFistAvailablePosition();
    il.add(new VarInsnNode(Opcodes.ASTORE, exceptionVariablePosition));

    this.methodOffset++; // Actualizamos el offset
    addGetCallback(il);/*w  w  w.  j a va 2s .  c o m*/
    il.add(new VarInsnNode(Opcodes.ALOAD, this.methodVarIndex));
    il.add(new VarInsnNode(Opcodes.ALOAD, exceptionVariablePosition));
    il.add(new VarInsnNode(Opcodes.ALOAD, this.executionIdIndex));
    il.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, "org/brutusin/instrumentation/Callback",
            "onThrowableThrown", "(Ljava/lang/Object;Ljava/lang/Throwable;Ljava/lang/String;)V", false));

    il.add(new VarInsnNode(Opcodes.ALOAD, exceptionVariablePosition));

    return il;
}

From source file:org.brutusin.instrumentation.Instrumentator.java

License:Apache License

private InsnList getReturnTraceInstructions() {

    InsnList il = new InsnList();

    int retunedVariablePosition = getFistAvailablePosition();
    il.add(TreeInstructions.getStoreInst(this.methodReturnType, retunedVariablePosition));

    this.variableCreated(this.methodReturnType); // Actualizamos el offset
    addGetCallback(il);/*ww w . ja v  a2  s .  co m*/
    il.add(new VarInsnNode(Opcodes.ALOAD, this.methodVarIndex));
    il.add(TreeInstructions.getLoadInst(this.methodReturnType, retunedVariablePosition));
    MethodInsnNode mNode = TreeInstructions.getWrapperContructionInst(this.methodReturnType);
    if (mNode != null) {
        il.add(mNode);
    }
    il.add(new VarInsnNode(Opcodes.ALOAD, this.executionIdIndex));
    il.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, "org/brutusin/instrumentation/Callback", "onFinish",
            "(Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/String;)V", false));

    il.add(TreeInstructions.getLoadInst(this.methodReturnType, retunedVariablePosition));

    return il;

}

From source file:org.brutusin.instrumentation.utils.TreeInstructions.java

License:Apache License

public static InsnList getClassReferenceInstruction(Type type, int majorVersion) {

    InsnList list = new InsnList();
    char charType = type.getDescriptor().charAt(0);
    String wrapper;//from  ww w.j  av a 2s  .c om
    switch (charType) {
    case 'B':
        wrapper = "java/lang/Byte";
        break;
    case 'C':
        wrapper = "java/lang/Character";
        break;
    case 'D':
        wrapper = "java/lang/Double";
        break;
    case 'F':
        wrapper = "java/lang/Float";
        break;
    case 'I':
        wrapper = "java/lang/Integer";
        break;
    case 'J':
        wrapper = "java/lang/Long";
        break;
    case 'L':
    case '[':
        return getClassConstantReference(type, majorVersion);
    case 'Z':
        wrapper = "java/lang/Boolean";
        break;
    case 'S':
        wrapper = "java/lang/Short";
        break;
    default:
        throw new ClassFormatError("Invalid method signature: " + type.getDescriptor());
    }

    list.add(new FieldInsnNode(Opcodes.GETSTATIC, wrapper, "TYPE", "Ljava/lang/Class;"));
    return list;

}

From source file:org.brutusin.instrumentation.utils.TreeInstructions.java

License:Apache License

public static InsnList getClassConstantReference(Type type, int majorVersion) {
    InsnList il = new InsnList();

    if (majorVersion >= Opcodes.V1_5) {
        il.add(new LdcInsnNode(type));

    } else {//from   w  ww.j av  a 2  s.c  o  m
        String fullyQualifiedName = type.getInternalName().replaceAll("/", ".");
        il.add(new LdcInsnNode(fullyQualifiedName));
        il.add(new MethodInsnNode(Opcodes.INVOKESTATIC, "java/lang/Class", "forName",
                "(Ljava/lang/String;)Ljava/lang/Class;", false));
    }
    return il;
}

From source file:org.coldswap.asm.field.PrivateStaticFieldReplacer.java

License:Open Source License

/**
 * Removes any initializing reference of the field.
 *
 * @param classNode containing the old class.
 * @param fieldNode containing the old field.
 * @return the initializing list of instructions.
 *///from   ww w. j  av a  2  s.  c om
@SuppressWarnings("unchecked")
private InsnList cleanClInit(ClassNode classNode, FieldNode fieldNode) {
    List<MethodNode> methodNodes = classNode.methods;
    AbstractInsnNode firstInst = null;
    int counter = 0;
    for (MethodNode methodNode : methodNodes) {
        if (methodNode.name.equals("<clinit>")) {
            // search for PUTSTATIC
            InsnList insnList = methodNode.instructions;
            Iterator iterator1 = insnList.iterator();
            while (iterator1.hasNext()) {
                AbstractInsnNode ins2 = (AbstractInsnNode) iterator1.next();
                // if a initializing has been found, then copy everything from
                // the corresponding label to the PUTSTATIC
                if (ins2.getOpcode() == Opcodes.PUTSTATIC) {
                    final Boolean[] fieldFound = { false };
                    final FieldNode fNode = fieldNode;
                    ins2.accept(new MethodVisitor(Opcodes.ASM5) {
                        @Override
                        public void visitFieldInsn(int i, String s, String s2, String s3) {
                            if (s2.equals(fNode.name)) {
                                fieldFound[0] = true;
                            }
                            super.visitFieldInsn(i, s, s2, s3);
                        }
                    });
                    if (fieldFound[0]) {
                        // find the first PUTSTATIC before this one.
                        boolean staticFound = false;
                        while (!staticFound) {
                            AbstractInsnNode tmpInst = ins2.getPrevious();
                            if (tmpInst != null) {
                                if (tmpInst.getOpcode() != Opcodes.F_NEW) {
                                    if (tmpInst.getOpcode() == Opcodes.PUTSTATIC) {
                                        staticFound = true;
                                    } else {
                                        firstInst = tmpInst;
                                        counter++;
                                    }
                                }
                            } else {
                                staticFound = true;
                            }
                            ins2 = tmpInst;
                        }

                        break;
                    }
                }
            }

            if (firstInst != null) {
                InsnList iList = new InsnList();
                iList.add(firstInst.clone(null));
                counter--;
                while (counter > 0) {
                    AbstractInsnNode ain = firstInst.getNext();
                    iList.add(ain.clone(null));
                    counter--;
                    insnList.remove(firstInst);
                    firstInst = ain;
                }
                // remove last instruction and the putstatic instruction
                AbstractInsnNode putStatic = firstInst.getNext();
                insnList.remove(firstInst);
                insnList.remove(putStatic);
                return iList;
            }
        }
    }
    return null;
}

From source file:org.coldswap.asm.field.ProtectedStaticFieldReplacer.java

License:Open Source License

/**
 * Removes any initializing reference of the field.
 *
 * @param classNode containing the old class.
 * @param fieldNode containing the old field.
 * @param canRemove <code>true</code> if this method should remove the initializing code and return it
 *                  or <code>false</code> if you only want to return the init code.
 * @return the initializing list of instructions.
 *//*from w w  w.jav a2  s .c  o  m*/
@SuppressWarnings("unchecked")
private InsnList cleanClInit(ClassNode classNode, FieldNode fieldNode, boolean canRemove) {
    List<MethodNode> methodNodes = classNode.methods;
    AbstractInsnNode firstInst = null;
    int counter = 0;
    for (MethodNode methodNode : methodNodes) {
        if (methodNode.name.equals("<clinit>")) {
            // search for PUTSTATIC
            InsnList insnList = methodNode.instructions;
            Iterator iterator1 = insnList.iterator();
            while (iterator1.hasNext()) {
                AbstractInsnNode ins2 = (AbstractInsnNode) iterator1.next();
                // if a initializing has been found, then copy everything from
                // the coresponding label to the PUTSTATIC
                if (ins2.getOpcode() == Opcodes.PUTSTATIC) {
                    final Boolean[] fieldFound = { false };
                    final FieldNode fNode = fieldNode;
                    ins2.accept(new MethodVisitor(Opcodes.ASM5) {
                        @Override
                        public void visitFieldInsn(int i, String s, String s2, String s3) {
                            if (s2.equals(fNode.name)) {
                                fieldFound[0] = true;
                            }
                            super.visitFieldInsn(i, s, s2, s3);
                        }
                    });
                    if (fieldFound[0]) {
                        // find the first PUTSTATIC before this one.
                        boolean staticFound = false;
                        while (!staticFound) {
                            AbstractInsnNode tmpInst = ins2.getPrevious();
                            if (tmpInst != null) {
                                if (tmpInst.getOpcode() != Opcodes.F_NEW) {
                                    if (tmpInst.getOpcode() == Opcodes.PUTSTATIC) {
                                        staticFound = true;
                                    } else {
                                        firstInst = tmpInst;
                                        counter++;
                                    }
                                }
                            } else {
                                staticFound = true;
                            }
                            ins2 = tmpInst;
                        }

                        break;
                    }
                }
            }

            if (firstInst != null) {
                InsnList iList = new InsnList();
                iList.add(firstInst.clone(null));
                counter--;
                while (counter > 0) {
                    AbstractInsnNode ain = firstInst.getNext();
                    iList.add(ain.clone(null));
                    counter--;
                    if (canRemove) {
                        insnList.remove(firstInst);
                    }
                    firstInst = ain;
                }
                if (canRemove) {
                    // remove last instruction and the putstatic instruction
                    AbstractInsnNode putStatic = firstInst.getNext();
                    insnList.remove(firstInst);
                    insnList.remove(putStatic);
                }
                return iList;
            }
        }
    }
    return null;
}

From source file:org.coldswap.asm.method.PublicFloatMethodReplacer.java

License:Open Source License

private InsnList replaceReturn(InsnList insnList, Type retType) {
    final Type rretType = retType;
    int retOpcode = MethodUtil.getRetOpcodeToReplace(retType);
    for (int i = 0; i < insnList.size(); i++) {
        AbstractInsnNode absIns = insnList.get(i);
        int opcode = absIns.getOpcode();
        if (opcode == retOpcode) {
            // if tries to return a Reference type into a primitive then
            // remove the unbox( we return an Object). If a primitive is returned
            // into a  primitive then we must try to box from primitive to Object/Integer, etc..

            // check if an unbox takes place before return
            final boolean[] isBoxUnbox = { false, false };
            AbstractInsnNode valueOf = null;
            AbstractInsnNode primitiveValue = null;
            if (i > 1) {
                valueOf = insnList.get(i - 1);
                primitiveValue = insnList.get(i - 2);
                if (valueOf.getOpcode() == Opcodes.INVOKESTATIC) {
                    valueOf.accept(new MethodVisitor(Opcodes.ASM5) {
                        @Override
                        public void visitMethodInsn(int i, String s, String s2, String s3) {
                            if (AutoBoxing.isPrimitive(rretType.getDescriptor())) {
                                if ((AutoBoxing.getBoxClassName(rretType) + ".valueOf").equals(s + s2)) {
                                    isBoxUnbox[0] = true;
                                }/* w ww  . jav  a  2s . co  m*/
                            }
                            super.visitMethodInsn(i, s, s2, s3);
                        }
                    });
                }

                if (isBoxUnbox[0] && primitiveValue.getOpcode() == Opcodes.INVOKEVIRTUAL) {
                    primitiveValue.accept(new MethodVisitor(Opcodes.ASM5) {
                        @Override
                        public void visitMethodInsn(int i, String s, String s2, String s3) {
                            if ((s + s2).equals(AutoBoxing.getUnBoxInvoke(rretType))) {
                                isBoxUnbox[1] = true;
                            }
                            super.visitMethodInsn(i, s, s2, s3);
                        }
                    });
                }
            }

            if (isBoxUnbox[0] && isBoxUnbox[1]) {
                // remove indexes
                insnList.remove(valueOf);
                insnList.remove(primitiveValue);
            } else {
                InsnList iList = new InsnList();
                iList.add(AutoBoxing.box(retType));
                iList.add(new InsnNode(Opcodes.ARETURN));
                insnList.insertBefore(absIns, iList);
                insnList.remove(absIns);

            }
        }
    }
    return insnList;
}

From source file:org.coldswap.asm.method.PublicObjectMethodReplacer.java

License:Open Source License

private InsnList replaceReturn(InsnList insnList, Type retType) {
    final Type rretType = retType;
    int retOpcode = MethodUtil.getRetOpcodeToReplace(retType);
    for (int i = 0; i < insnList.size(); i++) {
        AbstractInsnNode absIns = insnList.get(i);
        int opcode = absIns.getOpcode();
        if (opcode == retOpcode) {
            // if tries to return a Reference type into a primitive then
            // remove the unbox( we return an Object). If a primitive is returned
            // into a  primitive then we must try to box from primitive to Object/Integer, etc..

            // check if an unbox takes place before return
            final boolean[] isBoxUnbox = { false, false };
            AbstractInsnNode valueOf = null;
            AbstractInsnNode primitiveValue = null;
            if (i > 1) {
                valueOf = insnList.get(i - 1);
                primitiveValue = insnList.get(i - 2);
                if (valueOf.getOpcode() == Opcodes.INVOKESTATIC) {
                    valueOf.accept(new MethodVisitor(Opcodes.ASM5) {
                        @Override
                        public void visitMethodInsn(int i, String s, String s2, String s3) {
                            if (AutoBoxing.isPrimitive(rretType.getDescriptor())) {
                                if ((AutoBoxing.getBoxClassName(rretType) + ".valueOf").equals(s + s2)) {
                                    isBoxUnbox[0] = true;
                                }/*from  w  w  w.  ja va2  s. co m*/
                            }
                            super.visitMethodInsn(i, s, s2, s3);
                        }
                    });
                }

                if (isBoxUnbox[0] && primitiveValue.getOpcode() == Opcodes.INVOKEVIRTUAL) {
                    primitiveValue.accept(new MethodVisitor(Opcodes.ASM5) {
                        @Override
                        public void visitMethodInsn(int i, String s, String s2, String s3) {
                            if ((s + s2).equals(AutoBoxing.getUnBoxInvoke(rretType))) {
                                isBoxUnbox[1] = true;
                            }
                            super.visitMethodInsn(i, s, s2, s3);
                        }
                    });
                }
            }

            if (isBoxUnbox[0] && isBoxUnbox[1]) {
                // remove indexes
                insnList.remove(valueOf);
                insnList.remove(primitiveValue);
            } else {
                InsnList iList = new InsnList();
                iList.add(AutoBoxing.box(retType));
                iList.add(new InsnNode(Opcodes.ARETURN));
                insnList.insertBefore(absIns, iList);
                insnList.remove(absIns);
            }
        }
    }
    return insnList;
}

From source file:org.coldswap.transformer.ClInitTransformer.java

License:Open Source License

@SuppressWarnings("unchecked")
@Override//  w  w w  .j av  a2s.c o  m
public byte[] transform(ClassLoader classLoader, String s, Class<?> aClass, ProtectionDomain protectionDomain,
        byte[] bytes) throws IllegalClassFormatException {
    if (s != null && !"".equals(s)) {
        for (String pack : ClassUtil.skipTransforming) {
            if (s.startsWith(pack)) {
                return bytes;
            }
        }

        ClassNode cn = new ClassNode(Opcodes.ASM5);
        ClassReader cr = new ClassReader(bytes);
        ClassWriter cw = new ClassWriter(cr, ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS);
        // create adapter for method insertion.
        cr.accept(cn, 0);
        // insert <clinit>V if it is not inserted
        List methods = cn.methods;
        boolean clInitFound = false;
        for (MethodNode methodNode : (List<MethodNode>) methods) {
            if ("<clinit>".equals(methodNode.name)) {
                clInitFound = true;
            }
        }

        if (!clInitFound) {
            MethodNode mn = new MethodNode(Opcodes.ACC_STATIC, "<clinit>", "()V", null, null);
            InsnList insnList = mn.instructions;
            insnList.add(new LabelNode());
            insnList.add(new InsnNode(Opcodes.RETURN));
            cn.methods.add(mn);
        }

        cn.accept(cw);
        byte[] toRet = cw.toByteArray();
        if (toRet != null) {
            logger.info("Successful transformation!");
            return toRet;
        } else {
            logger.severe("Could not transform class");
            return bytes;
        }
    }
    return bytes;
}