Search in sources :

Example 11 with OutputFile

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

the class CompileEnvironmentUtil method doWriteToJar.

// TODO: includeRuntime should be not a flag but a path to runtime
private static void doWriteToJar(OutputFileCollection outputFiles, OutputStream fos, @Nullable FqName mainClass, boolean includeRuntime) {
    try {
        Manifest manifest = new Manifest();
        Attributes mainAttributes = manifest.getMainAttributes();
        mainAttributes.putValue("Manifest-Version", "1.0");
        mainAttributes.putValue("Created-By", "JetBrains Kotlin");
        if (mainClass != null) {
            mainAttributes.putValue("Main-Class", mainClass.asString());
        }
        JarOutputStream stream = new JarOutputStream(fos, manifest);
        for (OutputFile outputFile : outputFiles.asList()) {
            stream.putNextEntry(new JarEntry(outputFile.getRelativePath()));
            stream.write(outputFile.asByteArray());
        }
        if (includeRuntime) {
            writeRuntimeToJar(stream);
        }
        stream.finish();
    } catch (IOException e) {
        throw new CompileEnvironmentException("Failed to generate jar file", e);
    }
}
Also used : OutputFile(org.jetbrains.kotlin.backend.common.output.OutputFile)

Example 12 with OutputFile

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

the class InlineCodegenUtil method buildClassReaderByInternalName.

@NotNull
static /* package */
ClassReader buildClassReaderByInternalName(@NotNull GenerationState state, @NotNull String internalName) {
    //try to find just compiled classes then in dependencies
    try {
        OutputFile outputFile = state.getFactory().get(internalName + ".class");
        if (outputFile != null) {
            return new ClassReader(outputFile.asByteArray());
        }
        VirtualFile file = findVirtualFileImprecise(state, internalName);
        if (file != null) {
            return new ClassReader(file.contentsToByteArray());
        }
        throw new RuntimeException("Couldn't find virtual file for " + internalName);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : OutputFile(org.jetbrains.kotlin.backend.common.output.OutputFile) VirtualFile(com.intellij.openapi.vfs.VirtualFile) IOException(java.io.IOException) NotNull(org.jetbrains.annotations.NotNull)

Example 13 with OutputFile

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

the class ClassFileFactory method createText.

@NotNull
@TestOnly
public String createText() {
    StringBuilder answer = new StringBuilder();
    for (OutputFile file : asList()) {
        answer.append("@").append(file.getRelativePath()).append('\n');
        answer.append(file.asText());
    }
    return answer.toString();
}
Also used : OutputFile(org.jetbrains.kotlin.backend.common.output.OutputFile) TestOnly(org.jetbrains.annotations.TestOnly) NotNull(org.jetbrains.annotations.NotNull)

Example 14 with OutputFile

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

the class GeneratedClassLoader method findClass.

@NotNull
@Override
protected Class<?> findClass(@NotNull String name) throws ClassNotFoundException {
    String classFilePath = name.replace('.', '/') + ".class";
    OutputFile outputFile = factory.get(classFilePath);
    if (outputFile != null) {
        byte[] bytes = outputFile.asByteArray();
        int lastDot = name.lastIndexOf('.');
        if (lastDot >= 0) {
            String pkgName = name.substring(0, lastDot);
            if (getPackage(pkgName) == null) {
                definePackage(pkgName, new Manifest(), null);
            }
        }
        return defineClass(name, bytes, 0, bytes.length);
    }
    return super.findClass(name);
}
Also used : OutputFile(org.jetbrains.kotlin.backend.common.output.OutputFile) Manifest(java.util.jar.Manifest) NotNull(org.jetbrains.annotations.NotNull)

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