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:com.android.builder.testing.MockableJarGenerator.java

License:Apache License

/**
 * Rewrites the method bytecode to remove the "Stub!" exception.
 *///www  .  j ava2 s.c  om
private void fixMethodBody(MethodNode methodNode, ClassNode classNode) {
    if ((methodNode.access & Opcodes.ACC_NATIVE) != 0 || (methodNode.access & Opcodes.ACC_ABSTRACT) != 0) {
        // Abstract and native method don't have bodies to rewrite.
        return;
    }

    if ((classNode.access & Opcodes.ACC_ENUM) != 0 && ENUM_METHODS.contains(methodNode.name)) {
        // Don't break enum classes.
        return;
    }

    Type returnType = Type.getReturnType(methodNode.desc);
    InsnList instructions = methodNode.instructions;

    if (methodNode.name.equals(CONSTRUCTOR)) {
        // Keep the call to parent constructor, delete the exception after that.

        boolean deadCode = false;
        for (AbstractInsnNode instruction : instructions.toArray()) {
            if (!deadCode) {
                if (instruction.getOpcode() == Opcodes.INVOKESPECIAL) {
                    instructions.insert(instruction, new InsnNode(Opcodes.RETURN));
                    // Start removing all following instructions.
                    deadCode = true;
                }
            } else {
                instructions.remove(instruction);
            }
        }
    } else {
        instructions.clear();

        if (returnDefaultValues || methodNode.name.equals(CLASS_CONSTRUCTOR)) {
            if (INTEGER_LIKE_TYPES.contains(returnType)) {
                instructions.add(new InsnNode(Opcodes.ICONST_0));
            } else if (returnType.equals(Type.LONG_TYPE)) {
                instructions.add(new InsnNode(Opcodes.LCONST_0));
            } else if (returnType.equals(Type.FLOAT_TYPE)) {
                instructions.add(new InsnNode(Opcodes.FCONST_0));
            } else if (returnType.equals(Type.DOUBLE_TYPE)) {
                instructions.add(new InsnNode(Opcodes.DCONST_0));
            } else {
                instructions.add(new InsnNode(Opcodes.ACONST_NULL));
            }

            instructions.add(new InsnNode(returnType.getOpcode(Opcodes.IRETURN)));
        } else {
            instructions.insert(throwExceptionsList(methodNode, classNode));
        }
    }
}

From source file:com.android.ide.eclipse.apt.internal.analysis.ConstantFinalAnalyzer.java

License:Apache License

@Override
protected Collection<Problem> analyzeMethod(final MethodNode methodNode) {
    final LinkedList<Problem> problems = new LinkedList<Problem>();
    final InsnList instructions = methodNode.instructions;
    for (int i = 0; i < instructions.size(); i++) {
        final AbstractInsnNode insnNode = instructions.get(i);
        if (insnNode.getOpcode() == Opcodes.PUTSTATIC) {
            final FieldInsnNode field = (FieldInsnNode) insnNode;
            final String fieldName = field.name;
            if (!isFinal(fieldName)) {
                final Problem problem = new Problem(insnNode);
                problems.add(problem);//from w w  w .  j a v  a 2 s .  com
            }
        }
    }
    return problems;
}

From source file:com.android.ide.eclipse.apt.internal.analysis.EnhancedForLoopAnalyzer.java

License:Apache License

@Override
protected Collection<Problem> analyzeMethod(MethodNode methodNode) {
    final LinkedList<Problem> problems = new LinkedList<Problem>();
    final LinkedList<Problem> iterators = new LinkedList<Problem>();
    final InsnList instructions = methodNode.instructions;
    for (int i = 0; i < instructions.size(); i++) {
        final AbstractInsnNode insnNode = instructions.get(i);
        if (insnNode.getOpcode() == Opcodes.INVOKEINTERFACE) {
            final MethodInsnNode interfaceInst = (MethodInsnNode) insnNode;
            if (interfaceInst.owner.equals(ITERATOR_CLASS)) {
                final Problem problem = new Problem(insnNode);
                if (iterators.contains(problem)) {
                    iterators.remove(problem);
                    problems.add(problem);
                } else {
                    iterators.add(problem);
                }//from ww w.  j ava 2 s.c o m
            }
        }
    }
    return problems;
}

From source file:com.android.ide.eclipse.apt.internal.analysis.EnumAnalyzer.java

License:Apache License

@Override
protected Collection<Problem> analyzeMethod(MethodNode methodNode) {
    final Collection<Problem> problems = new LinkedList<Problem>();
    final InsnList instructions = methodNode.instructions;
    for (int i = 0; i < instructions.size(); i++) {
        final AbstractInsnNode instruction = instructions.get(i);
        final int op = instruction.getOpcode();
        if (op == Opcodes.GETSTATIC) {
            final FieldInsnNode field = (FieldInsnNode) instruction;
            final String fieldDesc = field.desc;
            final int fieldDescLength = fieldDesc.length();
            if (fieldDescLength > 1) {
                final String type = fieldDesc.substring(1, fieldDesc.length() - 1);
                if (mEnumClasseNames.contains(type)) {
                    final Problem problem = new Problem(instruction);
                    problems.add(problem);
                }//from   www  . ja v a2  s  . co m
            }
        }
    }
    return problems;
}

From source file:com.android.ide.eclipse.apt.internal.analysis.FloatAnalyzer.java

License:Apache License

@Override
protected Collection<Problem> analyzeMethod(final MethodNode methodNode) {
    final Collection<Problem> problems = new LinkedList<Problem>();
    final InsnList instructions = methodNode.instructions;
    for (int i = 0; i < instructions.size(); i++) {
        final AbstractInsnNode instruction = instructions.get(i);
        final int res = Arrays.binarySearch(FLOAT_OPERATIONS, instruction.getOpcode());
        boolean isProblem = res >= 0;
        if (!isProblem && instruction.getType() == AbstractInsnNode.METHOD_INSN) {
            final String owner = ((MethodInsnNode) instruction).owner;
            isProblem = FLOAT_CLASS.equals(owner);
        }//from w w  w . jav  a  2 s .  c  om
        if (isProblem) {
            final Problem problem = new Problem(instruction);
            if (!problems.contains(problem)) {
                problems.add(problem);
            }
        }
    }
    return problems;
}

From source file:com.android.ide.eclipse.apt.internal.analysis.InnerClassAnalyzer.java

License:Apache License

@Override
protected Collection<Problem> analyzeMethod(MethodNode methodNode) {
    final LinkedList<Problem> problems = new LinkedList<Problem>();
    final InsnList instructions = methodNode.instructions;
    for (int i = 0; i < instructions.size(); i++) {
        final AbstractInsnNode insnNode = instructions.get(i);
        if (insnNode.getOpcode() == Opcodes.INVOKESTATIC) {
            final MethodInsnNode method = (MethodInsnNode) insnNode;
            if (isStaticAccess(method)) {
                if (confirmParentClassAccess(methodNode, method)) {
                    final AbstractInsnNode inst = retrieveMethodOrField(method);
                    final int type = inst.getType();
                    if (type == AbstractInsnNode.FIELD_INSN || type == AbstractInsnNode.METHOD_INSN) {
                        final Problem problem = new Problem(inst);
                        problems.add(problem);
                    }/* w  ww  . ja  v a2 s. com*/
                }
            }
        }
    }
    return problems;
}

From source file:com.android.ide.eclipse.apt.internal.analysis.InnerClassAnalyzer.java

License:Apache License

/**
 * Checks if a method of an inner class accesses the private content of an outer class (field or method)
 * @param methodNode//from w w w  . ja v a  2 s .c o  m
 * @param method
 * @return True if the outer access is confirmed, false otherwise.
 */
private boolean confirmParentClassAccess(final MethodNode methodNode, final MethodInsnNode method) {
    final String methodNodeName = methodNode.name;
    AbstractInsnNode prev = method.getPrevious();
    boolean result = false;
    while (!result) {
        if (methodNodeName.startsWith("<init>")) {
            if (prev.getOpcode() == Opcodes.ALOAD) {
                final VarInsnNode varInsn = (VarInsnNode) prev;
                result = varInsn.var == 1;
            }
        } else {
            if (prev.getOpcode() == Opcodes.GETFIELD) {
                final FieldInsnNode getField = (FieldInsnNode) prev;
                final String field = getField.owner + getField.name;
                final String testField = mInnerClass.name + "this$0";
                result = field.equals(testField);
            }
        }
        if (prev.getType() == AbstractInsnNode.LINE) {
            break;
        } else {
            prev = prev.getPrevious();
        }
    }
    return result;
}

From source file:com.android.ide.eclipse.apt.internal.analysis.InternalGetSetAnalyzer.java

License:Apache License

@Override
protected Collection<Problem> analyzeMethod(MethodNode methodNode) {
    final Collection<Problem> problems = new LinkedList<Problem>();
    final InsnList instructions = methodNode.instructions;
    for (int i = 0; i < instructions.size(); i++) {
        final AbstractInsnNode instruction = instructions.get(i);
        final int op = instruction.getOpcode();
        if (op == Opcodes.INVOKESTATIC || op == Opcodes.INVOKEVIRTUAL) {
            final MethodInsnNode method = (MethodInsnNode) instruction;
            if (isGetterOrSetter(method)) {
                final Problem problem = new Problem(instruction);
                problems.add(problem);// w  ww. java2s  .  c om
            }
        }
    }
    return problems;
}

From source file:com.android.ide.eclipse.apt.internal.analysis.InternalGetSetAnalyzer.java

License:Apache License

/**
 * Checks if a method is a getter//from  w w w. j  av a2 s  .  c om
 * @param methodTest The method to test
 * @return True if the method is a getter, false otherwise
 */
private boolean isGetter(final MethodNode methodTest) {
    boolean getter = false;
    final String desc = methodTest.desc;
    final Type[] arguments = Type.getArgumentTypes(desc);
    final Type returnType = Type.getReturnType(desc);
    if (arguments.length == 0 && returnType.getSort() != Type.VOID) {
        final InsnList instructions = methodTest.instructions;
        //three next to skip label and line number instructions
        final AbstractInsnNode first = instructions.getFirst().getNext().getNext();
        final int returnOp = returnType.getOpcode(Opcodes.IRETURN);
        final int firstOp = first.getOpcode();
        //check for static getter
        if ((Opcodes.ACC_STATIC & methodTest.access) == 0) {
            if (firstOp == Opcodes.ALOAD) {
                final AbstractInsnNode second = first.getNext();
                if (second.getOpcode() == Opcodes.GETFIELD) {
                    final AbstractInsnNode third = second.getNext();
                    if (third.getOpcode() == returnOp) {
                        getter = true;
                    }
                }
            }
        } else {
            if (firstOp == Opcodes.GETSTATIC) {
                final AbstractInsnNode second = first.getNext();
                if (second.getOpcode() == returnOp) {
                    getter = true;
                }
            }
        }
    }
    return getter;
}

From source file:com.android.ide.eclipse.apt.internal.analysis.InternalGetSetAnalyzer.java

License:Apache License

/**
 * Checks if a method is a setter/*  w w w . ja v  a  2 s. c o  m*/
 * @param methodTest The method to be checked
 * @return True if the method is a setter, false otherwise
 */
private boolean isSetter(final MethodNode methodTest) {
    boolean setter = false;
    final String desc = methodTest.desc;
    final Type[] arguments = Type.getArgumentTypes(desc);
    final Type returnType = Type.getReturnType(desc);
    if (arguments.length == 1 && returnType.getSort() == Type.VOID) {
        final InsnList instructions = methodTest.instructions;
        //skip label and line number instructions
        final AbstractInsnNode first = instructions.getFirst().getNext().getNext();
        final int loadOp = arguments[0].getOpcode(Opcodes.ILOAD);
        final int firstOp = first.getOpcode();
        //check for static setter
        if ((Opcodes.ACC_STATIC & methodTest.access) == 0) {
            if (firstOp == Opcodes.ALOAD) {
                final AbstractInsnNode second = first.getNext();
                if (second.getOpcode() == loadOp) {
                    final AbstractInsnNode third = second.getNext();
                    if (third.getOpcode() == Opcodes.PUTFIELD) {
                        //three next to skip label and line number instructions
                        final AbstractInsnNode fourth = third.getNext().getNext().getNext();
                        if (fourth.getOpcode() == Opcodes.RETURN) {
                            setter = true;
                        }
                    }
                }
            }
        } else {
            if (firstOp == loadOp) {
                final AbstractInsnNode second = first.getNext();
                if (second.getOpcode() == Opcodes.PUTSTATIC) {
                    final AbstractInsnNode third = second.getNext().getNext().getNext();
                    if (third.getOpcode() == Opcodes.RETURN) {
                        setter = true;
                    }
                }
            }
        }
    }
    return setter;
}