Search in sources :

Example 6 with ClassReader

use of org.jetbrains.org.objectweb.asm.ClassReader 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)

Example 7 with ClassReader

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

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

the class GenerateNotNullAssertionsTest method assertNoIntrinsicsMethodIsCalled.

private void assertNoIntrinsicsMethodIsCalled(String className, boolean noClassFileIsAnError) {
    OutputFileCollection classes = generateClassesInFile();
    OutputFile file = classes.get(className + ".class");
    if (noClassFileIsAnError) {
        assertNotNull("File for " + className + " is absent", file);
    } else if (file == null) {
        return;
    }
    ClassReader reader = new ClassReader(file.asByteArray());
    reader.accept(new ClassVisitor(Opcodes.ASM5) {

        @Override
        public MethodVisitor visitMethod(int access, @NotNull final String callerName, @NotNull final String callerDesc, String signature, String[] exceptions) {
            return new MethodVisitor(Opcodes.ASM5) {

                @Override
                public void visitMethodInsn(int opcode, String owner, String name, String desc, boolean itf) {
                    assertFalse("Intrinsics method is called: " + name + desc + "  Caller: " + callerName + callerDesc, "kotlin/jvm/internal/Intrinsics".equals(owner));
                }
            };
        }
    }, 0);
}
Also used : OutputFile(org.jetbrains.kotlin.backend.common.output.OutputFile) OutputFileCollection(org.jetbrains.kotlin.backend.common.output.OutputFileCollection) ClassReader(org.jetbrains.org.objectweb.asm.ClassReader) ClassVisitor(org.jetbrains.org.objectweb.asm.ClassVisitor) MethodVisitor(org.jetbrains.org.objectweb.asm.MethodVisitor)

Example 9 with ClassReader

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

the class OuterClassGenTest method getKotlinClassReader.

@NotNull
private ClassReader getKotlinClassReader(@Language("RegExp") @NotNull String internalNameRegexp, @NotNull String testDataFile) {
    loadFile(getPrefix() + "/" + testDataFile + ".kt");
    OutputFileCollection outputFiles = generateClassesInFile();
    for (OutputFile file : outputFiles.asList()) {
        if (file.getRelativePath().matches(internalNameRegexp + "\\.class")) {
            return new ClassReader(file.asByteArray());
        }
    }
    throw new AssertionError("Couldn't find class by regexp: " + internalNameRegexp + " in:\n" + StringsKt.join(outputFiles.asList(), "\n"));
}
Also used : OutputFile(org.jetbrains.kotlin.backend.common.output.OutputFile) OutputFileCollection(org.jetbrains.kotlin.backend.common.output.OutputFileCollection) ClassReader(org.jetbrains.org.objectweb.asm.ClassReader) NotNull(org.jetbrains.annotations.NotNull)

Example 10 with ClassReader

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

the class OuterClassGenTest method doTest.

private void doTest(@NotNull String classFqName, @NotNull String javaClassName, @NotNull String testDataFile) throws Exception {
    File javaOut = CodegenTestUtil.compileJava(Collections.singletonList(KotlinTestUtils.getTestDataPathBase() + "/codegen/" + getPrefix() + "/" + testDataFile + ".java"), Collections.<String>emptyList(), Collections.<String>emptyList());
    String javaClassPath = javaClassName.replace('.', File.separatorChar) + ".class";
    ClassReader javaReader = new ClassReader(FilesKt.readBytes(new File(javaOut, javaClassPath)));
    ClassReader kotlinReader = getKotlinClassReader(classFqName.replace('.', '/').replace("$", "\\$"), testDataFile);
    checkInfo(kotlinReader, javaReader);
}
Also used : ClassReader(org.jetbrains.org.objectweb.asm.ClassReader) File(java.io.File) OutputFile(org.jetbrains.kotlin.backend.common.output.OutputFile)

Aggregations

ClassReader (org.jetbrains.org.objectweb.asm.ClassReader)30 ClassVisitor (org.jetbrains.org.objectweb.asm.ClassVisitor)12 File (java.io.File)6 IOException (java.io.IOException)6 OutputFile (org.jetbrains.kotlin.backend.common.output.OutputFile)5 ClassWriter (org.jetbrains.org.objectweb.asm.ClassWriter)4 FailSafeClassReader (com.intellij.compiler.instrumentation.FailSafeClassReader)3 NotNull (org.jetbrains.annotations.NotNull)3 Nullable (org.jetbrains.annotations.Nullable)3 CompilerMessage (org.jetbrains.jps.incremental.messages.CompilerMessage)3 ReadKotlinClassHeaderAnnotationVisitor (org.jetbrains.kotlin.load.kotlin.header.ReadKotlinClassHeaderAnnotationVisitor)3 MethodVisitor (org.jetbrains.org.objectweb.asm.MethodVisitor)3 ClassNode (org.jetbrains.org.objectweb.asm.tree.ClassNode)3 InstrumenterClassWriter (com.intellij.compiler.instrumentation.InstrumenterClassWriter)2 PsiJavaFileStubImpl (com.intellij.psi.impl.java.stubs.impl.PsiJavaFileStubImpl)2 SourceLineCounter (com.intellij.rt.coverage.instrumentation.SourceLineCounter)2 PrintWriter (java.io.PrintWriter)2 ProgressMessage (org.jetbrains.jps.incremental.messages.ProgressMessage)2 OutputFileCollection (org.jetbrains.kotlin.backend.common.output.OutputFileCollection)2 MethodNode (org.jetbrains.org.objectweb.asm.tree.MethodNode)2