Search in sources :

Example 1 with ClassVisitor

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

the class CompilingEvaluatorUtils method changeSuperToMagicAccessor.

public static byte[] changeSuperToMagicAccessor(byte[] bytes) {
    ClassWriter classWriter = new ClassWriter(0);
    ClassVisitor classVisitor = new ClassVisitor(Opcodes.ASM5, classWriter) {

        @Override
        public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) {
            if ("java/lang/Object".equals(superName)) {
                superName = "sun/reflect/MagicAccessorImpl";
            }
            super.visit(version, access, name, signature, superName, interfaces);
        }
    };
    new ClassReader(bytes).accept(classVisitor, 0);
    return classWriter.toByteArray();
}
Also used : ClassReader(org.jetbrains.org.objectweb.asm.ClassReader) ClassVisitor(org.jetbrains.org.objectweb.asm.ClassVisitor) ClassWriter(org.jetbrains.org.objectweb.asm.ClassWriter)

Example 2 with ClassVisitor

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

the class FileBasedKotlinClass method loadClassAnnotations.

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

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

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

Example 3 with ClassVisitor

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

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

the class FileBasedKotlinClass method create.

// TODO public to be accessible in companion object of subclass, workaround for KT-3974
@Nullable
public static <T extends FileBasedKotlinClass> T create(@NotNull byte[] fileContents, @NotNull Function4<ClassId, Integer, KotlinClassHeader, InnerClassesInfo, T> factory) {
    final ReadKotlinClassHeaderAnnotationVisitor readHeaderVisitor = new ReadKotlinClassHeaderAnnotationVisitor();
    final Ref<String> classNameRef = Ref.create();
    final Ref<Integer> classVersion = Ref.create();
    final InnerClassesInfo innerClasses = new InnerClassesInfo();
    new ClassReader(fileContents).accept(new ClassVisitor(ASM5) {

        @Override
        public void visit(int version, int access, @NotNull String name, String signature, String superName, String[] interfaces) {
            classNameRef.set(name);
            classVersion.set(version);
        }

        @Override
        public void visitInnerClass(@NotNull String name, String outerName, String innerName, int access) {
            innerClasses.add(name, outerName, innerName);
        }

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

        @Override
        public void visitEnd() {
            readHeaderVisitor.visitEnd();
        }
    }, SKIP_CODE | SKIP_DEBUG | SKIP_FRAMES);
    String className = classNameRef.get();
    if (className == null)
        return null;
    KotlinClassHeader header = readHeaderVisitor.createHeader();
    if (header == null)
        return null;
    ClassId id = resolveNameByInternalName(className, innerClasses);
    return factory.invoke(id, classVersion.get(), header, innerClasses);
}
Also used : KotlinClassHeader(org.jetbrains.kotlin.load.kotlin.header.KotlinClassHeader) ClassId(org.jetbrains.kotlin.name.ClassId) ClassVisitor(org.jetbrains.org.objectweb.asm.ClassVisitor) ReadKotlinClassHeaderAnnotationVisitor(org.jetbrains.kotlin.load.kotlin.header.ReadKotlinClassHeaderAnnotationVisitor) ReadKotlinClassHeaderAnnotationVisitor(org.jetbrains.kotlin.load.kotlin.header.ReadKotlinClassHeaderAnnotationVisitor) ClassReader(org.jetbrains.org.objectweb.asm.ClassReader) Nullable(org.jetbrains.annotations.Nullable)

Example 5 with ClassVisitor

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

the class CompileKotlinAgainstCustomBinariesTest method testInlineFunWithoutDebugInfo.

/*test source mapping generation when source info is absent*/
public void testInlineFunWithoutDebugInfo() throws Exception {
    compileKotlin("sourceInline.kt", tmpdir);
    File inlineFunClass = new File(tmpdir.getAbsolutePath(), "test/A.class");
    ClassWriter cw = new ClassWriter(Opcodes.ASM5);
    new ClassReader(FilesKt.readBytes(inlineFunClass)).accept(new ClassVisitor(Opcodes.ASM5, cw) {

        @Override
        public void visitSource(String source, String debug) {
        //skip debug info
        }
    }, 0);
    assert inlineFunClass.delete();
    assert !inlineFunClass.exists();
    FilesKt.writeBytes(inlineFunClass, cw.toByteArray());
    compileKotlin("source.kt", tmpdir, tmpdir);
    final Ref<String> debugInfo = new Ref<String>();
    File resultFile = new File(tmpdir.getAbsolutePath(), "test/B.class");
    new ClassReader(FilesKt.readBytes(resultFile)).accept(new ClassVisitor(Opcodes.ASM5) {

        @Override
        public void visitSource(String source, String debug) {
            //skip debug info
            debugInfo.set(debug);
        }
    }, 0);
    String expected = "SMAP\n" + "source.kt\n" + "Kotlin\n" + "*S Kotlin\n" + "*F\n" + "+ 1 source.kt\n" + "test/B\n" + "*L\n" + "1#1,13:1\n" + "*E\n";
    if (InlineCodegenUtil.GENERATE_SMAP) {
        assertEquals(expected, debugInfo.get());
    } else {
        assertEquals(null, debugInfo.get());
    }
}
Also used : Ref(com.intellij.openapi.util.Ref) ClassReader(org.jetbrains.org.objectweb.asm.ClassReader) ClassVisitor(org.jetbrains.org.objectweb.asm.ClassVisitor) JarFile(java.util.jar.JarFile) RecursiveDescriptorComparator.validateAndCompareDescriptorWithFile(org.jetbrains.kotlin.test.util.RecursiveDescriptorComparator.validateAndCompareDescriptorWithFile) File(java.io.File) ClassWriter(org.jetbrains.org.objectweb.asm.ClassWriter)

Aggregations

ClassVisitor (org.jetbrains.org.objectweb.asm.ClassVisitor)14 ClassReader (org.jetbrains.org.objectweb.asm.ClassReader)12 OutputFile (org.jetbrains.kotlin.backend.common.output.OutputFile)3 ReadKotlinClassHeaderAnnotationVisitor (org.jetbrains.kotlin.load.kotlin.header.ReadKotlinClassHeaderAnnotationVisitor)3 ClassWriter (org.jetbrains.org.objectweb.asm.ClassWriter)3 Ref (com.intellij.openapi.util.Ref)2 File (java.io.File)2 IOException (java.io.IOException)2 Nullable (org.jetbrains.annotations.Nullable)2 MethodVisitor (org.jetbrains.org.objectweb.asm.MethodVisitor)2 ArrayList (java.util.ArrayList)1 JarFile (java.util.jar.JarFile)1 NotNull (org.jetbrains.annotations.NotNull)1 OutputFileCollection (org.jetbrains.kotlin.backend.common.output.OutputFileCollection)1 KotlinClassHeader (org.jetbrains.kotlin.load.kotlin.header.KotlinClassHeader)1 ClassId (org.jetbrains.kotlin.name.ClassId)1 RecursiveDescriptorComparator.validateAndCompareDescriptorWithFile (org.jetbrains.kotlin.test.util.RecursiveDescriptorComparator.validateAndCompareDescriptorWithFile)1 FieldVisitor (org.jetbrains.org.objectweb.asm.FieldVisitor)1