Example usage for org.objectweb.asm.tree AbstractInsnNode getOpcode

List of usage examples for org.objectweb.asm.tree AbstractInsnNode getOpcode

Introduction

In this page you can find the example usage for org.objectweb.asm.tree AbstractInsnNode getOpcode.

Prototype

public int getOpcode() 

Source Link

Document

Returns the opcode of this instruction.

Usage

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

License:Open Source License

@SuppressWarnings("uncheked")
@Override/*from www. j  a va 2s.  co  m*/
public int findAndReplace(ClassNode classNode) {
    int counter = 0;
    if (classNode.superName.equals(supperClass)) {
        List<MethodNode> methodNodes = classNode.methods;
        for (MethodNode method : methodNodes) {
            InsnList inst = method.instructions;
            Iterator iter = inst.iterator();
            while (iter.hasNext()) {
                AbstractInsnNode absIns = (AbstractInsnNode) iter.next();
                int opcode = absIns.getOpcode();
                // check if instruction is GETSTATIC or PUTSTATIC
                if (opcode == Opcodes.GETSTATIC) {
                    // get type
                    if (absIns.getType() == AbstractInsnNode.FIELD_INSN) {
                        final Boolean[] foundField = { false };
                        absIns.accept(new MethodVisitor(Opcodes.ASM5) {
                            @Override
                            public void visitFieldInsn(int i, String s, String s2, String s3) {
                                if (oldClass.equals(s) && fieldToReplace.name.equals(s2)) {
                                    foundField[0] = true;
                                }
                                super.visitFieldInsn(i, s, s2, s3);
                            }
                        });
                        if (foundField[0]) {
                            inst.set(absIns, new FieldInsnNode(Opcodes.GETSTATIC, newClass, fieldToReplace.name,
                                    fieldToReplace.desc));
                            counter++;
                        }
                    }
                } else if (opcode == Opcodes.PUTSTATIC) {
                    if (absIns.getType() == AbstractInsnNode.FIELD_INSN) {
                        final Boolean[] foundField = { false };
                        absIns.accept(new MethodVisitor(Opcodes.ASM5) {
                            @Override
                            public void visitFieldInsn(int i, String s, String s2, String s3) {
                                if (oldClass.equals(s) && fieldToReplace.name.equals(s2)) {
                                    foundField[0] = true;
                                }
                                super.visitFieldInsn(i, s, s2, s3);
                            }
                        });
                        if (foundField[0]) {
                            inst.set(absIns, new FieldInsnNode(Opcodes.PUTSTATIC, newClass, fieldToReplace.name,
                                    fieldToReplace.desc));
                            counter++;
                        }
                    }
                }
            }
        }
    }
    return counter;
}

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  .j  a v a2 s. com
@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.field.ProtectedStaticFieldReplacer.java

License:Open Source License

/**
 * Replaces any GETSTATIC/PUTSTATIC call of the field in the old class with the field
 * introduced in the new class.//from ww  w .  j  a  v a  2s  . c o m
 *
 * @param classNode containing the old class.
 * @param fieldNode containing the old field.
 */
private void replaceReferences(ClassNode classNode, FieldNode fieldNode) {
    List<MethodNode> methodNodes = classNode.methods;
    String contClass = classNode.name.substring(classNode.name.lastIndexOf("/") + 1);
    final String className = classPackage
            + TransformerNameGenerator.getProtectedStaticFieldClassName(contClass, fieldNode.name);
    for (MethodNode method : methodNodes) {
        InsnList inst = method.instructions;
        Iterator iter = inst.iterator();
        while (iter.hasNext()) {
            AbstractInsnNode absIns = (AbstractInsnNode) iter.next();
            int opcode = absIns.getOpcode();
            // check if instruction is GETSTATIC or PUTSTATIC
            if (opcode == Opcodes.GETSTATIC) {
                // get type
                if (absIns.getType() == AbstractInsnNode.FIELD_INSN) {
                    final Boolean[] foundField = { false };
                    final ClassNode cNode = classNode;
                    final FieldNode fNode = fieldNode;
                    absIns.accept(new MethodVisitor(Opcodes.ASM5) {
                        @Override
                        public void visitFieldInsn(int i, String s, String s2, String s3) {
                            if (cNode.name.equals(s) && fNode.name.equals(s2)) {
                                foundField[0] = true;
                            }
                            super.visitFieldInsn(i, s, s2, s3);
                        }
                    });
                    if (foundField[0]) {
                        inst.set(absIns, new FieldInsnNode(Opcodes.GETSTATIC, className, fieldNode.name,
                                fieldNode.desc));
                    }
                }
            } else if (opcode == Opcodes.PUTSTATIC) {
                if (absIns.getType() == AbstractInsnNode.FIELD_INSN) {
                    final Boolean[] foundField = { false };
                    final ClassNode cNode = classNode;
                    final FieldNode fNode = fieldNode;
                    absIns.accept(new MethodVisitor(Opcodes.ASM5) {
                        @Override
                        public void visitFieldInsn(int i, String s, String s2, String s3) {
                            if (cNode.name.equals(s) && fNode.name.equals(s2)) {
                                foundField[0] = true;
                            }
                            super.visitFieldInsn(i, s, s2, s3);
                        }
                    });
                    if (foundField[0]) {
                        inst.set(absIns, new FieldInsnNode(Opcodes.PUTSTATIC, className, fieldNode.name,
                                fieldNode.desc));
                    }
                }
            }
        }
    }
}

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

License:Open Source License

@SuppressWarnings("unchecked")
@Override//from w  w w. ja  v  a 2s . c  o m
public int findAndReplace(ClassNode classNode) {
    int counter = 0;
    List<MethodNode> methodNodes = classNode.methods;
    for (MethodNode method : methodNodes) {
        InsnList inst = method.instructions;
        Iterator iter = inst.iterator();
        while (iter.hasNext()) {
            AbstractInsnNode absIns = (AbstractInsnNode) iter.next();
            int opcode = absIns.getOpcode();
            // check if instruction is GETSTATIC or PUTSTATIC
            if (opcode == Opcodes.GETSTATIC) {
                // get type
                if (absIns.getType() == AbstractInsnNode.FIELD_INSN) {
                    final Boolean[] foundField = { false };
                    absIns.accept(new MethodVisitor(Opcodes.ASM5) {
                        @Override
                        public void visitFieldInsn(int i, String s, String s2, String s3) {
                            if (oldClass.equals(s) && fieldToReplace.name.equals(s2)) {
                                foundField[0] = true;
                            }
                            super.visitFieldInsn(i, s, s2, s3);
                        }
                    });
                    if (foundField[0]) {
                        inst.set(absIns, new FieldInsnNode(Opcodes.GETSTATIC, newClass, fieldToReplace.name,
                                fieldToReplace.desc));
                        counter++;
                    }
                }
            } else if (opcode == Opcodes.PUTSTATIC) {
                if (absIns.getType() == AbstractInsnNode.FIELD_INSN) {
                    final Boolean[] foundField = { false };
                    absIns.accept(new MethodVisitor(Opcodes.ASM5) {
                        @Override
                        public void visitFieldInsn(int i, String s, String s2, String s3) {
                            if (oldClass.equals(s) && fieldToReplace.name.equals(s2)) {
                                foundField[0] = true;
                            }
                            super.visitFieldInsn(i, s, s2, s3);
                        }
                    });
                    if (foundField[0]) {
                        inst.set(absIns, new FieldInsnNode(Opcodes.PUTSTATIC, newClass, fieldToReplace.name,
                                fieldToReplace.desc));
                        counter++;
                    }
                }
            }
        }
    }
    return counter;
}

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

License:Open Source License

/**
 * Replaces any GETSTATIC/PUTSTATIC call of the field in the old class with the field
 * introduced in the new class.//w  w  w .ja  v a2s .  c  o m
 *
 * @param classNode containing the old class.
 * @param fieldNode containing the old field.
 */
private void replaceReferences(ClassNode classNode, FieldNode fieldNode) {
    List<MethodNode> methodNodes = classNode.methods;
    String contClass = classNode.name.substring(classNode.name.lastIndexOf("/") + 1);
    final String className = classPackage
            + TransformerNameGenerator.getPublicStaticFieldClassName(contClass, fieldNode.name);
    for (MethodNode method : methodNodes) {
        InsnList inst = method.instructions;
        Iterator iter = inst.iterator();
        while (iter.hasNext()) {
            AbstractInsnNode absIns = (AbstractInsnNode) iter.next();
            int opcode = absIns.getOpcode();
            // check if instruction is GETSTATIC or PUTSTATIC
            if (opcode == Opcodes.GETSTATIC) {
                // get type
                if (absIns.getType() == AbstractInsnNode.FIELD_INSN) {
                    final Boolean[] foundField = { false };
                    final ClassNode cNode = classNode;
                    final FieldNode fNode = fieldNode;
                    absIns.accept(new MethodVisitor(Opcodes.ASM5) {
                        @Override
                        public void visitFieldInsn(int i, String s, String s2, String s3) {
                            if (cNode.name.equals(s) && fNode.name.equals(s2)) {
                                foundField[0] = true;
                            }
                            super.visitFieldInsn(i, s, s2, s3);
                        }
                    });
                    if (foundField[0]) {
                        inst.set(absIns, new FieldInsnNode(Opcodes.GETSTATIC, className, fieldNode.name,
                                fieldNode.desc));
                    }
                }
            } else if (opcode == Opcodes.PUTSTATIC) {
                if (absIns.getType() == AbstractInsnNode.FIELD_INSN) {
                    final Boolean[] foundField = { false };
                    final ClassNode cNode = classNode;
                    final FieldNode fNode = fieldNode;
                    absIns.accept(new MethodVisitor(Opcodes.ASM5) {
                        @Override
                        public void visitFieldInsn(int i, String s, String s2, String s3) {
                            if (cNode.name.equals(s) && fNode.name.equals(s2)) {
                                foundField[0] = true;
                            }
                            super.visitFieldInsn(i, s, s2, s3);
                        }
                    });
                    if (foundField[0]) {
                        inst.set(absIns, new FieldInsnNode(Opcodes.PUTSTATIC, className, fieldNode.name,
                                fieldNode.desc));
                    }
                }
            }
        }
    }
}

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 w  w  . j a  v  a  2  s  . c  o  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;
                                }/*  w  w w .j  a  v  a 2  s . com*/
                            }
                            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.VirtualMethodReplacer.java

License:Open Source License

@Override
public MethodNode replaceInvoke(MethodNode methodNode) {
    InsnList instructions = methodNode.instructions;
    Iterator it = instructions.iterator();
    while (it.hasNext()) {
        AbstractInsnNode code = (AbstractInsnNode) it.next();
        if (code.getOpcode() == Opcodes.INVOKEVIRTUAL) {
            // check if methodToReplace is called
            final boolean[] callFounded = new boolean[] { false };
            code.accept(new MethodVisitor(Opcodes.ASM5) {
                @Override/*from  w w w  . j  ava 2s  . com*/
                public void visitMethodInsn(int i, String s, String s2, String s3) {
                    if (s.equals(classContainer) && s2.equals(methodName)) {
                        callFounded[0] = true;
                    }
                    super.visitMethodInsn(i, s, s2, s3);
                }
            });

            if (callFounded[0]) {
                // if the return type is primitive and the value is not discarded, unbox
                if (AutoBoxing.isPrimitive(retType.getDescriptor())) {
                    AbstractInsnNode codeNext = code.getNext();
                    boolean discarded = false;
                    // if returning primitive double or long and it is discarded with a pop2 than discard with
                    // simple pop, because we use an Object as return value.
                    if (codeNext.getOpcode() == Opcodes.POP2
                            && (retType.getDescriptor().equals("D") || retType.getDescriptor().equals("J"))) {
                        instructions.set(codeNext, new InsnNode(Opcodes.POP));

                    }
                    if (codeNext.getOpcode() == Opcodes.POP || codeNext.getOpcode() == Opcodes.POP2) {
                        discarded = true;
                    }
                    if (!discarded) {
                        instructions.insert(code, AutoBoxing.unbox(retType));
                    }
                }

                // replace call with a custom call
                String newMethodName;
                AbstractInsnNode newInvoke = null;
                if (Constants.VAROBJECT.equals(methodType)) {
                    newMethodName = TransformerNameGenerator.getObjectMethodNameWithCounter(classContainer,
                            methodNumber);
                    newInvoke = new MethodInsnNode(Opcodes.INVOKEVIRTUAL, classContainer, newMethodName,
                            "([Ljava/lang/Object;)Ljava/lang/Object;");
                } else if (Constants.INT.equals(methodType)) {
                    newMethodName = TransformerNameGenerator.getIntMethodNameWithCounter(classContainer,
                            methodNumber);
                    newInvoke = new MethodInsnNode(Opcodes.INVOKEVIRTUAL, classContainer, newMethodName,
                            "(I)Ljava/lang/Object;");
                } else if (Constants.FLOAT.equals(methodType)) {
                    newMethodName = TransformerNameGenerator.getFloatMethodNameWithCounter(classContainer,
                            methodNumber);
                    newInvoke = new MethodInsnNode(Opcodes.INVOKEVIRTUAL, classContainer, newMethodName,
                            "(F)Ljava/lang/Object;");
                } else if (Constants.STRING.equals(methodType)) {
                    newMethodName = TransformerNameGenerator.getStringMethodNameWithCounter(classContainer,
                            methodNumber);
                    newInvoke = new MethodInsnNode(Opcodes.INVOKEVIRTUAL, classContainer, newMethodName,
                            "(Ljava/lang/String;)Ljava/lang/Object;");
                } else if (Constants.LONG.equals(methodType)) {
                    newMethodName = TransformerNameGenerator.getLongMethodNameWithCounter(classContainer,
                            methodNumber);
                    newInvoke = new MethodInsnNode(Opcodes.INVOKEVIRTUAL, classContainer, newMethodName,
                            "(J)Ljava/lang/Object;");
                }
                if (newInvoke != null) {
                    instructions.set(code, newInvoke);
                }
            }
        }
    }
    return methodNode;
}

From source file:org.diorite.inject.controller.Transformer.java

License:Open Source License

private void findReturns(TransformerInitMethodData initPair) {
    MethodNode init = initPair.node;/*from  www  .  j  ava2  s .  co m*/
    AbstractInsnNode node = init.instructions.getFirst();
    while (node != null) {
        if ((node instanceof InsnNode) && AsmUtils.isReturnCode(node.getOpcode())) {
            initPair.returns.add((InsnNode) node);
        }
        node = node.getNext();
        if (node == null) {
            break;
        }
    }
}

From source file:org.diorite.inject.controller.Transformer.java

License:Open Source License

@SuppressWarnings("unchecked")
private MethodInsnNode findSuperNode(MethodNode init) {
    // we need to find super(...) invoke, it should be first super.<init> invoke, but it might be invoke to some new created object of super
    // type, so we need to track created objects
    Collection<String> objects = new LinkedList<>();
    ListIterator<AbstractInsnNode> iterator = init.instructions.iterator();
    while (iterator.hasNext()) {
        AbstractInsnNode next_ = iterator.next();
        if (next_.getOpcode() == Opcodes.NEW) {
            TypeInsnNode next = (TypeInsnNode) next_;
            objects.add(next.desc);/* www.j  av a 2s .c  o  m*/
        } else if (next_.getOpcode() == Opcodes.INVOKESPECIAL) {
            MethodInsnNode next = (MethodInsnNode) next_;
            if (!next.name.equals(InjectionController.CONSTRUCTOR_NAME)) {
                continue;
            }
            if (!objects.remove(next.owner) && next.owner.equals(this.classNode.superName)) {
                return next;
            }
        }
    }
    throw new TransformerError("Can't find super() invoke for constructor!");
}