Java Code Examples for com.sun.source.util.JavacTask#setTaskListener()

The following examples show how to use com.sun.source.util.JavacTask#setTaskListener() . 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: ComboTask.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Fork a new compilation task; if possible the compilation context from previous executions is
 * retained (see comments in ReusableContext as to when it's safe to do so); otherwise a brand
 * new context is created.
 */
public JavacTask getTask() {
    if (task == null) {
        ReusableContext context = env.context();
        String opts = options == null ? "" :
                StreamSupport.stream(options.spliterator(), false).collect(Collectors.joining());
        context.clear();
        if (!context.polluted && (context.opts == null || context.opts.equals(opts))) {
            //we can reuse former context
            env.info().ctxReusedCount++;
        } else {
            env.info().ctxDroppedCount++;
            //it's not safe to reuse context - create a new one
            context = env.setContext(new ReusableContext());
        }
        context.opts = opts;
        JavacTask javacTask = ((JavacTool)env.javaCompiler()).getTask(out, env.fileManager(),
                diagsCollector, options, null, sources, context);
        javacTask.setTaskListener(context);
        for (TaskListener l : listeners) {
            javacTask.addTaskListener(l);
        }
        task = javacTask;
    }
    return task;
}
 
Example 2
Source File: TestClose2.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
void run() throws IOException {
    File testSrc = new File(System.getProperty("test.src"));
    File testClasses = new File(System.getProperty("test.classes"));

    JavacTool tool = (JavacTool) ToolProvider.getSystemJavaCompiler();
    final ClassLoader cl = getClass().getClassLoader();
    Context c = new Context();
    StandardJavaFileManager fm = new JavacFileManager(c, true, null) {
        @Override
        protected ClassLoader getClassLoader(URL[] urls) {
            return new URLClassLoader(urls, cl) {
                @Override
                public void close() throws IOException {
                    System.err.println(getClass().getName() + " closing");
                    TestClose2.this.closedCount++;
                    TestClose2.this.closedIsLast = true;
                    super.close();
                }
            };
        }
    };

    fm.setLocation(StandardLocation.CLASS_OUTPUT,
            Collections.singleton(new File(".")));
    fm.setLocation(StandardLocation.ANNOTATION_PROCESSOR_PATH,
            Collections.singleton(testClasses));
    Iterable<? extends JavaFileObject> files =
            fm.getJavaFileObjects(new File(testSrc, TestClose2.class.getName() + ".java"));
    List<String> options = Arrays.asList(
            "--add-exports", "jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED",
            "--add-exports", "jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED",
            "--add-exports", "jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED",
            "-processor", TestClose2.class.getName());

    JavacTask task = tool.getTask(null, fm, null, options, null, files);
    task.setTaskListener(this);

    if (!task.call())
        throw new Error("compilation failed");

    if (closedCount == 0)
        throw new Error("no closing message");
    else if (closedCount > 1)
        throw new Error(closedCount + " closed messages");

    if (!closedIsLast)
        throw new Error("closing message not last");
}
 
Example 3
Source File: TestSimpleAddRemove.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
void test(CompileKind ck, AddKind ak, RemoveKind rk) throws IOException {
    System.err.println("Test: " + ck + " " + ak + " " + rk);

    File tmpDir = new File(ck + "-" + ak + "-" + rk);
    tmpDir.mkdirs();
    fm.setLocation(StandardLocation.CLASS_OUTPUT, Arrays.asList(tmpDir));

    List<String> options = new ArrayList<String>();
    Iterable<? extends JavaFileObject> files = Arrays.asList(new TestSource());
    StringWriter sw = new StringWriter();
    PrintWriter pw = new PrintWriter(sw);
    JavacTask task = tool.getTask(pw, fm, null, options, null, files);

    EventKindCounter ec = new EventKindCounter();
    TaskListener listener = new TestListener(ec);
    boolean needProcessor = false;

    switch (ak) {
        case SET_IN_TASK:
            task.setTaskListener(listener);
            break;
        case ADD_IN_TASK:
            task.addTaskListener(listener);
            break;
        case ADD_IN_PROCESSOR:
        case ADD_IN_LISTENER:
            needProcessor = true;
    }

    switch (rk) {
        case REMOVE_IN_TASK:
            task.removeTaskListener(listener);
            break;
        case REMOVE_IN_PROCESSOR:
        case REMOVE_IN_LISTENER:
            needProcessor = true;
    }

    if (needProcessor)
        task.setProcessors(Arrays.asList(new TestProcessor(ak, rk, listener)));

    ck.run(task);
    System.err.println(ec);

    check(ck, ak, rk, ec);

    System.err.println();
}
 
Example 4
Source File: TestClose2.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
void run() throws IOException {
    File testSrc = new File(System.getProperty("test.src"));
    File testClasses = new File(System.getProperty("test.classes"));

    JavacTool tool = (JavacTool) ToolProvider.getSystemJavaCompiler();
    final ClassLoader cl = getClass().getClassLoader();
    Context c = new Context();
    StandardJavaFileManager fm = new JavacFileManager(c, true, null) {
        @Override
        protected ClassLoader getClassLoader(URL[] urls) {
            return new URLClassLoader(urls, cl) {
                @Override
                public void close() throws IOException {
                    System.err.println(getClass().getName() + " closing");
                    TestClose2.this.closedCount++;
                    TestClose2.this.closedIsLast = true;
                    super.close();
                }
            };
        }
    };

    fm.setLocation(StandardLocation.CLASS_OUTPUT,
            Collections.singleton(new File(".")));
    fm.setLocation(StandardLocation.ANNOTATION_PROCESSOR_PATH,
            Collections.singleton(testClasses));
    Iterable<? extends JavaFileObject> files =
            fm.getJavaFileObjects(new File(testSrc, TestClose2.class.getName() + ".java"));
    List<String> options = Arrays.asList(
            "-processor", TestClose2.class.getName());

    JavacTask task = tool.getTask(null, fm, null, options, null, files);
    task.setTaskListener(this);

    if (!task.call())
        throw new Error("compilation failed");

    if (closedCount == 0)
        throw new Error("no closing message");
    else if (closedCount > 1)
        throw new Error(closedCount + " closed messages");

    if (!closedIsLast)
        throw new Error("closing message not last");
}
 
Example 5
Source File: TestSimpleAddRemove.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
void test(CompileKind ck, AddKind ak, RemoveKind rk) throws IOException {
    System.err.println("Test: " + ck + " " + ak + " " + rk);

    File tmpDir = new File(ck + "-" + ak + "-" + rk);
    tmpDir.mkdirs();
    fm.setLocation(StandardLocation.CLASS_OUTPUT, Arrays.asList(tmpDir));

    List<String> options = new ArrayList<String>();
    Iterable<? extends JavaFileObject> files = Arrays.asList(new TestSource());
    StringWriter sw = new StringWriter();
    PrintWriter pw = new PrintWriter(sw);
    JavacTask task = tool.getTask(pw, fm, null, options, null, files);

    EventKindCounter ec = new EventKindCounter();
    TaskListener listener = new TestListener(ec);
    boolean needProcessor = false;

    switch (ak) {
        case SET_IN_TASK:
            task.setTaskListener(listener);
            break;
        case ADD_IN_TASK:
            task.addTaskListener(listener);
            break;
        case ADD_IN_PROCESSOR:
        case ADD_IN_LISTENER:
            needProcessor = true;
    }

    switch (rk) {
        case REMOVE_IN_TASK:
            task.removeTaskListener(listener);
            break;
        case REMOVE_IN_PROCESSOR:
        case REMOVE_IN_LISTENER:
            needProcessor = true;
    }

    if (needProcessor)
        task.setProcessors(Arrays.asList(new TestProcessor(ak, rk, listener)));

    ck.run(task);
    System.err.println(ec);

    check(ck, ak, rk, ec);

    System.err.println();
}
 
Example 6
Source File: TestClose2.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
void run() throws IOException {
    File testSrc = new File(System.getProperty("test.src"));
    File testClasses = new File(System.getProperty("test.classes"));

    JavacTool tool = (JavacTool) ToolProvider.getSystemJavaCompiler();
    final ClassLoader cl = getClass().getClassLoader();
    Context c = new Context();
    StandardJavaFileManager fm = new JavacFileManager(c, true, null) {
        @Override
        protected ClassLoader getClassLoader(URL[] urls) {
            return new URLClassLoader(urls, cl) {
                @Override
                public void close() throws IOException {
                    System.err.println(getClass().getName() + " closing");
                    TestClose2.this.closedCount++;
                    TestClose2.this.closedIsLast = true;
                    super.close();
                }
            };
        }
    };

    fm.setLocation(StandardLocation.CLASS_OUTPUT,
            Collections.singleton(new File(".")));
    fm.setLocation(StandardLocation.ANNOTATION_PROCESSOR_PATH,
            Collections.singleton(testClasses));
    Iterable<? extends JavaFileObject> files =
            fm.getJavaFileObjects(new File(testSrc, TestClose2.class.getName() + ".java"));
    List<String> options = Arrays.asList(
            "-processor", TestClose2.class.getName());

    JavacTask task = tool.getTask(null, fm, null, options, null, files);
    task.setTaskListener(this);

    if (!task.call())
        throw new Error("compilation failed");

    if (closedCount == 0)
        throw new Error("no closing message");
    else if (closedCount > 1)
        throw new Error(closedCount + " closed messages");

    if (!closedIsLast)
        throw new Error("closing message not last");
}
 
Example 7
Source File: TestSimpleAddRemove.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
void test(CompileKind ck, AddKind ak, RemoveKind rk) throws IOException {
    System.err.println("Test: " + ck + " " + ak + " " + rk);

    File tmpDir = new File(ck + "-" + ak + "-" + rk);
    tmpDir.mkdirs();
    fm.setLocation(StandardLocation.CLASS_OUTPUT, Arrays.asList(tmpDir));

    List<String> options = new ArrayList<String>();
    Iterable<? extends JavaFileObject> files = Arrays.asList(new TestSource());
    StringWriter sw = new StringWriter();
    PrintWriter pw = new PrintWriter(sw);
    JavacTask task = tool.getTask(pw, fm, null, options, null, files);

    EventKindCounter ec = new EventKindCounter();
    TaskListener listener = new TestListener(ec);
    boolean needProcessor = false;

    switch (ak) {
        case SET_IN_TASK:
            task.setTaskListener(listener);
            break;
        case ADD_IN_TASK:
            task.addTaskListener(listener);
            break;
        case ADD_IN_PROCESSOR:
        case ADD_IN_LISTENER:
            needProcessor = true;
    }

    switch (rk) {
        case REMOVE_IN_TASK:
            task.removeTaskListener(listener);
            break;
        case REMOVE_IN_PROCESSOR:
        case REMOVE_IN_LISTENER:
            needProcessor = true;
    }

    if (needProcessor)
        task.setProcessors(Arrays.asList(new TestProcessor(ak, rk, listener)));

    ck.run(task);
    System.err.println(ec);

    check(ck, ak, rk, ec);

    System.err.println();
}
 
Example 8
Source File: TestClose2.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
void run() throws IOException {
    File testSrc = new File(System.getProperty("test.src"));
    File testClasses = new File(System.getProperty("test.classes"));

    JavacTool tool = (JavacTool) ToolProvider.getSystemJavaCompiler();
    final ClassLoader cl = getClass().getClassLoader();
    Context c = new Context();
    StandardJavaFileManager fm = new JavacFileManager(c, true, null) {
        @Override
        protected ClassLoader getClassLoader(URL[] urls) {
            return new URLClassLoader(urls, cl) {
                @Override
                public void close() throws IOException {
                    System.err.println(getClass().getName() + " closing");
                    TestClose2.this.closedCount++;
                    TestClose2.this.closedIsLast = true;
                    super.close();
                }
            };
        }
    };

    fm.setLocation(StandardLocation.CLASS_OUTPUT,
            Collections.singleton(new File(".")));
    fm.setLocation(StandardLocation.ANNOTATION_PROCESSOR_PATH,
            Collections.singleton(testClasses));
    Iterable<? extends JavaFileObject> files =
            fm.getJavaFileObjects(new File(testSrc, TestClose2.class.getName() + ".java"));
    List<String> options = Arrays.asList(
            "-processor", TestClose2.class.getName());

    JavacTask task = tool.getTask(null, fm, null, options, null, files);
    task.setTaskListener(this);

    if (!task.call())
        throw new Error("compilation failed");

    if (closedCount == 0)
        throw new Error("no closing message");
    else if (closedCount > 1)
        throw new Error(closedCount + " closed messages");

    if (!closedIsLast)
        throw new Error("closing message not last");
}
 
Example 9
Source File: TestSimpleAddRemove.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
void test(CompileKind ck, AddKind ak, RemoveKind rk) throws IOException {
    System.err.println("Test: " + ck + " " + ak + " " + rk);

    File tmpDir = new File(ck + "-" + ak + "-" + rk);
    tmpDir.mkdirs();
    fm.setLocation(StandardLocation.CLASS_OUTPUT, Arrays.asList(tmpDir));

    List<String> options = new ArrayList<String>();
    Iterable<? extends JavaFileObject> files = Arrays.asList(new TestSource());
    StringWriter sw = new StringWriter();
    PrintWriter pw = new PrintWriter(sw);
    JavacTask task = tool.getTask(pw, fm, null, options, null, files);

    EventKindCounter ec = new EventKindCounter();
    TaskListener listener = new TestListener(ec);
    boolean needProcessor = false;

    switch (ak) {
        case SET_IN_TASK:
            task.setTaskListener(listener);
            break;
        case ADD_IN_TASK:
            task.addTaskListener(listener);
            break;
        case ADD_IN_PROCESSOR:
        case ADD_IN_LISTENER:
            needProcessor = true;
    }

    switch (rk) {
        case REMOVE_IN_TASK:
            task.removeTaskListener(listener);
            break;
        case REMOVE_IN_PROCESSOR:
        case REMOVE_IN_LISTENER:
            needProcessor = true;
    }

    if (needProcessor)
        task.setProcessors(Arrays.asList(new TestProcessor(ak, rk, listener)));

    ck.run(task);
    System.err.println(ec);

    check(ck, ak, rk, ec);

    System.err.println();
}
 
Example 10
Source File: TestClose2.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
void run() throws IOException {
    File testSrc = new File(System.getProperty("test.src"));
    File testClasses = new File(System.getProperty("test.classes"));

    JavacTool tool = (JavacTool) ToolProvider.getSystemJavaCompiler();
    final ClassLoader cl = getClass().getClassLoader();
    Context c = new Context();
    StandardJavaFileManager fm = new JavacFileManager(c, true, null) {
        @Override
        protected ClassLoader getClassLoader(URL[] urls) {
            return new URLClassLoader(urls, cl) {
                @Override
                public void close() throws IOException {
                    System.err.println(getClass().getName() + " closing");
                    TestClose2.this.closedCount++;
                    TestClose2.this.closedIsLast = true;
                    super.close();
                }
            };
        }
    };

    fm.setLocation(StandardLocation.CLASS_OUTPUT,
            Collections.singleton(new File(".")));
    fm.setLocation(StandardLocation.ANNOTATION_PROCESSOR_PATH,
            Collections.singleton(testClasses));
    Iterable<? extends JavaFileObject> files =
            fm.getJavaFileObjects(new File(testSrc, TestClose2.class.getName() + ".java"));
    List<String> options = Arrays.asList(
            "-processor", TestClose2.class.getName());

    JavacTask task = tool.getTask(null, fm, null, options, null, files);
    task.setTaskListener(this);

    if (!task.call())
        throw new Error("compilation failed");

    if (closedCount == 0)
        throw new Error("no closing message");
    else if (closedCount > 1)
        throw new Error(closedCount + " closed messages");

    if (!closedIsLast)
        throw new Error("closing message not last");
}
 
Example 11
Source File: TestSimpleAddRemove.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
void test(CompileKind ck, AddKind ak, RemoveKind rk) throws IOException {
    System.err.println("Test: " + ck + " " + ak + " " + rk);

    File tmpDir = new File(ck + "-" + ak + "-" + rk);
    tmpDir.mkdirs();
    fm.setLocation(StandardLocation.CLASS_OUTPUT, Arrays.asList(tmpDir));

    List<String> options = new ArrayList<String>();
    Iterable<? extends JavaFileObject> files = Arrays.asList(new TestSource());
    StringWriter sw = new StringWriter();
    PrintWriter pw = new PrintWriter(sw);
    JavacTask task = tool.getTask(pw, fm, null, options, null, files);

    EventKindCounter ec = new EventKindCounter();
    TaskListener listener = new TestListener(ec);
    boolean needProcessor = false;

    switch (ak) {
        case SET_IN_TASK:
            task.setTaskListener(listener);
            break;
        case ADD_IN_TASK:
            task.addTaskListener(listener);
            break;
        case ADD_IN_PROCESSOR:
        case ADD_IN_LISTENER:
            needProcessor = true;
    }

    switch (rk) {
        case REMOVE_IN_TASK:
            task.removeTaskListener(listener);
            break;
        case REMOVE_IN_PROCESSOR:
        case REMOVE_IN_LISTENER:
            needProcessor = true;
    }

    if (needProcessor)
        task.setProcessors(Arrays.asList(new TestProcessor(ak, rk, listener)));

    ck.run(task);
    System.err.println(ec);

    check(ck, ak, rk, ec);

    System.err.println();
}
 
Example 12
Source File: TestClose2.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
void run() throws IOException {
    File testSrc = new File(System.getProperty("test.src"));
    File testClasses = new File(System.getProperty("test.classes"));

    JavacTool tool = (JavacTool) ToolProvider.getSystemJavaCompiler();
    final ClassLoader cl = getClass().getClassLoader();
    Context c = new Context();
    StandardJavaFileManager fm = new JavacFileManager(c, true, null) {
        @Override
        protected ClassLoader getClassLoader(URL[] urls) {
            return new URLClassLoader(urls, cl) {
                @Override
                public void close() throws IOException {
                    System.err.println(getClass().getName() + " closing");
                    TestClose2.this.closedCount++;
                    TestClose2.this.closedIsLast = true;
                    super.close();
                }
            };
        }
    };

    fm.setLocation(StandardLocation.CLASS_OUTPUT,
            Collections.singleton(new File(".")));
    fm.setLocation(StandardLocation.ANNOTATION_PROCESSOR_PATH,
            Collections.singleton(testClasses));
    Iterable<? extends JavaFileObject> files =
            fm.getJavaFileObjects(new File(testSrc, TestClose2.class.getName() + ".java"));
    List<String> options = Arrays.asList(
            "-processor", TestClose2.class.getName());

    JavacTask task = tool.getTask(null, fm, null, options, null, files);
    task.setTaskListener(this);

    if (!task.call())
        throw new Error("compilation failed");

    if (closedCount == 0)
        throw new Error("no closing message");
    else if (closedCount > 1)
        throw new Error(closedCount + " closed messages");

    if (!closedIsLast)
        throw new Error("closing message not last");
}
 
Example 13
Source File: TestSimpleAddRemove.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
void test(CompileKind ck, AddKind ak, RemoveKind rk) throws IOException {
    System.err.println("Test: " + ck + " " + ak + " " + rk);

    File tmpDir = new File(ck + "-" + ak + "-" + rk);
    tmpDir.mkdirs();
    fm.setLocation(StandardLocation.CLASS_OUTPUT, Arrays.asList(tmpDir));

    List<String> options = new ArrayList<String>();
    Iterable<? extends JavaFileObject> files = Arrays.asList(new TestSource());
    StringWriter sw = new StringWriter();
    PrintWriter pw = new PrintWriter(sw);
    JavacTask task = tool.getTask(pw, fm, null, options, null, files);

    EventKindCounter ec = new EventKindCounter();
    TaskListener listener = new TestListener(ec);
    boolean needProcessor = false;

    switch (ak) {
        case SET_IN_TASK:
            task.setTaskListener(listener);
            break;
        case ADD_IN_TASK:
            task.addTaskListener(listener);
            break;
        case ADD_IN_PROCESSOR:
        case ADD_IN_LISTENER:
            needProcessor = true;
    }

    switch (rk) {
        case REMOVE_IN_TASK:
            task.removeTaskListener(listener);
            break;
        case REMOVE_IN_PROCESSOR:
        case REMOVE_IN_LISTENER:
            needProcessor = true;
    }

    if (needProcessor)
        task.setProcessors(Arrays.asList(new TestProcessor(ak, rk, listener)));

    ck.run(task);
    System.err.println(ec);

    check(ck, ak, rk, ec);

    System.err.println();
}
 
Example 14
Source File: TestClose2.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
void run() throws IOException {
    File testSrc = new File(System.getProperty("test.src"));
    File testClasses = new File(System.getProperty("test.classes"));

    JavacTool tool = (JavacTool) ToolProvider.getSystemJavaCompiler();
    final ClassLoader cl = getClass().getClassLoader();
    Context c = new Context();
    StandardJavaFileManager fm = new JavacFileManager(c, true, null) {
        @Override
        protected ClassLoader getClassLoader(URL[] urls) {
            return new URLClassLoader(urls, cl) {
                @Override
                public void close() throws IOException {
                    System.err.println(getClass().getName() + " closing");
                    TestClose2.this.closedCount++;
                    TestClose2.this.closedIsLast = true;
                    super.close();
                }
            };
        }
    };

    fm.setLocation(StandardLocation.CLASS_OUTPUT,
            Collections.singleton(new File(".")));
    fm.setLocation(StandardLocation.ANNOTATION_PROCESSOR_PATH,
            Collections.singleton(testClasses));
    Iterable<? extends JavaFileObject> files =
            fm.getJavaFileObjects(new File(testSrc, TestClose2.class.getName() + ".java"));
    List<String> options = Arrays.asList(
            "-processor", TestClose2.class.getName());

    JavacTask task = tool.getTask(null, fm, null, options, null, files);
    task.setTaskListener(this);

    if (!task.call())
        throw new Error("compilation failed");

    if (closedCount == 0)
        throw new Error("no closing message");
    else if (closedCount > 1)
        throw new Error(closedCount + " closed messages");

    if (!closedIsLast)
        throw new Error("closing message not last");
}
 
Example 15
Source File: TestSimpleAddRemove.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
void test(CompileKind ck, AddKind ak, RemoveKind rk) throws IOException {
    System.err.println("Test: " + ck + " " + ak + " " + rk);

    File tmpDir = new File(ck + "-" + ak + "-" + rk);
    tmpDir.mkdirs();
    fm.setLocation(StandardLocation.CLASS_OUTPUT, Arrays.asList(tmpDir));

    List<String> options = new ArrayList<String>();
    Iterable<? extends JavaFileObject> files = Arrays.asList(new TestSource());
    StringWriter sw = new StringWriter();
    PrintWriter pw = new PrintWriter(sw);
    JavacTask task = tool.getTask(pw, fm, null, options, null, files);

    EventKindCounter ec = new EventKindCounter();
    TaskListener listener = new TestListener(ec);
    boolean needProcessor = false;

    switch (ak) {
        case SET_IN_TASK:
            task.setTaskListener(listener);
            break;
        case ADD_IN_TASK:
            task.addTaskListener(listener);
            break;
        case ADD_IN_PROCESSOR:
        case ADD_IN_LISTENER:
            needProcessor = true;
    }

    switch (rk) {
        case REMOVE_IN_TASK:
            task.removeTaskListener(listener);
            break;
        case REMOVE_IN_PROCESSOR:
        case REMOVE_IN_LISTENER:
            needProcessor = true;
    }

    if (needProcessor)
        task.setProcessors(Arrays.asList(new TestProcessor(ak, rk, listener)));

    ck.run(task);
    System.err.println(ec);

    check(ck, ak, rk, ec);

    System.err.println();
}
 
Example 16
Source File: TestClose2.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
void run() throws IOException {
    File testSrc = new File(System.getProperty("test.src"));
    File testClasses = new File(System.getProperty("test.classes"));

    JavacTool tool = (JavacTool) ToolProvider.getSystemJavaCompiler();
    final ClassLoader cl = getClass().getClassLoader();
    Context c = new Context();
    StandardJavaFileManager fm = new JavacFileManager(c, true, null) {
        @Override
        protected ClassLoader getClassLoader(URL[] urls) {
            return new URLClassLoader(urls, cl) {
                @Override
                public void close() throws IOException {
                    System.err.println(getClass().getName() + " closing");
                    TestClose2.this.closedCount++;
                    TestClose2.this.closedIsLast = true;
                    super.close();
                }
            };
        }
    };

    fm.setLocation(StandardLocation.CLASS_OUTPUT,
            Collections.singleton(new File(".")));
    fm.setLocation(StandardLocation.ANNOTATION_PROCESSOR_PATH,
            Collections.singleton(testClasses));
    Iterable<? extends JavaFileObject> files =
            fm.getJavaFileObjects(new File(testSrc, TestClose2.class.getName() + ".java"));
    List<String> options = Arrays.asList(
            "-processor", TestClose2.class.getName());

    JavacTask task = tool.getTask(null, fm, null, options, null, files);
    task.setTaskListener(this);

    if (!task.call())
        throw new Error("compilation failed");

    if (closedCount == 0)
        throw new Error("no closing message");
    else if (closedCount > 1)
        throw new Error(closedCount + " closed messages");

    if (!closedIsLast)
        throw new Error("closing message not last");
}
 
Example 17
Source File: TestSimpleAddRemove.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
void test(CompileKind ck, AddKind ak, RemoveKind rk) throws IOException {
    System.err.println("Test: " + ck + " " + ak + " " + rk);

    File tmpDir = new File(ck + "-" + ak + "-" + rk);
    tmpDir.mkdirs();
    fm.setLocation(StandardLocation.CLASS_OUTPUT, Arrays.asList(tmpDir));

    List<String> options = new ArrayList<String>();
    Iterable<? extends JavaFileObject> files = Arrays.asList(new TestSource());
    StringWriter sw = new StringWriter();
    PrintWriter pw = new PrintWriter(sw);
    JavacTask task = tool.getTask(pw, fm, null, options, null, files);

    EventKindCounter ec = new EventKindCounter();
    TaskListener listener = new TestListener(ec);
    boolean needProcessor = false;

    switch (ak) {
        case SET_IN_TASK:
            task.setTaskListener(listener);
            break;
        case ADD_IN_TASK:
            task.addTaskListener(listener);
            break;
        case ADD_IN_PROCESSOR:
        case ADD_IN_LISTENER:
            needProcessor = true;
    }

    switch (rk) {
        case REMOVE_IN_TASK:
            task.removeTaskListener(listener);
            break;
        case REMOVE_IN_PROCESSOR:
        case REMOVE_IN_LISTENER:
            needProcessor = true;
    }

    if (needProcessor)
        task.setProcessors(Arrays.asList(new TestProcessor(ak, rk, listener)));

    ck.run(task);
    System.err.println(ec);

    check(ck, ak, rk, ec);

    System.err.println();
}