Example #1
0
  public static void main(String... args) throws Throwable {
    String testSrcDir = System.getProperty("test.src");
    String testClassDir = System.getProperty("test.classes");
    String self = T6361619.class.getName();

    JavacTool tool = JavacTool.create();

    final PrintWriter out = new PrintWriter(System.err, true);

    Iterable<String> flags =
        Arrays.asList(
            "-processorpath", testClassDir,
            "-processor", self,
            "-d", ".");
    DiagnosticListener<JavaFileObject> dl =
        new DiagnosticListener<JavaFileObject>() {
          public void report(Diagnostic<? extends JavaFileObject> m) {
            out.println(m);
          }
        };

    StandardJavaFileManager fm = tool.getStandardFileManager(dl, null, null);
    Iterable<? extends JavaFileObject> f =
        fm.getJavaFileObjectsFromFiles(Arrays.asList(new File(testSrcDir, self + ".java")));

    JavacTask task = tool.getTask(out, fm, dl, flags, null, f);
    MyTaskListener tl = new MyTaskListener(task);
    task.setTaskListener(tl);

    // should complete, without exceptions
    task.call();
  }
Example #2
0
  void run() throws IOException {
    JavacTool tool = (JavacTool) ToolProvider.getSystemJavaCompiler();
    StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null);

    File classes = new File("classes");
    classes.mkdirs();
    File extraClasses = new File("extraClasses");
    extraClasses.mkdirs();

    System.out.println("compiling classes to extraClasses");
    { // setup class in extraClasses
      fm.setLocation(StandardLocation.CLASS_OUTPUT, Collections.singleton(extraClasses));
      List<? extends JavaFileObject> files =
          Arrays.asList(
              new MemFile("AnnoProc.java", annoProc), new MemFile("Callback.java", callback));
      JavacTask task = tool.getTask(null, fm, null, null, null, files);
      check(task.call());
    }

    System.out.println("compiling dummy to classes with anno processor");
    { // use that class in a TaskListener after processing has completed
      PrintStream prev = System.out;
      String out;
      ByteArrayOutputStream baos = new ByteArrayOutputStream();
      try (PrintStream ps = new PrintStream(baos)) {
        System.setOut(ps);
        File testClasses = new File(System.getProperty("test.classes"));
        fm.setLocation(StandardLocation.CLASS_OUTPUT, Collections.singleton(classes));
        fm.setLocation(
            StandardLocation.ANNOTATION_PROCESSOR_PATH, Arrays.asList(extraClasses, testClasses));
        List<? extends JavaFileObject> files =
            Arrays.asList(new MemFile("my://dummy", "class Dummy { }"));
        List<String> options = Arrays.asList("-processor", "AnnoProc");
        JavacTask task = tool.getTask(null, fm, null, options, null, files);
        task.setTaskListener(this);
        check(task.call());
      } finally {
        System.setOut(prev);
        out = baos.toString();
        if (!out.isEmpty()) System.out.println(out);
      }
      check(out.contains("AnnoProc$1: run()"));
      check(out.contains("Callback: run()"));
    }
  }