Search in sources :

Example 1 with MethodInsnNode

use of org.jetbrains.org.objectweb.asm.tree.MethodInsnNode in project kotlin by JetBrains.

the class ClassContext method getLocation.

/**
     * Returns a location for the given {@link AbstractInsnNode}.
     *
     * @param instruction the instruction to look up the location for
     * @return a location pointing to the instruction, or as close to it
     *         as possible
     */
@NonNull
public Location getLocation(@NonNull AbstractInsnNode instruction) {
    SearchHints hints = SearchHints.create(FORWARD).matchJavaSymbol();
    String pattern = null;
    if (instruction instanceof MethodInsnNode) {
        MethodInsnNode call = (MethodInsnNode) instruction;
        if (call.name.equals(CONSTRUCTOR_NAME)) {
            pattern = call.owner;
            hints = hints.matchConstructor();
        } else {
            pattern = call.name;
        }
        int index = pattern.lastIndexOf('$');
        if (index != -1) {
            pattern = pattern.substring(index + 1);
        }
        index = pattern.lastIndexOf('/');
        if (index != -1) {
            pattern = pattern.substring(index + 1);
        }
    }
    int line = findLineNumber(instruction);
    return getLocationForLine(line, pattern, null, hints);
}
Also used : MethodInsnNode(org.jetbrains.org.objectweb.asm.tree.MethodInsnNode) SearchHints(com.android.tools.klint.detector.api.Location.SearchHints) NonNull(com.android.annotations.NonNull)

Example 2 with MethodInsnNode

use of org.jetbrains.org.objectweb.asm.tree.MethodInsnNode in project kotlin by JetBrains.

the class LintDriver method isSuppressed.

/**
     * Returns whether the given issue is suppressed in the given class.
     *
     * @param issue the issue to be checked, or null to just check for "all"
     * @param classNode the class containing the issue
     * @return true if there is a suppress annotation covering the specific
     *         issue in this class
     */
public boolean isSuppressed(@Nullable Issue issue, @NonNull ClassNode classNode) {
    if (classNode.invisibleAnnotations != null) {
        @SuppressWarnings("unchecked") List<AnnotationNode> annotations = classNode.invisibleAnnotations;
        return isSuppressed(issue, annotations);
    }
    if (classNode.outerClass != null && classNode.outerMethod == null && isAnonymousClass(classNode)) {
        ClassNode outer = getOuterClassNode(classNode);
        if (outer != null) {
            MethodNode m = findMethod(outer, CONSTRUCTOR_NAME, false);
            if (m != null) {
                MethodInsnNode call = findConstructorInvocation(m, classNode.name);
                if (call != null) {
                    if (isSuppressed(issue, outer, m, call)) {
                        return true;
                    }
                }
            }
            m = findMethod(outer, CLASS_CONSTRUCTOR, false);
            if (m != null) {
                MethodInsnNode call = findConstructorInvocation(m, classNode.name);
                if (call != null) {
                    if (isSuppressed(issue, outer, m, call)) {
                        return true;
                    }
                }
            }
        }
    }
    return false;
}
Also used : ClassNode(org.jetbrains.org.objectweb.asm.tree.ClassNode) MethodNode(org.jetbrains.org.objectweb.asm.tree.MethodNode) AnnotationNode(org.jetbrains.org.objectweb.asm.tree.AnnotationNode) MethodInsnNode(org.jetbrains.org.objectweb.asm.tree.MethodInsnNode)

Example 3 with MethodInsnNode

use of org.jetbrains.org.objectweb.asm.tree.MethodInsnNode in project intellij-community by JetBrains.

the class NullableInterpreter method naryOperation.

@Override
public BasicValue naryOperation(AbstractInsnNode insn, List<? extends BasicValue> values) throws AnalyzerException {
    int opcode = insn.getOpcode();
    boolean isStaticInvoke = opcode == INVOKESTATIC;
    int shift = isStaticInvoke ? 0 : 1;
    if ((opcode == INVOKESPECIAL || opcode == INVOKEINTERFACE || opcode == INVOKEVIRTUAL) && values.get(0) instanceof ParamValue) {
        subResult = NPE;
    }
    switch(opcode) {
        case INVOKEINTERFACE:
            if (nullableAnalysis) {
                for (int i = shift; i < values.size(); i++) {
                    if (values.get(i) instanceof ParamValue) {
                        top = true;
                        return super.naryOperation(insn, values);
                    }
                }
            }
            break;
        case INVOKESTATIC:
        case INVOKESPECIAL:
        case INVOKEVIRTUAL:
            boolean stable = opcode == INVOKESTATIC || opcode == INVOKESPECIAL;
            MethodInsnNode methodNode = (MethodInsnNode) insn;
            Method method = new Method(methodNode.owner, methodNode.name, methodNode.desc);
            for (int i = shift; i < values.size(); i++) {
                BasicValue value = values.get(i);
                if (value instanceof ParamValue || (NullValue == value && nullityMask == In.NULLABLE_MASK && "<init>".equals(methodNode.name))) {
                    subResult = combine(subResult, new ConditionalNPE(new Key(method, new In(i - shift, nullityMask), stable)));
                }
            }
            break;
        default:
    }
    return super.naryOperation(insn, values);
}
Also used : In(com.intellij.codeInspection.bytecodeAnalysis.Direction.In) MethodInsnNode(org.jetbrains.org.objectweb.asm.tree.MethodInsnNode) BasicValue(org.jetbrains.org.objectweb.asm.tree.analysis.BasicValue)

Example 4 with MethodInsnNode

use of org.jetbrains.org.objectweb.asm.tree.MethodInsnNode in project kotlin by JetBrains.

the class ControlFlowGraph method dotDescribe.

private static String dotDescribe(Node node) {
    AbstractInsnNode instruction = node.instruction;
    if (instruction instanceof LabelNode) {
        return "Label";
    } else if (instruction instanceof LineNumberNode) {
        LineNumberNode lineNode = (LineNumberNode) instruction;
        return "Line " + lineNode.line;
    } else if (instruction instanceof FrameNode) {
        return "Stack Frame";
    } else if (instruction instanceof MethodInsnNode) {
        MethodInsnNode method = (MethodInsnNode) instruction;
        String cls = method.owner.substring(method.owner.lastIndexOf('/') + 1);
        cls = cls.replace('$', '.');
        return "Call " + cls + "#" + method.name;
    } else if (instruction instanceof FieldInsnNode) {
        FieldInsnNode field = (FieldInsnNode) instruction;
        String cls = field.owner.substring(field.owner.lastIndexOf('/') + 1);
        cls = cls.replace('$', '.');
        return "Field " + cls + "#" + field.name;
    } else if (instruction instanceof TypeInsnNode && instruction.getOpcode() == Opcodes.NEW) {
        return "New " + ((TypeInsnNode) instruction).desc;
    }
    StringBuilder sb = new StringBuilder();
    String opcodeName = getOpcodeName(instruction.getOpcode());
    sb.append(opcodeName);
    if (instruction instanceof IntInsnNode) {
        IntInsnNode in = (IntInsnNode) instruction;
        sb.append(" ").append(Integer.toString(in.operand));
    } else if (instruction instanceof LdcInsnNode) {
        LdcInsnNode ldc = (LdcInsnNode) instruction;
        sb.append(" ");
        if (ldc.cst instanceof String) {
            sb.append("\\\"");
        }
        sb.append(ldc.cst);
        if (ldc.cst instanceof String) {
            sb.append("\\\"");
        }
    }
    return sb.toString();
}
Also used : LabelNode(org.jetbrains.org.objectweb.asm.tree.LabelNode) FrameNode(org.jetbrains.org.objectweb.asm.tree.FrameNode) LdcInsnNode(org.jetbrains.org.objectweb.asm.tree.LdcInsnNode) MethodInsnNode(org.jetbrains.org.objectweb.asm.tree.MethodInsnNode) FieldInsnNode(org.jetbrains.org.objectweb.asm.tree.FieldInsnNode) IntInsnNode(org.jetbrains.org.objectweb.asm.tree.IntInsnNode) LineNumberNode(org.jetbrains.org.objectweb.asm.tree.LineNumberNode) TypeInsnNode(org.jetbrains.org.objectweb.asm.tree.TypeInsnNode) AbstractInsnNode(org.jetbrains.org.objectweb.asm.tree.AbstractInsnNode)

Example 5 with MethodInsnNode

use of org.jetbrains.org.objectweb.asm.tree.MethodInsnNode in project kotlin by JetBrains.

the class AsmVisitor method runClassDetectors.

// ASM API uses raw types
@SuppressWarnings("rawtypes")
void runClassDetectors(ClassContext context) {
    ClassNode classNode = context.getClassNode();
    for (Detector detector : mAllDetectors) {
        detector.beforeCheckFile(context);
    }
    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);
    }
}
Also used : ClassNode(org.jetbrains.org.objectweb.asm.tree.ClassNode) ClassScanner(com.android.tools.klint.detector.api.Detector.ClassScanner) InsnList(org.jetbrains.org.objectweb.asm.tree.InsnList) AbstractInsnNode(org.jetbrains.org.objectweb.asm.tree.AbstractInsnNode) Detector(com.android.tools.klint.detector.api.Detector) ClassScanner(com.android.tools.klint.detector.api.Detector.ClassScanner) MethodNode(org.jetbrains.org.objectweb.asm.tree.MethodNode) MethodInsnNode(org.jetbrains.org.objectweb.asm.tree.MethodInsnNode) ArrayList(java.util.ArrayList) List(java.util.List) InsnList(org.jetbrains.org.objectweb.asm.tree.InsnList)

Aggregations

MethodInsnNode (org.jetbrains.org.objectweb.asm.tree.MethodInsnNode)5 AbstractInsnNode (org.jetbrains.org.objectweb.asm.tree.AbstractInsnNode)2 ClassNode (org.jetbrains.org.objectweb.asm.tree.ClassNode)2 MethodNode (org.jetbrains.org.objectweb.asm.tree.MethodNode)2 NonNull (com.android.annotations.NonNull)1 Detector (com.android.tools.klint.detector.api.Detector)1 ClassScanner (com.android.tools.klint.detector.api.Detector.ClassScanner)1 SearchHints (com.android.tools.klint.detector.api.Location.SearchHints)1 In (com.intellij.codeInspection.bytecodeAnalysis.Direction.In)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 AnnotationNode (org.jetbrains.org.objectweb.asm.tree.AnnotationNode)1 FieldInsnNode (org.jetbrains.org.objectweb.asm.tree.FieldInsnNode)1 FrameNode (org.jetbrains.org.objectweb.asm.tree.FrameNode)1 InsnList (org.jetbrains.org.objectweb.asm.tree.InsnList)1 IntInsnNode (org.jetbrains.org.objectweb.asm.tree.IntInsnNode)1 LabelNode (org.jetbrains.org.objectweb.asm.tree.LabelNode)1 LdcInsnNode (org.jetbrains.org.objectweb.asm.tree.LdcInsnNode)1 LineNumberNode (org.jetbrains.org.objectweb.asm.tree.LineNumberNode)1 TypeInsnNode (org.jetbrains.org.objectweb.asm.tree.TypeInsnNode)1