Search in sources :

Example 1 with MethodNode

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

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

the class InlineCodegen method generateLambdaBody.

@NotNull
private SMAPAndMethodNode generateLambdaBody(@NotNull LambdaInfo info) {
    KtExpression declaration = info.getFunctionWithBodyOrCallableReference();
    FunctionDescriptor descriptor = info.getFunctionDescriptor();
    ClassContext closureContext = info.isPropertyReference() ? codegen.getContext().intoAnonymousClass(info.getClassDescriptor(), codegen, OwnerKind.IMPLEMENTATION) : codegen.getContext().intoClosure(descriptor, codegen, typeMapper);
    MethodContext context = closureContext.intoInlinedLambda(descriptor, info.isCrossInline, info.isPropertyReference());
    JvmMethodSignature jvmMethodSignature = typeMapper.mapSignatureSkipGeneric(descriptor);
    Method asmMethod = jvmMethodSignature.getAsmMethod();
    MethodNode methodNode = new MethodNode(InlineCodegenUtil.API, getMethodAsmFlags(descriptor, context.getContextKind(), state), asmMethod.getName(), asmMethod.getDescriptor(), null, null);
    MethodVisitor adapter = InlineCodegenUtil.wrapWithMaxLocalCalc(methodNode);
    SMAP smap = generateMethodBody(adapter, descriptor, context, declaration, jvmMethodSignature, codegen, info);
    adapter.visitMaxs(-1, -1);
    return new SMAPAndMethodNode(methodNode, smap);
}
Also used : JvmMethodSignature(org.jetbrains.kotlin.resolve.jvm.jvmSignature.JvmMethodSignature) MethodNode(org.jetbrains.org.objectweb.asm.tree.MethodNode) Method(org.jetbrains.org.objectweb.asm.commons.Method) MethodVisitor(org.jetbrains.org.objectweb.asm.MethodVisitor) NotNull(org.jetbrains.annotations.NotNull)

Example 3 with MethodNode

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

the class InlineCodegen method createMethodNode.

@NotNull
static SMAPAndMethodNode createMethodNode(@NotNull final FunctionDescriptor functionDescriptor, @NotNull JvmMethodSignature jvmSignature, @NotNull ExpressionCodegen codegen, @NotNull CodegenContext context, boolean callDefault, @Nullable ResolvedCall<?> resolvedCall) {
    if (InlineCodegenUtil.isSpecialEnumMethod(functionDescriptor)) {
        assert resolvedCall != null : "Resolved call for " + functionDescriptor + " should be not null";
        Map<TypeParameterDescriptor, KotlinType> arguments = resolvedCall.getTypeArguments();
        assert arguments.size() == 1 : "Resolved call for " + functionDescriptor + " should have 1 type argument";
        MethodNode node = InlineCodegenUtil.createSpecialEnumMethodBody(codegen, functionDescriptor.getName().asString(), arguments.keySet().iterator().next().getDefaultType(), codegen.getState().getTypeMapper());
        return new SMAPAndMethodNode(node, SMAPParser.parseOrCreateDefault(null, null, "fake", -1, -1));
    } else if (CoroutineCodegenUtilKt.isBuiltInSuspendCoroutineOrReturnInJvm(functionDescriptor)) {
        return new SMAPAndMethodNode(CoroutineCodegenUtilKt.createMethodNodeForSuspendCoroutineOrReturn(functionDescriptor, codegen.getState().getTypeMapper()), SMAPParser.parseOrCreateDefault(null, null, "fake", -1, -1));
    }
    final GenerationState state = codegen.getState();
    final Method asmMethod = callDefault ? state.getTypeMapper().mapDefaultMethod(functionDescriptor, context.getContextKind()) : jvmSignature.getAsmMethod();
    MethodId methodId = new MethodId(DescriptorUtils.getFqNameSafe(functionDescriptor.getContainingDeclaration()), asmMethod);
    final CallableMemberDescriptor directMember = getDirectMemberAndCallableFromObject(functionDescriptor);
    if (!isBuiltInArrayIntrinsic(functionDescriptor) && !(directMember instanceof DeserializedCallableMemberDescriptor)) {
        return doCreateMethodNodeFromSource(functionDescriptor, jvmSignature, codegen, context, callDefault, state, asmMethod);
    }
    SMAPAndMethodNode resultInCache = InlineCacheKt.getOrPut(state.getInlineCache().getMethodNodeById(), methodId, new Function0<SMAPAndMethodNode>() {

        @Override
        public SMAPAndMethodNode invoke() {
            SMAPAndMethodNode result = doCreateMethodNodeFromCompiled(directMember, state, asmMethod);
            if (result == null) {
                throw new IllegalStateException("Couldn't obtain compiled function body for " + functionDescriptor);
            }
            return result;
        }
    });
    return resultInCache.copyWithNewNode(cloneMethodNode(resultInCache.getNode()));
}
Also used : KotlinType(org.jetbrains.kotlin.types.KotlinType) Method(org.jetbrains.org.objectweb.asm.commons.Method) DeserializedCallableMemberDescriptor(org.jetbrains.kotlin.serialization.deserialization.descriptors.DeserializedCallableMemberDescriptor) GenerationState(org.jetbrains.kotlin.codegen.state.GenerationState) MethodNode(org.jetbrains.org.objectweb.asm.tree.MethodNode) DeserializedCallableMemberDescriptor(org.jetbrains.kotlin.serialization.deserialization.descriptors.DeserializedCallableMemberDescriptor) NotNull(org.jetbrains.annotations.NotNull)

Example 4 with MethodNode

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

the class InlineCodegen method cloneMethodNode.

@NotNull
private static MethodNode cloneMethodNode(@NotNull MethodNode methodNode) {
    methodNode.instructions.resetLabels();
    MethodNode result = new MethodNode(API, methodNode.access, methodNode.name, methodNode.desc, methodNode.signature, ArrayUtil.toStringArray(methodNode.exceptions));
    methodNode.accept(result);
    return result;
}
Also used : MethodNode(org.jetbrains.org.objectweb.asm.tree.MethodNode) NotNull(org.jetbrains.annotations.NotNull)

Example 5 with MethodNode

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

the class InlineCodegen method throwCompilationException.

@NotNull
private CompilationException throwCompilationException(@Nullable SMAPAndMethodNode nodeAndSmap, @NotNull Exception e, boolean generateNodeText) {
    CallableMemberDescriptor contextDescriptor = codegen.getContext().getContextDescriptor();
    PsiElement element = DescriptorToSourceUtils.descriptorToDeclaration(contextDescriptor);
    MethodNode node = nodeAndSmap != null ? nodeAndSmap.getNode() : null;
    throw new CompilationException("Couldn't inline method call '" + functionDescriptor.getName() + "' into\n" + DescriptorRenderer.DEBUG_TEXT.render(contextDescriptor) + "\n" + (element != null ? element.getText() : "<no source>") + (generateNodeText ? ("\nCause: " + InlineCodegenUtil.getNodeText(node)) : ""), e, callElement);
}
Also used : MethodNode(org.jetbrains.org.objectweb.asm.tree.MethodNode) DeserializedCallableMemberDescriptor(org.jetbrains.kotlin.serialization.deserialization.descriptors.DeserializedCallableMemberDescriptor) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

MethodNode (org.jetbrains.org.objectweb.asm.tree.MethodNode)16 NotNull (org.jetbrains.annotations.NotNull)8 List (java.util.List)4 ClassNode (org.jetbrains.org.objectweb.asm.tree.ClassNode)4 DeserializedCallableMemberDescriptor (org.jetbrains.kotlin.serialization.deserialization.descriptors.DeserializedCallableMemberDescriptor)3 MethodVisitor (org.jetbrains.org.objectweb.asm.MethodVisitor)3 AbstractInsnNode (org.jetbrains.org.objectweb.asm.tree.AbstractInsnNode)3 AnalyzerException (org.jetbrains.org.objectweb.asm.tree.analysis.AnalyzerException)3 PsiElement (com.intellij.psi.PsiElement)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 KotlinType (org.jetbrains.kotlin.types.KotlinType)2 ClassReader (org.jetbrains.org.objectweb.asm.ClassReader)2 Method (org.jetbrains.org.objectweb.asm.commons.Method)2 InsnList (org.jetbrains.org.objectweb.asm.tree.InsnList)2 MethodInsnNode (org.jetbrains.org.objectweb.asm.tree.MethodInsnNode)2 Detector (com.android.tools.klint.detector.api.Detector)1 ClassScanner (com.android.tools.klint.detector.api.Detector.ClassScanner)1 Location (com.android.tools.klint.detector.api.Location)1 ProcessCanceledException (com.intellij.openapi.progress.ProcessCanceledException)1