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: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   ww  w. j  a  va  2 s.c  o  m
            }
        }
    }
    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   w  ww .  j  a va 2 s . co 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);
                }//www.  ja  v  a  2s. c  o 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);
        }/*  www  .j a  v  a 2s  . co  m*/
        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.  jav a2  s  .com*/
                }
            }
        }
    }
    return problems;
}

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. j  ava 2 s. c om*/
            }
        }
    }
    return problems;
}

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

License:Apache License

@Override
protected Collection<Problem> analyzeMethod(final MethodNode methodNode) {
    final Collection<Problem> problems = new LinkedList<Problem>();
    final int acc = methodNode.access;
    if ((acc & Opcodes.ACC_STATIC) == 0 && (acc & Opcodes.ACC_ABSTRACT) == 0) {
        final InsnList instructions = methodNode.instructions;
        //find if a method load this (ALOAD 0)
        boolean aload0 = false;
        for (int i = 0; i < instructions.size(); i++) {
            final AbstractInsnNode instruction = instructions.get(i);
            if (instruction.getOpcode() == Opcodes.ALOAD) {
                final VarInsnNode aload = (VarInsnNode) instruction;
                aload0 = aload.var == 0;
                if (aload0) {
                    break;
                }//from  ww  w  .  java  2 s  .c  o  m
            }
        }
        if (!aload0) {
            final Problem problem = new StaticVirtualProblem(instructions.get(1));
            problems.add(problem);
        }
    }
    return problems;
}

From source file:com.android.ide.eclipse.apt.internal.analysis.VirtualInterfaceAnalyzer.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.INVOKEINTERFACE) {
            final Problem problem = new Problem(insnNode);
            problems.add(problem);/*from   w  w w . j a  v  a  2  s  .c om*/
        }
    }
    return problems;
}

From source file:com.android.tools.klint.checks.ControlFlowGraph.java

License:Apache License

/**
 * Creates a new {@link ControlFlowGraph} and populates it with the flow
 * control for the given method. If the optional {@code initial} parameter is
 * provided with an existing graph, then the graph is simply populated, not
 * created. This allows subclassing of the graph instance, if necessary.
 *
 * @param initial usually null, but can point to an existing instance of a
 *            {@link ControlFlowGraph} in which that graph is reused (but
 *            populated with new edges)//  w  w  w.  java 2 s.c o m
 * @param classNode the class containing the method to be analyzed
 * @param method the method to be analyzed
 * @return a {@link ControlFlowGraph} with nodes for the control flow in the
 *         given method
 * @throws AnalyzerException if the underlying bytecode library is unable to
 *             analyze the method bytecode
 */
@NonNull
public static ControlFlowGraph create(@Nullable ControlFlowGraph initial, @NonNull ClassNode classNode,
        @NonNull MethodNode method) throws AnalyzerException {
    final ControlFlowGraph graph = initial != null ? initial : new ControlFlowGraph();
    final InsnList instructions = method.instructions;
    graph.mNodeMap = Maps.newHashMapWithExpectedSize(instructions.size());
    graph.mMethod = method;

    // Create a flow control graph using ASM5's analyzer. According to the ASM 4 guide
    // (download.forge.objectweb.org/asm/asm4-guide.pdf) there are faster ways to construct
    // it, but those require a lot more code.
    Analyzer analyzer = new Analyzer(new BasicInterpreter()) {
        @Override
        protected void newControlFlowEdge(int insn, int successor) {
            // Update the information as of whether the this object has been
            // initialized at the given instruction.
            AbstractInsnNode from = instructions.get(insn);
            AbstractInsnNode to = instructions.get(successor);
            graph.add(from, to);
        }

        @Override
        protected boolean newControlFlowExceptionEdge(int insn, TryCatchBlockNode tcb) {
            AbstractInsnNode from = instructions.get(insn);
            graph.exception(from, tcb);
            return super.newControlFlowExceptionEdge(insn, tcb);
        }

        @Override
        protected boolean newControlFlowExceptionEdge(int insn, int successor) {
            AbstractInsnNode from = instructions.get(insn);
            AbstractInsnNode to = instructions.get(successor);
            graph.exception(from, to);
            return super.newControlFlowExceptionEdge(insn, successor);
        }
    };

    analyzer.analyze(classNode.name, method);
    return graph;
}

From source file:com.android.tools.klint.client.api.AsmVisitor.java

License:Apache License

@SuppressWarnings("rawtypes") // ASM API uses raw types
void runClassDetectors(ClassContext context) {
    ClassNode classNode = context.getClassNode();

    for (Detector detector : mAllDetectors) {
        detector.beforeCheckFile(context);
    }/*from   w  ww  .  ja  va  2s  .  co  m*/

    for (Detector detector : mFullClassChecks) {
        Detector.ClassScanner scanner = (Detector.ClassScanner) detector;
        scanner.checkClass(context, classNode);
        detector.afterCheckFile(context);
    }

    if (!mMethodNameToChecks.isEmpty() || !mMethodOwnerToChecks.isEmpty()
            || mNodeTypeDetectors != null && mNodeTypeDetectors.length > 0) {
        List methodList = classNode.methods;
        for (Object m : methodList) {
            MethodNode method = (MethodNode) m;
            InsnList nodes = method.instructions;
            for (int i = 0, n = nodes.size(); i < n; i++) {
                AbstractInsnNode instruction = nodes.get(i);
                int type = instruction.getType();
                if (type == AbstractInsnNode.METHOD_INSN) {
                    MethodInsnNode call = (MethodInsnNode) instruction;

                    String owner = call.owner;
                    List<ClassScanner> scanners = mMethodOwnerToChecks.get(owner);
                    if (scanners != null) {
                        for (ClassScanner scanner : scanners) {
                            scanner.checkCall(context, classNode, method, call);
                        }
                    }

                    String name = call.name;
                    scanners = mMethodNameToChecks.get(name);
                    if (scanners != null) {
                        for (ClassScanner scanner : scanners) {
                            scanner.checkCall(context, classNode, method, call);
                        }
                    }
                }

                if (mNodeTypeDetectors != null && type < mNodeTypeDetectors.length) {
                    List<ClassScanner> scanners = mNodeTypeDetectors[type];
                    if (scanners != null) {
                        for (ClassScanner scanner : scanners) {
                            scanner.checkInstruction(context, classNode, method, instruction);
                        }
                    }
                }
            }
        }
    }

    for (Detector detector : mAllDetectors) {
        detector.afterCheckFile(context);
    }
}