Search in sources :

Example 1 with MethodVisitor

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

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

the class FileBasedKotlinClass method visitMembers.

@Override
public void visitMembers(@NotNull final MemberVisitor memberVisitor, @Nullable byte[] cachedContents) {
    byte[] fileContents = cachedContents != null ? cachedContents : getFileContents();
    new ClassReader(fileContents).accept(new ClassVisitor(ASM5) {

        @Override
        public FieldVisitor visitField(int access, @NotNull String name, @NotNull String desc, String signature, Object value) {
            final AnnotationVisitor v = memberVisitor.visitField(Name.identifier(name), desc, value);
            if (v == null)
                return null;
            return new FieldVisitor(ASM5) {

                @Override
                public org.jetbrains.org.objectweb.asm.AnnotationVisitor visitAnnotation(@NotNull String desc, boolean visible) {
                    return convertAnnotationVisitor(v, desc, innerClasses);
                }

                @Override
                public void visitEnd() {
                    v.visitEnd();
                }
            };
        }

        @Override
        public MethodVisitor visitMethod(int access, @NotNull String name, @NotNull String desc, String signature, String[] exceptions) {
            final MethodAnnotationVisitor v = memberVisitor.visitMethod(Name.identifier(name), desc);
            if (v == null)
                return null;
            return new MethodVisitor(ASM5) {

                @Override
                public org.jetbrains.org.objectweb.asm.AnnotationVisitor visitAnnotation(@NotNull String desc, boolean visible) {
                    return convertAnnotationVisitor(v, desc, innerClasses);
                }

                @Override
                public org.jetbrains.org.objectweb.asm.AnnotationVisitor visitParameterAnnotation(int parameter, @NotNull String desc, boolean visible) {
                    AnnotationArgumentVisitor av = v.visitParameterAnnotation(parameter, resolveNameByDesc(desc, innerClasses), SourceElement.NO_SOURCE);
                    return av == null ? null : convertAnnotationVisitor(av, innerClasses);
                }

                @Override
                public void visitEnd() {
                    v.visitEnd();
                }
            };
        }
    }, SKIP_CODE | SKIP_DEBUG | SKIP_FRAMES);
}
Also used : ClassVisitor(org.jetbrains.org.objectweb.asm.ClassVisitor) FieldVisitor(org.jetbrains.org.objectweb.asm.FieldVisitor) MethodVisitor(org.jetbrains.org.objectweb.asm.MethodVisitor) ReadKotlinClassHeaderAnnotationVisitor(org.jetbrains.kotlin.load.kotlin.header.ReadKotlinClassHeaderAnnotationVisitor) ClassReader(org.jetbrains.org.objectweb.asm.ClassReader)

Example 3 with MethodVisitor

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

the class ImplementationBodyCodegen method generateEnumValueOfMethod.

private void generateEnumValueOfMethod() {
    FunctionDescriptor valueOfFunction = CollectionsKt.single(descriptor.getStaticScope().getContributedFunctions(ENUM_VALUE_OF, NoLookupLocation.FROM_BACKEND), new Function1<FunctionDescriptor, Boolean>() {

        @Override
        public Boolean invoke(FunctionDescriptor descriptor) {
            return DescriptorUtilsKt.isEnumValueOfMethod(descriptor);
        }
    });
    MethodVisitor mv = v.newMethod(JvmDeclarationOriginKt.OtherOrigin(myClass, valueOfFunction), ACC_PUBLIC | ACC_STATIC, ENUM_VALUE_OF.asString(), "(Ljava/lang/String;)" + classAsmType.getDescriptor(), null, null);
    if (!state.getClassBuilderMode().generateBodies)
        return;
    mv.visitCode();
    mv.visitLdcInsn(classAsmType);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitMethodInsn(INVOKESTATIC, "java/lang/Enum", "valueOf", "(Ljava/lang/Class;Ljava/lang/String;)Ljava/lang/Enum;", false);
    mv.visitTypeInsn(CHECKCAST, classAsmType.getInternalName());
    mv.visitInsn(ARETURN);
    FunctionCodegen.endVisit(mv, "valueOf()", myClass);
}
Also used : MethodVisitor(org.jetbrains.org.objectweb.asm.MethodVisitor)

Example 4 with MethodVisitor

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

the class ImplementationBodyCodegen method generateEnumValuesMethod.

private void generateEnumValuesMethod() {
    Type type = typeMapper.mapType(DescriptorUtilsKt.getBuiltIns(descriptor).getArrayType(INVARIANT, descriptor.getDefaultType()));
    FunctionDescriptor valuesFunction = CollectionsKt.single(descriptor.getStaticScope().getContributedFunctions(ENUM_VALUES, NoLookupLocation.FROM_BACKEND));
    MethodVisitor mv = v.newMethod(JvmDeclarationOriginKt.OtherOrigin(myClass, valuesFunction), ACC_PUBLIC | ACC_STATIC, ENUM_VALUES.asString(), "()" + type.getDescriptor(), null, null);
    if (!state.getClassBuilderMode().generateBodies)
        return;
    mv.visitCode();
    mv.visitFieldInsn(GETSTATIC, classAsmType.getInternalName(), ENUM_VALUES_FIELD_NAME, type.getDescriptor());
    mv.visitMethodInsn(INVOKEVIRTUAL, type.getInternalName(), "clone", "()Ljava/lang/Object;", false);
    mv.visitTypeInsn(CHECKCAST, type.getInternalName());
    mv.visitInsn(ARETURN);
    FunctionCodegen.endVisit(mv, "values()", myClass);
}
Also used : Type(org.jetbrains.org.objectweb.asm.Type) KotlinType(org.jetbrains.kotlin.types.KotlinType) Type.getObjectType(org.jetbrains.org.objectweb.asm.Type.getObjectType) MethodVisitor(org.jetbrains.org.objectweb.asm.MethodVisitor)

Example 5 with MethodVisitor

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

the class SamWrapperCodegen method generateConstructor.

private void generateConstructor(Type ownerType, Type functionType, ClassBuilder cv) {
    MethodVisitor mv = cv.newMethod(JvmDeclarationOriginKt.OtherOrigin(samType.getJavaClassDescriptor()), visibility, "<init>", Type.getMethodDescriptor(Type.VOID_TYPE, functionType), null, null);
    if (state.getClassBuilderMode().generateBodies) {
        mv.visitCode();
        InstructionAdapter iv = new InstructionAdapter(mv);
        // super constructor
        iv.load(0, OBJECT_TYPE);
        iv.invokespecial(OBJECT_TYPE.getInternalName(), "<init>", "()V", false);
        // save parameter to field
        iv.load(0, OBJECT_TYPE);
        iv.load(1, functionType);
        iv.putfield(ownerType.getInternalName(), FUNCTION_FIELD_NAME, functionType.getDescriptor());
        iv.visitInsn(RETURN);
        FunctionCodegen.endVisit(iv, "constructor of SAM wrapper");
    }
}
Also used : InstructionAdapter(org.jetbrains.org.objectweb.asm.commons.InstructionAdapter) MethodVisitor(org.jetbrains.org.objectweb.asm.MethodVisitor)

Aggregations

MethodVisitor (org.jetbrains.org.objectweb.asm.MethodVisitor)23 NotNull (org.jetbrains.annotations.NotNull)7 InstructionAdapter (org.jetbrains.org.objectweb.asm.commons.InstructionAdapter)7 Type (org.jetbrains.org.objectweb.asm.Type)5 KotlinType (org.jetbrains.kotlin.types.KotlinType)4 EvaluateException (com.intellij.debugger.engine.evaluation.EvaluateException)3 List (java.util.List)3 Nullable (org.jetbrains.annotations.Nullable)3 ClassReader (org.jetbrains.org.objectweb.asm.ClassReader)3 Label (org.jetbrains.org.objectweb.asm.Label)3 MethodNode (org.jetbrains.org.objectweb.asm.tree.MethodNode)3 SourcePosition (com.intellij.debugger.SourcePosition)2 DebuggerUtilsEx (com.intellij.debugger.impl.DebuggerUtilsEx)2 MethodBytecodeUtil (com.intellij.debugger.jdi.MethodBytecodeUtil)2 ApplicationManager (com.intellij.openapi.application.ApplicationManager)2 Logger (com.intellij.openapi.diagnostic.Logger)2 Document (com.intellij.openapi.editor.Document)2 com.intellij.psi (com.intellij.psi)2 ClassVisitor (org.jetbrains.org.objectweb.asm.ClassVisitor)2 Opcodes (org.jetbrains.org.objectweb.asm.Opcodes)2