Search in sources :

Example 1 with GeneratedClassLoader

use of org.jetbrains.kotlin.codegen.GeneratedClassLoader in project kotlin by JetBrains.

the class ExecuteKotlinScriptMojo method executeScriptFile.

private void executeScriptFile(File scriptFile) throws MojoExecutionException {
    initCompiler();
    Disposable rootDisposable = Disposer.newDisposable();
    try {
        MavenPluginLogMessageCollector messageCollector = new MavenPluginLogMessageCollector(getLog());
        CompilerConfiguration configuration = new CompilerConfiguration();
        configuration.put(CLIConfigurationKeys.MESSAGE_COLLECTOR_KEY, messageCollector);
        List<File> deps = new ArrayList<File>();
        deps.addAll(PathUtil.getJdkClassesRoots());
        deps.addAll(getDependenciesForScript());
        for (File item : deps) {
            if (item.exists()) {
                configuration.add(JVMConfigurationKeys.CONTENT_ROOTS, new JvmClasspathRoot(item));
                getLog().debug("Adding to classpath: " + item.getAbsolutePath());
            } else {
                getLog().debug("Skipping non-existing dependency: " + item.getAbsolutePath());
            }
        }
        configuration.add(JVMConfigurationKeys.CONTENT_ROOTS, new KotlinSourceRoot(scriptFile.getAbsolutePath()));
        configuration.put(CommonConfigurationKeys.MODULE_NAME, JvmAbi.DEFAULT_MODULE_NAME);
        K2JVMCompiler.Companion.configureScriptDefinitions(scriptTemplates.toArray(new String[scriptTemplates.size()]), configuration, messageCollector, new HashMap<String, Object>());
        KotlinCoreEnvironment environment = KotlinCoreEnvironment.createForProduction(rootDisposable, configuration, EnvironmentConfigFiles.JVM_CONFIG_FILES);
        GenerationState state = KotlinToJVMBytecodeCompiler.INSTANCE.analyzeAndGenerate(environment);
        if (state == null) {
            throw new ScriptExecutionException(scriptFile, "compile error");
        }
        GeneratedClassLoader classLoader = new GeneratedClassLoader(state.getFactory(), getClass().getClassLoader());
        KtScript script = environment.getSourceFiles().get(0).getScript();
        FqName nameForScript = script.getFqName();
        try {
            Class<?> klass = classLoader.loadClass(nameForScript.asString());
            ExecuteKotlinScriptMojo.INSTANCE = this;
            if (ReflectionUtilKt.tryConstructClassFromStringArgs(klass, scriptArguments) == null)
                throw new ScriptExecutionException(scriptFile, "unable to construct script");
        } catch (ClassNotFoundException e) {
            throw new ScriptExecutionException(scriptFile, "internal error", e);
        }
    } finally {
        rootDisposable.dispose();
        ExecuteKotlinScriptMojo.INSTANCE = null;
    }
}
Also used : Disposable(com.intellij.openapi.Disposable) KtScript(org.jetbrains.kotlin.psi.KtScript) KotlinSourceRoot(org.jetbrains.kotlin.config.KotlinSourceRoot) FqName(org.jetbrains.kotlin.name.FqName) ArrayList(java.util.ArrayList) GenerationState(org.jetbrains.kotlin.codegen.state.GenerationState) JvmClasspathRoot(org.jetbrains.kotlin.cli.jvm.config.JvmClasspathRoot) GeneratedClassLoader(org.jetbrains.kotlin.codegen.GeneratedClassLoader) KotlinCoreEnvironment(org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment) CompilerConfiguration(org.jetbrains.kotlin.config.CompilerConfiguration) File(java.io.File)

Aggregations

Disposable (com.intellij.openapi.Disposable)1 File (java.io.File)1 ArrayList (java.util.ArrayList)1 KotlinCoreEnvironment (org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment)1 JvmClasspathRoot (org.jetbrains.kotlin.cli.jvm.config.JvmClasspathRoot)1 GeneratedClassLoader (org.jetbrains.kotlin.codegen.GeneratedClassLoader)1 GenerationState (org.jetbrains.kotlin.codegen.state.GenerationState)1 CompilerConfiguration (org.jetbrains.kotlin.config.CompilerConfiguration)1 KotlinSourceRoot (org.jetbrains.kotlin.config.KotlinSourceRoot)1 FqName (org.jetbrains.kotlin.name.FqName)1 KtScript (org.jetbrains.kotlin.psi.KtScript)1