Java Code Examples for com.sun.tools.javac.file.JavacFileManager#setLocationFromPaths()

The following examples show how to use com.sun.tools.javac.file.JavacFileManager#setLocationFromPaths() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JavacTurbineTest.java    From bazel with Apache License 2.0 5 votes vote down vote up
private void compileLib(
    Path jar, Collection<Path> classpath, Iterable<? extends JavaFileObject> units)
    throws IOException {
  final Path outdir = temp.newFolder().toPath();
  JavacFileManager fm = new JavacFileManager(new Context(), false, UTF_8);
  fm.setLocationFromPaths(StandardLocation.CLASS_OUTPUT, Collections.singleton(outdir));
  fm.setLocationFromPaths(StandardLocation.CLASS_PATH, classpath);
  List<String> options = ImmutableList.of("-d", outdir.toString());
  JavacTool tool = JavacTool.create();

  JavacTask task =
      tool.getTask(
          new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.err, UTF_8)), true),
          fm,
          null,
          options,
          null,
          units);
  assertThat(task.call()).isTrue();

  try (JarOutputStream jos = new JarOutputStream(Files.newOutputStream(jar))) {
    Files.walkFileTree(
        outdir,
        new SimpleFileVisitor<Path>() {
          @Override
          public FileVisitResult visitFile(Path path, BasicFileAttributes attrs)
              throws IOException {
            JarEntry je = new JarEntry(outdir.relativize(path).toString());
            jos.putNextEntry(je);
            Files.copy(path, jos);
            return FileVisitResult.CONTINUE;
          }
        });
  }
}
 
Example 2
Source File: IntegrationTestSupport.java    From turbine with Apache License 2.0 4 votes vote down vote up
private static JavacTask setupJavac(
    Map<String, String> sources,
    Collection<Path> classpath,
    ImmutableList<String> options,
    DiagnosticCollector<JavaFileObject> collector,
    FileSystem fs,
    Path out)
    throws IOException {
  Path srcs = fs.getPath("srcs");

  Files.createDirectories(out);

  ArrayList<Path> inputs = new ArrayList<>();
  for (Map.Entry<String, String> entry : sources.entrySet()) {
    Path path = srcs.resolve(entry.getKey());
    if (path.getParent() != null) {
      Files.createDirectories(path.getParent());
    }
    MoreFiles.asCharSink(path, UTF_8).write(entry.getValue());
    inputs.add(path);
  }

  JavacTool compiler = JavacTool.create();
  JavacFileManager fileManager = new JavacFileManager(new Context(), true, UTF_8);
  fileManager.setLocationFromPaths(StandardLocation.CLASS_OUTPUT, ImmutableList.of(out));
  fileManager.setLocationFromPaths(StandardLocation.CLASS_PATH, classpath);
  fileManager.setLocationFromPaths(StandardLocation.locationFor("MODULE_PATH"), classpath);
  if (inputs.stream().filter(i -> i.getFileName().toString().equals("module-info.java")).count()
      > 1) {
    // multi-module mode
    fileManager.setLocationFromPaths(
        StandardLocation.locationFor("MODULE_SOURCE_PATH"), ImmutableList.of(srcs));
  }

  return compiler.getTask(
      new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.err, UTF_8)), true),
      fileManager,
      collector,
      options,
      ImmutableList.of(),
      fileManager.getJavaFileObjectsFromPaths(inputs));
}
 
Example 3
Source File: ClassWriterTest.java    From turbine with Apache License 2.0 4 votes vote down vote up
@Test
public void roundTrip() throws Exception {
  FileSystem fs = Jimfs.newFileSystem(Configuration.unix());
  Path path = fs.getPath("test/Test.java");
  Files.createDirectories(path.getParent());
  Files.write(
      path,
      ImmutableList.of(
          "package test;",
          "import java.util.List;",
          "class Test<T extends String> implements Runnable {", //
          "  public void run() {}",
          "  public <T extends Exception> void f() throws T {}",
          "  public static int X;",
          "  class Inner {}",
          "}"),
      UTF_8);
  Path out = fs.getPath("out");
  Files.createDirectories(out);

  JavacFileManager fileManager = new JavacFileManager(new Context(), false, UTF_8);
  fileManager.setLocationFromPaths(StandardLocation.CLASS_OUTPUT, ImmutableList.of(out));
  DiagnosticCollector<JavaFileObject> collector = new DiagnosticCollector<>();
  JavacTask task =
      JavacTool.create()
          .getTask(
              new PrintWriter(
                  new BufferedWriter(new OutputStreamWriter(System.err, UTF_8)), true),
              fileManager,
              collector,
              ImmutableList.of("-source", "8", "-target", "8"),
              /* classes= */ null,
              fileManager.getJavaFileObjects(path));

  assertWithMessage(collector.getDiagnostics().toString()).that(task.call()).isTrue();

  byte[] original = Files.readAllBytes(out.resolve("test/Test.class"));
  byte[] actual = ClassWriter.writeClass(ClassReader.read(null, original));

  assertThat(AsmUtils.textify(original, /* skipDebug= */ true))
      .isEqualTo(AsmUtils.textify(actual, /* skipDebug= */ true));
}
 
Example 4
Source File: BlazeJavacMain.java    From bazel with Apache License 2.0 4 votes vote down vote up
private static void setLocations(JavacFileManager fileManager, BlazeJavacArguments arguments) {
  try {
    fileManager.setLocationFromPaths(StandardLocation.CLASS_PATH, arguments.classPath());
    // modular dependencies must be on the module path, not the classpath
    fileManager.setLocationFromPaths(
        StandardLocation.locationFor("MODULE_PATH"), arguments.classPath());

    fileManager.setLocationFromPaths(
        StandardLocation.CLASS_OUTPUT, ImmutableList.of(arguments.classOutput()));
    if (arguments.nativeHeaderOutput() != null) {
      fileManager.setLocationFromPaths(
          StandardLocation.NATIVE_HEADER_OUTPUT,
          ImmutableList.of(arguments.nativeHeaderOutput()));
    }

    ImmutableList<Path> sourcePath = arguments.sourcePath();
    if (sourcePath.isEmpty()) {
      // javac expects a module-info-relative source path to be set when compiling modules,
      // otherwise it reports an error:
      // "file should be on source path, or on patch path for module"
      ImmutableList<Path> moduleInfos =
          arguments.sourceFiles().stream()
              .filter(f -> f.getFileName().toString().equals("module-info.java"))
              .collect(toImmutableList());
      if (moduleInfos.size() == 1) {
        sourcePath = ImmutableList.of(getOnlyElement(moduleInfos).getParent());
      }
    }
    fileManager.setLocationFromPaths(StandardLocation.SOURCE_PATH, sourcePath);

    Path system = arguments.system();
    if (system != null) {
      fileManager.setLocationFromPaths(
          StandardLocation.locationFor("SYSTEM_MODULES"), ImmutableList.of(system));
    }
    // The bootclasspath may legitimately be empty if --release is being used.
    Collection<Path> bootClassPath = arguments.bootClassPath();
    if (!bootClassPath.isEmpty()) {
      fileManager.setLocationFromPaths(StandardLocation.PLATFORM_CLASS_PATH, bootClassPath);
    }
    fileManager.setLocationFromPaths(
        StandardLocation.ANNOTATION_PROCESSOR_PATH, arguments.processorPath());
    if (arguments.sourceOutput() != null) {
      fileManager.setLocationFromPaths(
          StandardLocation.SOURCE_OUTPUT, ImmutableList.of(arguments.sourceOutput()));
    }
  } catch (IOException e) {
    throw new IOError(e);
  }
}