Ejemplo n.º 1
0
  @NotNull
  public static NamespaceDescriptor compileJava(
      @NotNull Collection<File> javaFiles, File tmpdir, Disposable disposable) throws IOException {
    JavaCompiler javaCompiler = ToolProvider.getSystemJavaCompiler();

    StandardJavaFileManager fileManager =
        javaCompiler.getStandardFileManager(null, Locale.ENGLISH, Charset.forName("utf-8"));
    try {
      Iterable<? extends JavaFileObject> javaFileObjectsFromFiles =
          fileManager.getJavaFileObjectsFromFiles(javaFiles);
      List<String> options =
          Arrays.asList(
              "-classpath",
              "out/production/runtime"
                  + File.pathSeparator
                  + JetTestUtils.getAnnotationsJar().getPath(),
              "-d",
              tmpdir.getPath());
      JavaCompiler.CompilationTask task =
          javaCompiler.getTask(null, fileManager, null, options, null, javaFileObjectsFromFiles);

      Assert.assertTrue(task.call());
    } finally {
      fileManager.close();
    }

    JetCoreEnvironment jetCoreEnvironment =
        new JetCoreEnvironment(
            disposable,
            CompileCompilerDependenciesTest.compilerConfigurationForTests(
                ConfigurationKind.JDK_ONLY,
                TestJdkKind.MOCK_JDK,
                JetTestUtils.getAnnotationsJar(),
                tmpdir,
                new File("out/production/runtime")));

    InjectorForJavaSemanticServices injector =
        new InjectorForJavaSemanticServices(
            BuiltinsScopeExtensionMode.ALL, jetCoreEnvironment.getProject());
    JavaDescriptorResolver javaDescriptorResolver = injector.getJavaDescriptorResolver();
    return javaDescriptorResolver.resolveNamespace(
        FqName.topLevel(Name.identifier("test")), DescriptorSearchRule.ERROR_IF_FOUND_IN_KOTLIN);
  }
  private void blackBoxFileAgainstJavaByFullPath(@NotNull String ktFileFullPath) {
    String ktFile = relativePath(new File(ktFileFullPath));
    File javaClassesTempDirectory = compileJava(ktFile.replaceFirst("\\.kt$", ".java"));

    myEnvironment =
        JetCoreEnvironment.createForTests(
            getTestRootDisposable(),
            JetTestUtils.compilerConfigurationForTests(
                ConfigurationKind.ALL,
                TestJdkKind.FULL_JDK,
                JetTestUtils.getAnnotationsJar(),
                javaClassesTempDirectory));

    loadFile(ktFile);
    blackBox();
  }
Ejemplo n.º 3
0
  @NotNull
  public static File compileJava(@NotNull String filename) {
    try {
      File javaClassesTempDirectory = JetTestUtils.tmpDir("java-classes");
      String classPath =
          ForTestCompileRuntime.runtimeJarForTests()
              + File.pathSeparator
              + JetTestUtils.getAnnotationsJar().getPath();
      List<String> options =
          Arrays.asList("-classpath", classPath, "-d", javaClassesTempDirectory.getPath());

      File javaFile = new File("compiler/testData/codegen/" + filename);
      JetTestUtils.compileJavaFiles(Collections.singleton(javaFile), options);

      return javaClassesTempDirectory;
    } catch (IOException e) {
      throw UtilsPackage.rethrow(e);
    }
  }
  private void blackBoxFileWithJavaByFullPath(@NotNull String directory) throws Exception {
    File dirFile = new File(directory);

    final List<String> javaFilePaths = new ArrayList<String>();
    final List<String> ktFilePaths = new ArrayList<String>();
    FileUtil.processFilesRecursively(
        dirFile,
        new Processor<File>() {
          @Override
          public boolean process(File file) {
            String path = relativePath(file);
            if (path.endsWith(".kt")) {
              ktFilePaths.add(path);
            } else if (path.endsWith(".java")) {
              javaFilePaths.add(path);
            }
            return true;
          }
        });

    CompilerConfiguration configuration =
        JetTestUtils.compilerConfigurationForTests(
            ConfigurationKind.ALL, TestJdkKind.FULL_JDK, JetTestUtils.getAnnotationsJar());
    configuration.add(JVMConfigurationKeys.CLASSPATH_KEY, dirFile);
    myEnvironment = JetCoreEnvironment.createForTests(getTestRootDisposable(), configuration);
    loadFiles(ArrayUtil.toStringArray(ktFilePaths));
    classFileFactory =
        GenerationUtils.compileManyFilesGetGenerationStateForTest(
                myEnvironment.getProject(), myFiles.getPsiFiles())
            .getFactory();
    File kotlinOut = JetTestUtils.tmpDir(toString());
    OutputUtilsPackage.writeAllTo(classFileFactory, kotlinOut);

    // TODO: support several Java sources
    File javaOut = compileJava(KotlinPackage.single(javaFilePaths), kotlinOut.getPath());
    // Add javac output to classpath so that the created class loader can find generated Java
    // classes
    configuration.add(JVMConfigurationKeys.CLASSPATH_KEY, javaOut);

    blackBox();
  }