Search in sources :

Example 1 with OutputFile

use of org.jetbrains.kotlin.backend.common.output.OutputFile in project kotlin by JetBrains.

the class AbstractLineNumberTest method extractActualLineNumbersFromBytecode.

@NotNull
private static List<String> extractActualLineNumbersFromBytecode(@NotNull ClassFileFactory factory, boolean testFunInvoke) {
    List<String> actualLineNumbers = Lists.newArrayList();
    for (OutputFile outputFile : ClassFileUtilsKt.getClassFiles(factory)) {
        ClassReader cr = new ClassReader(outputFile.asByteArray());
        try {
            List<String> lineNumbers = testFunInvoke ? readTestFunLineNumbers(cr) : readAllLineNumbers(cr);
            actualLineNumbers.addAll(lineNumbers);
        } catch (Throwable e) {
            System.out.println(factory.createText());
            throw ExceptionUtilsKt.rethrow(e);
        }
    }
    return actualLineNumbers;
}
Also used : OutputFile(org.jetbrains.kotlin.backend.common.output.OutputFile) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with OutputFile

use of org.jetbrains.kotlin.backend.common.output.OutputFile in project kotlin by JetBrains.

the class DxChecker method check.

public static void check(ClassFileFactory outputFiles) {
    Main.Arguments arguments = new Main.Arguments();
    String[] array = new String[1];
    array[0] = "testArgs";
    arguments.parse(array);
    for (OutputFile file : ClassFileUtilsKt.getClassFiles(outputFiles)) {
        try {
            byte[] bytes = file.asByteArray();
            checkFileWithDx(bytes, file.getRelativePath(), arguments);
        } catch (Throwable e) {
            Assert.fail(generateExceptionMessage(e));
        }
    }
}
Also used : OutputFile(org.jetbrains.kotlin.backend.common.output.OutputFile) Main(com.android.dx.command.dexer.Main)

Example 3 with OutputFile

use of org.jetbrains.kotlin.backend.common.output.OutputFile in project kotlin by JetBrains.

the class AbstractCheckLocalVariablesTableTest method doTest.

protected void doTest(@NotNull String ktFileName) throws Exception {
    ktFile = new File(ktFileName);
    String text = FileUtil.loadFile(ktFile, true);
    KtFile psiFile = KotlinTestUtils.createFile(ktFile.getName(), text, environment.getProject());
    OutputFileCollection outputFiles = GenerationUtils.compileFile(psiFile, environment);
    String classAndMethod = parseClassAndMethodSignature();
    String[] split = classAndMethod.split("\\.");
    assert split.length == 2 : "Exactly one dot is expected: " + classAndMethod;
    final String classFileRegex = StringUtil.escapeToRegexp(split[0] + ".class").replace("\\*", ".+");
    String methodName = split[1];
    OutputFile outputFile = ContainerUtil.find(outputFiles.asList(), new Condition<OutputFile>() {

        @Override
        public boolean value(OutputFile outputFile) {
            return outputFile.getRelativePath().matches(classFileRegex);
        }
    });
    String pathsString = StringUtil.join(outputFiles.asList(), new Function<OutputFile, String>() {

        @Override
        public String fun(OutputFile file) {
            return file.getRelativePath();
        }
    }, ", ");
    assertNotNull("Couldn't find class file for pattern " + classFileRegex + " in: " + pathsString, outputFile);
    ClassReader cr = new ClassReader(outputFile.asByteArray());
    List<LocalVariable> actualLocalVariables = readLocalVariable(cr, methodName);
    KotlinTestUtils.assertEqualsToFile(ktFile, text.substring(0, text.indexOf("// VARIABLE : ")) + getActualVariablesAsString(actualLocalVariables));
}
Also used : OutputFile(org.jetbrains.kotlin.backend.common.output.OutputFile) OutputFileCollection(org.jetbrains.kotlin.backend.common.output.OutputFileCollection) KtFile(org.jetbrains.kotlin.psi.KtFile) KtFile(org.jetbrains.kotlin.psi.KtFile) File(java.io.File) OutputFile(org.jetbrains.kotlin.backend.common.output.OutputFile)

Example 4 with OutputFile

use of org.jetbrains.kotlin.backend.common.output.OutputFile in project kotlin by JetBrains.

the class KotlinBytecodeToolWindow method getBytecodeForFile.

// public for tests
@NotNull
public static String getBytecodeForFile(@NotNull KtFile ktFile, @NotNull CompilerConfiguration configuration) {
    GenerationState state;
    try {
        state = compileSingleFile(ktFile, configuration);
    } catch (ProcessCanceledException e) {
        throw e;
    } catch (Exception e) {
        return printStackTraceToString(e);
    }
    StringBuilder answer = new StringBuilder();
    Collection<Diagnostic> diagnostics = state.getCollectedExtraJvmDiagnostics().all();
    if (!diagnostics.isEmpty()) {
        answer.append("// Backend Errors: \n");
        answer.append("// ================\n");
        for (Diagnostic diagnostic : diagnostics) {
            answer.append("// Error at ").append(diagnostic.getPsiFile().getName()).append(StringsKt.join(diagnostic.getTextRanges(), ",")).append(": ").append(DefaultErrorMessages.render(diagnostic)).append("\n");
        }
        answer.append("// ================\n\n");
    }
    OutputFileCollection outputFiles = state.getFactory();
    for (OutputFile outputFile : outputFiles.asList()) {
        answer.append("// ================");
        answer.append(outputFile.getRelativePath());
        answer.append(" =================\n");
        answer.append(outputFile.asText()).append("\n\n");
    }
    return answer.toString();
}
Also used : OutputFile(org.jetbrains.kotlin.backend.common.output.OutputFile) OutputFileCollection(org.jetbrains.kotlin.backend.common.output.OutputFileCollection) Diagnostic(org.jetbrains.kotlin.diagnostics.Diagnostic) GenerationState(org.jetbrains.kotlin.codegen.state.GenerationState) ProcessCanceledException(com.intellij.openapi.progress.ProcessCanceledException) ProcessCanceledException(com.intellij.openapi.progress.ProcessCanceledException) NotNull(org.jetbrains.annotations.NotNull)

Example 5 with OutputFile

use of org.jetbrains.kotlin.backend.common.output.OutputFile 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)

Aggregations

OutputFile (org.jetbrains.kotlin.backend.common.output.OutputFile)14 NotNull (org.jetbrains.annotations.NotNull)7 OutputFileCollection (org.jetbrains.kotlin.backend.common.output.OutputFileCollection)4 ClassReader (org.jetbrains.org.objectweb.asm.ClassReader)4 ClassVisitor (org.jetbrains.org.objectweb.asm.ClassVisitor)3 Main (com.android.dx.command.dexer.Main)1 ProcessCanceledException (com.intellij.openapi.progress.ProcessCanceledException)1 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 ReferenceType (com.sun.jdi.ReferenceType)1 File (java.io.File)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Manifest (java.util.jar.Manifest)1 TestOnly (org.jetbrains.annotations.TestOnly)1 GenerationState (org.jetbrains.kotlin.codegen.state.GenerationState)1 Diagnostic (org.jetbrains.kotlin.diagnostics.Diagnostic)1 KtFile (org.jetbrains.kotlin.psi.KtFile)1 MethodVisitor (org.jetbrains.org.objectweb.asm.MethodVisitor)1