Search in sources :

Example 1 with OutputFileCollection

use of org.jetbrains.kotlin.backend.common.output.OutputFileCollection 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 2 with OutputFileCollection

use of org.jetbrains.kotlin.backend.common.output.OutputFileCollection 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 3 with OutputFileCollection

use of org.jetbrains.kotlin.backend.common.output.OutputFileCollection 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 4 with OutputFileCollection

use of org.jetbrains.kotlin.backend.common.output.OutputFileCollection 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 5 with OutputFileCollection

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

the class GenerateNotNullAssertionsTest method testNoAssertionsForKotlinFromBinary.

public void testNoAssertionsForKotlinFromBinary() throws Exception {
    setUpEnvironment(false, true);
    loadSource("noAssertionsForKotlin.kt");
    OutputFileCollection outputFiles = generateClassesInFile();
    File compiledDirectory = new File(FileUtil.getTempDirectory(), "kotlin-classes");
    OutputUtilsKt.writeAllTo(outputFiles, compiledDirectory);
    setUpEnvironment(false, true, compiledDirectory);
    loadSource("noAssertionsForKotlinMain.kt");
    assertNoIntrinsicsMethodIsCalledInMyClasses(false);
}
Also used : OutputFileCollection(org.jetbrains.kotlin.backend.common.output.OutputFileCollection) KtFile(org.jetbrains.kotlin.psi.KtFile) File(java.io.File) OutputFile(org.jetbrains.kotlin.backend.common.output.OutputFile)

Aggregations

OutputFileCollection (org.jetbrains.kotlin.backend.common.output.OutputFileCollection)7 OutputFile (org.jetbrains.kotlin.backend.common.output.OutputFile)5 File (java.io.File)4 KtFile (org.jetbrains.kotlin.psi.KtFile)4 NotNull (org.jetbrains.annotations.NotNull)3 VirtualFile (com.intellij.openapi.vfs.VirtualFile)2 K2JSTranslator (org.jetbrains.kotlin.js.facade.K2JSTranslator)2 TranslationResult (org.jetbrains.kotlin.js.facade.TranslationResult)2 ClassReader (org.jetbrains.org.objectweb.asm.ClassReader)2 ProcessCanceledException (com.intellij.openapi.progress.ProcessCanceledException)1 Project (com.intellij.openapi.project.Project)1 PsiFile (com.intellij.psi.PsiFile)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 PrintStream (java.io.PrintStream)1 AnalysisResult (org.jetbrains.kotlin.analyzer.AnalysisResult)1 AnalyzerWithCompilerReport (org.jetbrains.kotlin.cli.common.messages.AnalyzerWithCompilerReport)1 MessageCollector (org.jetbrains.kotlin.cli.common.messages.MessageCollector)1 PrintingMessageCollector (org.jetbrains.kotlin.cli.common.messages.PrintingMessageCollector)1 KotlinCoreEnvironment (org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment)1 GenerationState (org.jetbrains.kotlin.codegen.state.GenerationState)1