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;
}
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;
}
}
Aggregations