Search in sources :

Example 1 with KtScript

use of org.jetbrains.kotlin.psi.KtScript in project kotlin by JetBrains.

the class KotlinBytecodeToolWindow method compileSingleFile.

@NotNull
public static GenerationState compileSingleFile(@NotNull final KtFile ktFile, @NotNull CompilerConfiguration configuration) {
    ResolutionFacade resolutionFacade = ResolutionUtils.getResolutionFacade(ktFile);
    BindingContext bindingContextForFile = resolutionFacade.analyzeFullyAndGetResult(Collections.singletonList(ktFile)).getBindingContext();
    kotlin.Pair<BindingContext, List<KtFile>> result = DebuggerUtils.INSTANCE.analyzeInlinedFunctions(resolutionFacade, ktFile, configuration.getBoolean(CommonConfigurationKeys.DISABLE_INLINE), bindingContextForFile);
    BindingContext bindingContext = result.getFirst();
    List<KtFile> toProcess = result.getSecond();
    GenerationState.GenerateClassFilter generateClassFilter = new GenerationState.GenerateClassFilter() {

        @Override
        public boolean shouldGeneratePackagePart(@NotNull KtFile file) {
            return file == ktFile;
        }

        @Override
        public boolean shouldAnnotateClass(@NotNull KtClassOrObject processingClassOrObject) {
            return true;
        }

        @Override
        public boolean shouldGenerateClass(@NotNull KtClassOrObject processingClassOrObject) {
            return processingClassOrObject.getContainingKtFile() == ktFile;
        }

        @Override
        public boolean shouldGenerateScript(@NotNull KtScript script) {
            return script.getContainingKtFile() == ktFile;
        }
    };
    GenerationState state = new GenerationState(ktFile.getProject(), ClassBuilderFactories.TEST, resolutionFacade.getModuleDescriptor(), bindingContext, toProcess, configuration, generateClassFilter);
    KotlinCodegenFacade.compileCorrectFiles(state, CompilationErrorHandler.THROW_EXCEPTION);
    return state;
}
Also used : KtClassOrObject(org.jetbrains.kotlin.psi.KtClassOrObject) KtScript(org.jetbrains.kotlin.psi.KtScript) BindingContext(org.jetbrains.kotlin.resolve.BindingContext) NotNull(org.jetbrains.annotations.NotNull) GenerationState(org.jetbrains.kotlin.codegen.state.GenerationState) ResolutionFacade(org.jetbrains.kotlin.idea.resolve.ResolutionFacade) List(java.util.List) KtFile(org.jetbrains.kotlin.psi.KtFile) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with KtScript

use of org.jetbrains.kotlin.psi.KtScript 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

GenerationState (org.jetbrains.kotlin.codegen.state.GenerationState)2 KtScript (org.jetbrains.kotlin.psi.KtScript)2 Disposable (com.intellij.openapi.Disposable)1 File (java.io.File)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 NotNull (org.jetbrains.annotations.NotNull)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 CompilerConfiguration (org.jetbrains.kotlin.config.CompilerConfiguration)1 KotlinSourceRoot (org.jetbrains.kotlin.config.KotlinSourceRoot)1 ResolutionFacade (org.jetbrains.kotlin.idea.resolve.ResolutionFacade)1 FqName (org.jetbrains.kotlin.name.FqName)1 KtClassOrObject (org.jetbrains.kotlin.psi.KtClassOrObject)1 KtFile (org.jetbrains.kotlin.psi.KtFile)1 BindingContext (org.jetbrains.kotlin.resolve.BindingContext)1