示例#1
0
  private CompilerConfiguration createConfiguration(
      @Nullable String stdlib,
      @Nullable String[] classpath,
      @Nullable String[] externalAnnotationsPath,
      @NotNull String[] sourceRoots,
      boolean enableInline,
      boolean enableOptimization) {
    KotlinPaths paths = getKotlinPathsForAntTask();
    CompilerConfiguration configuration = new CompilerConfiguration();
    configuration.addAll(CLASSPATH_KEY, PathUtil.getJdkClassesRoots());
    if ((stdlib != null) && (stdlib.trim().length() > 0)) {
      configuration.add(CLASSPATH_KEY, new File(stdlib));
    } else {
      File path = paths.getRuntimePath();
      if (path.exists()) {
        configuration.add(CLASSPATH_KEY, path);
      }
    }
    if ((classpath != null) && (classpath.length > 0)) {
      for (String path : classpath) {
        configuration.add(CLASSPATH_KEY, new File(path));
      }
    }
    if ((externalAnnotationsPath != null) && (externalAnnotationsPath.length > 0)) {
      for (String path : externalAnnotationsPath) {
        configuration.add(ANNOTATIONS_PATH_KEY, new File(path));
      }
    }
    File jdkAnnotationsPath = paths.getJdkAnnotationsPath();
    if (jdkAnnotationsPath.exists()) {
      configuration.add(ANNOTATIONS_PATH_KEY, jdkAnnotationsPath);
    }

    configuration.addAll(CommonConfigurationKeys.SOURCE_ROOTS_KEY, Arrays.asList(sourceRoots));
    for (String sourceRoot : sourceRoots) {
      File file = new File(sourceRoot);
      if (!file.isFile() || !"kt".equals(FileUtilRt.getExtension(file.getName()))) {
        configuration.add(JVMConfigurationKeys.CLASSPATH_KEY, file);
      }
    }
    configuration.put(
        CLIConfigurationKeys.MESSAGE_COLLECTOR_KEY,
        MessageCollectorPlainTextToStream.PLAIN_TEXT_TO_SYSTEM_ERR);

    configuration.put(ENABLE_INLINE, enableInline);
    configuration.put(ENABLE_OPTIMIZATION, enableOptimization);

    // lets register any compiler plugins
    configuration.addAll(CLIConfigurationKeys.COMPILER_PLUGINS, getCompilerPlugins());
    return configuration;
  }
  @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;
  }
  private KotlinEnvironment(@NotNull IJavaProject javaProject) {
    this.javaProject = javaProject;

    applicationEnvironment = createJavaCoreApplicationEnvironment();

    projectEnvironment = new JavaCoreProjectEnvironment(DISPOSABLE, applicationEnvironment);

    project = projectEnvironment.getProject();

    CoreExternalAnnotationsManager annotationsManager =
        new CoreExternalAnnotationsManager(project.getComponent(PsiManager.class));
    project.registerService(ExternalAnnotationsManager.class, annotationsManager);
    project.registerService(
        CoreJavaFileManager.class,
        (CoreJavaFileManager) ServiceManager.getService(project, JavaFileManager.class));

    CliLightClassGenerationSupport cliLightClassGenerationSupport =
        new CliLightClassGenerationSupport();
    project.registerService(LightClassGenerationSupport.class, cliLightClassGenerationSupport);
    project.registerService(CliLightClassGenerationSupport.class, cliLightClassGenerationSupport);
    project.registerService(
        KotlinLightClassForPackage.FileStubCache.class,
        new KotlinLightClassForPackage.FileStubCache(project));

    VirtualFile ktJDKAnnotations =
        PathUtil.jarFileOrDirectoryToVirtualFile(new File(KT_JDK_ANNOTATIONS_PATH));
    annotationsManager.addExternalAnnotationsRoot(ktJDKAnnotations);

    addJreClasspath();
    addSourcesToClasspath();
    addLibsToClasspath();

    project.registerService(
        VirtualFileFinderFactory.class, new EclipseVirtualFileFinder(classPath));

    CoreApplicationEnvironment.registerExtensionPoint(
        Extensions.getRootArea(),
        ClsCustomNavigationPolicy.EP_NAME,
        ClsCustomNavigationPolicy.class);
    CoreApplicationEnvironment.registerExtensionPoint(
        Extensions.getRootArea(),
        ClassFileDecompilers.EP_NAME,
        ClassFileDecompilers.Decompiler.class);

    cachedEnvironment.put(javaProject, this);
  }
示例#4
0
 private static KotlinPaths getKotlinPathsForAntTask() {
   return new KotlinPathsFromHomeDir(
       PathUtil.getJarPathForClass(BytecodeCompiler.class).getParentFile().getParentFile());
 }