@Nullable private static AnalyzeExhaust analyze( final K2JVMCompileEnvironmentConfiguration configuration, boolean stubs) { final JetCoreEnvironment environment = configuration.getEnvironment(); AnalyzerWithCompilerReport analyzerWithCompilerReport = new AnalyzerWithCompilerReport(configuration.getMessageCollector()); final Predicate<PsiFile> filesToAnalyzeCompletely = stubs ? Predicates.<PsiFile>alwaysFalse() : Predicates.<PsiFile>alwaysTrue(); analyzerWithCompilerReport.analyzeAndReport( new Function0<AnalyzeExhaust>() { @NotNull @Override public AnalyzeExhaust invoke() { return AnalyzerFacadeForJVM.analyzeFilesWithJavaIntegration( environment.getProject(), environment.getSourceFiles(), filesToAnalyzeCompletely, JetControlFlowDataTraceFactory.EMPTY, configuration.getEnvironment().getCompilerDependencies()); } }, environment.getSourceFiles()); return analyzerWithCompilerReport.hasErrors() ? null : analyzerWithCompilerReport.getAnalyzeExhaust(); }
@NotNull @Override public List<JetFile> allInScope(@NotNull GlobalSearchScope scope) { List<JetFile> answer = new ArrayList<JetFile>(); for (JetFile file : environment.getSourceFiles()) { if (scope.contains(file.getVirtualFile())) { answer.add(file); } } return answer; }
@NotNull public static List<Module> loadModuleScript( String moduleScriptFile, MessageCollector messageCollector) { Disposable disposable = new Disposable() { @Override public void dispose() {} }; CompilerConfiguration configuration = new CompilerConfiguration(); File defaultRuntimePath = CompilerPathUtil.getRuntimePath(); if (defaultRuntimePath != null) { configuration.add(JVMConfigurationKeys.CLASSPATH_KEY, defaultRuntimePath); } configuration.add(JVMConfigurationKeys.CLASSPATH_KEY, PathUtil.findRtJar()); File jdkAnnotationsPath = CompilerPathUtil.getJdkAnnotationsPath(); if (jdkAnnotationsPath != null) { configuration.add(JVMConfigurationKeys.ANNOTATIONS_PATH_KEY, jdkAnnotationsPath); } configuration.add(CommonConfigurationKeys.SOURCE_ROOTS_KEY, moduleScriptFile); JetCoreEnvironment scriptEnvironment = JetCoreEnvironment.createCoreEnvironmentForJVM(disposable, configuration); GenerationState generationState = KotlinToJVMBytecodeCompiler.analyzeAndGenerate( new K2JVMCompileEnvironmentConfiguration( scriptEnvironment, messageCollector, false, BuiltinsScopeExtensionMode.ALL, false, BuiltinToJavaTypesMapping.ENABLED), false); if (generationState == null) { throw new CompileEnvironmentException( "Module script " + moduleScriptFile + " analyze failed"); } List<Module> modules = runDefineModules(moduleScriptFile, generationState.getFactory()); Disposer.dispose(disposable); if (modules == null) { throw new CompileEnvironmentException( "Module script " + moduleScriptFile + " compilation failed"); } if (modules.isEmpty()) { throw new CompileEnvironmentException("No modules where defined by " + moduleScriptFile); } return modules; }
@NotNull private static GenerationState generate( final K2JVMCompileEnvironmentConfiguration configuration, AnalyzeExhaust exhaust, boolean stubs) { JetCoreEnvironment environment = configuration.getEnvironment(); Project project = environment.getProject(); Progress backendProgress = new Progress() { @Override public void log(String message) { configuration .getMessageCollector() .report( CompilerMessageSeverity.LOGGING, message, CompilerMessageLocation.NO_LOCATION); } }; GenerationState generationState = new GenerationState( project, ClassBuilderFactories.binaries(stubs), backendProgress, exhaust, environment.getSourceFiles(), configuration.getEnvironment().getCompilerDependencies().getCompilerSpecialMode()); generationState.compileCorrectFiles(CompilationErrorHandler.THROW_EXCEPTION); List<CompilerPlugin> plugins = configuration.getCompilerPlugins(); if (plugins != null) { CompilerPluginContext context = new CompilerPluginContext( project, exhaust.getBindingContext(), environment.getSourceFiles()); for (CompilerPlugin plugin : plugins) { plugin.processFiles(context); } } return generationState; }
/** * Creates new instance of {@link JetCoreEnvironment} instance using the arguments specified. * * @param stdlib path to "kotlin-runtime.jar", only used if not null and not empty * @param classpath compilation classpath, only used if not null and not empty * @param sourceRoots * @param enableInline * @param enableOptimization * @return compile environment instance */ private JetCoreEnvironment env( String stdlib, String[] classpath, String[] externalAnnotationsPath, String[] sourceRoots, boolean enableInline, boolean enableOptimization) { CompilerConfiguration configuration = createConfiguration( stdlib, classpath, externalAnnotationsPath, sourceRoots, enableInline, enableOptimization); return JetCoreEnvironment.createForProduction(Disposer.newDisposable(), configuration); }
@Override public boolean isFileInScope(@NotNull JetFile file, @NotNull GlobalSearchScope scope) { return scope.contains(file.getVirtualFile()) && environment.getSourceFiles().contains(file); }
@Override public Collection<JetFile> fun(JetFile file) { return environment.getSourceFiles(); }