Search in sources :

Example 1 with ClassNode

use of org.jetbrains.org.objectweb.asm.tree.ClassNode 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 2 with ClassNode

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

the class LintDriver method getOuterClassNode.

/** Returns the outer class node of the given class node
     * @param classNode the inner class node
     * @return the outer class node */
public ClassNode getOuterClassNode(@NonNull ClassNode classNode) {
    String outerName = classNode.outerClass;
    Iterator<ClassNode> iterator = mOuterClasses.iterator();
    while (iterator.hasNext()) {
        ClassNode node = iterator.next();
        if (outerName != null) {
            if (node.name.equals(outerName)) {
                return node;
            }
        } else if (node == classNode) {
            return iterator.hasNext() ? iterator.next() : null;
        }
    }
    return null;
}
Also used : ClassNode(org.jetbrains.org.objectweb.asm.tree.ClassNode)

Example 3 with ClassNode

use of org.jetbrains.org.objectweb.asm.tree.ClassNode in project android by JetBrains.

the class ClassConverterTest method testMethodWrapping.

public void testMethodWrapping() throws Exception {
    byte[] data = ClassConverterTest.dumpTestViewClass();
    assertTrue(isValidClassFile(data));
    byte[] modified = rewriteClass(data, Integer.MAX_VALUE);
    assertTrue(isValidClassFile(data));
    // Parse both classes and compare
    ClassNode classNode = new ClassNode();
    ClassReader classReader = new ClassReader(modified);
    classReader.accept(classNode, 0);
    assertEquals(3, classNode.methods.size());
    final Set<String> methods = new HashSet<>();
    classNode.accept(new ClassVisitor(ASM5) {

        @Override
        public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
            methods.add(name + desc);
            return super.visitMethod(access, name, desc, signature, exceptions);
        }
    });
    assertTrue(methods.contains("onMeasure(II)V"));
    assertTrue(methods.contains("onMeasure_Original(II)V"));
}
Also used : ClassNode(org.jetbrains.org.objectweb.asm.tree.ClassNode) HashSet(java.util.HashSet)

Example 4 with ClassNode

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

the class CodegenTestCase method verifyWithAsm.

private static boolean verifyWithAsm(@NotNull OutputFile file, ClassLoader loader) {
    ClassNode classNode = new ClassNode();
    new ClassReader(file.asByteArray()).accept(classNode, 0);
    SimpleVerifier verifier = new SimpleVerifier();
    verifier.setClassLoader(loader);
    Analyzer<BasicValue> analyzer = new Analyzer<BasicValue>(verifier);
    boolean noErrors = true;
    for (MethodNode method : classNode.methods) {
        try {
            analyzer.analyze(classNode.name, method);
        } catch (Throwable e) {
            System.err.println(file.asText());
            System.err.println(classNode.name + "::" + method.name + method.desc);
            //noinspection InstanceofCatchParameter
            if (e instanceof AnalyzerException) {
                // Print the erroneous instruction
                TraceMethodVisitor tmv = new TraceMethodVisitor(new Textifier());
                ((AnalyzerException) e).node.accept(tmv);
                PrintWriter pw = new PrintWriter(System.err);
                tmv.p.print(pw);
                pw.flush();
            }
            e.printStackTrace();
            noErrors = false;
        }
    }
    return noErrors;
}
Also used : ClassNode(org.jetbrains.org.objectweb.asm.tree.ClassNode) AnalyzerException(org.jetbrains.org.objectweb.asm.tree.analysis.AnalyzerException) SimpleVerifier(org.jetbrains.org.objectweb.asm.tree.analysis.SimpleVerifier) Analyzer(org.jetbrains.org.objectweb.asm.tree.analysis.Analyzer) Textifier(org.jetbrains.org.objectweb.asm.util.Textifier) TraceMethodVisitor(org.jetbrains.org.objectweb.asm.util.TraceMethodVisitor) BasicValue(org.jetbrains.org.objectweb.asm.tree.analysis.BasicValue) MethodNode(org.jetbrains.org.objectweb.asm.tree.MethodNode) ClassReader(org.jetbrains.org.objectweb.asm.ClassReader) PrintWriter(java.io.PrintWriter)

Example 5 with ClassNode

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

the class ClassContext method report.

/**
     * Reports an issue.
     * <p>
     * Detectors should only call this method if an error applies to the whole class
     * scope and there is no specific method or field that applies to the error.
     * If so, use
     * {@link #report(Issue, MethodNode, AbstractInsnNode, Location, String)} or
     * {@link #report(Issue, FieldNode, Location, String)}, such that
     * suppress annotations are checked.
     *
     * @param issue the issue to report
     * @param location the location of the issue, or null if not known
     * @param message the message for this warning
     */
@Override
public void report(@NonNull Issue issue, @NonNull Location location, @NonNull String message) {
    if (mDriver.isSuppressed(issue, mClassNode)) {
        return;
    }
    ClassNode curr = mClassNode;
    while (curr != null) {
        ClassNode prev = curr;
        curr = mDriver.getOuterClassNode(curr);
        if (curr != null) {
            if (prev.outerMethod != null) {
                // ASM API
                @SuppressWarnings("rawtypes") List methods = curr.methods;
                for (Object m : methods) {
                    MethodNode method = (MethodNode) m;
                    if (method.name.equals(prev.outerMethod) && method.desc.equals(prev.outerMethodDesc)) {
                        // class hierarchy)
                        if (method != null && mDriver.isSuppressed(issue, mClassNode, method, null)) {
                            return;
                        }
                        break;
                    }
                }
            }
            if (mDriver.isSuppressed(issue, curr)) {
                return;
            }
        }
    }
    super.report(issue, location, message);
}
Also used : ClassNode(org.jetbrains.org.objectweb.asm.tree.ClassNode) MethodNode(org.jetbrains.org.objectweb.asm.tree.MethodNode) List(java.util.List)

Aggregations

ClassNode (org.jetbrains.org.objectweb.asm.tree.ClassNode)8 MethodNode (org.jetbrains.org.objectweb.asm.tree.MethodNode)4 ClassReader (org.jetbrains.org.objectweb.asm.ClassReader)3 Detector (com.android.tools.klint.detector.api.Detector)2 List (java.util.List)2 MethodInsnNode (org.jetbrains.org.objectweb.asm.tree.MethodInsnNode)2 Nullable (com.android.annotations.Nullable)1 ClassContext (com.android.tools.klint.detector.api.ClassContext)1 ClassScanner (com.android.tools.klint.detector.api.Detector.ClassScanner)1 ResourceXmlDetector (com.android.tools.klint.detector.api.ResourceXmlDetector)1 PsiFile (com.intellij.psi.PsiFile)1 File (java.io.File)1 IOException (java.io.IOException)1 PrintWriter (java.io.PrintWriter)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 AbstractInsnNode (org.jetbrains.org.objectweb.asm.tree.AbstractInsnNode)1 AnnotationNode (org.jetbrains.org.objectweb.asm.tree.AnnotationNode)1 InsnList (org.jetbrains.org.objectweb.asm.tree.InsnList)1 Analyzer (org.jetbrains.org.objectweb.asm.tree.analysis.Analyzer)1