Exemple #1
0
  public static void compileScript(
      @NotNull JetScript script,
      @NotNull Type classType,
      @NotNull List<Pair<ScriptDescriptor, Type>> earlierScripts,
      @NotNull GenerationState state,
      @NotNull CompilationErrorHandler errorHandler) {
    registerEarlierScripts(state, earlierScripts);
    registerClassNameForScript(state.getBindingTrace(), script, classType);

    state.beforeCompile();
    KotlinCodegenFacade.generatePackage(
        state,
        script.getContainingJetFile().getPackageFqName(),
        Collections.singleton(script.getContainingJetFile()),
        errorHandler);
  }
    @NotNull
    @Override
    protected String processRequest(@NotNull Location location) {
      JetFile jetFile = location.getJetFile();
      assert jetFile != null;

      GenerationState state;
      try {
        AnalyzeExhaust exhaust = AnalyzerFacadeWithCache.analyzeFileWithCache(jetFile);
        if (exhaust.isError()) {
          return printStackTraceToString(exhaust.getError());
        }
        state =
            new GenerationState(
                jetFile.getProject(),
                ClassBuilderFactories.TEST,
                Progress.DEAF,
                exhaust.getBindingContext(),
                Collections.singletonList(jetFile),
                true,
                true,
                GenerationState.GenerateClassFilter.GENERATE_ALL,
                enableInline.isSelected());
        KotlinCodegenFacade.compileCorrectFiles(state, CompilationErrorHandler.THROW_EXCEPTION);
      } catch (ProcessCanceledException e) {
        throw e;
      } catch (Exception e) {
        return printStackTraceToString(e);
      }

      StringBuilder answer = new StringBuilder();

      OutputFileCollection outputFiles = state.getFactory();
      for (OutputFile outputFile : outputFiles.asList()) {
        answer.append("// ================");
        answer.append(outputFile.getRelativePath());
        answer.append(" =================\n");
        answer.append(outputFile.asText()).append("\n\n");
      }

      return answer.toString();
    }