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

The following examples show how to use com.sun.source.util.JavacTask#instance() . 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: TestClose.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
public static void add(ProcessingEnvironment env, Runnable r) {
    try {
        JavacTask task = JavacTask.instance(env);
        TaskListener l = ((BasicJavacTask) task).getTaskListeners().iterator().next();
        // The TaskListener is an instanceof TestClose, but when using the
        // default class loaders. the taskListener uses a different
        // instance of Class<TestClose> than the anno processor.
        // If you try to evaluate
        //      TestClose tc = (TestClose) (l).
        // you get the following somewhat confusing error:
        //   java.lang.ClassCastException: TestClose cannot be cast to TestClose
        // The workaround is to access the fields of TestClose with reflection.
        Field f = l.getClass().getField("runnables");
        @SuppressWarnings("unchecked")
        List<Runnable> runnables = (List<Runnable>) f.get(l);
        runnables.add(r);
    } catch (Throwable t) {
        t.printStackTrace();
    }
}
 
Example 2
Source File: TestSimpleAddRemove.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
@Override
        public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
//            System.err.println("TestProcessor.process " + roundEnv);
            JavacTask task = JavacTask.instance(processingEnv);
            if (++round == 1) {
                switch (ak) {
                    case ADD_IN_PROCESSOR:
                        task.addTaskListener(listener);
                        break;
                    case ADD_IN_LISTENER:
                        addInListener(task, TaskEvent.Kind.ANALYZE, listener);
                        break;
                }
            } else if (roundEnv.processingOver()) {
                switch (rk) {
                    case REMOVE_IN_PROCESSOR:
                        task.removeTaskListener(listener);
                        break;
                    case REMOVE_IN_LISTENER:
                        removeInListener(task, TaskEvent.Kind.GENERATE, listener);
                        break;
                }
            }
            return true;
        }
 
Example 3
Source File: TestClose.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
public static void add(ProcessingEnvironment env, Runnable r) {
    try {
        JavacTask task = JavacTask.instance(env);
        TaskListener l = ((BasicJavacTask) task).getTaskListeners().iterator().next();
        // The TaskListener is an instanceof TestClose, but when using the
        // default class loaders. the taskListener uses a different
        // instance of Class<TestClose> than the anno processor.
        // If you try to evaluate
        //      TestClose tc = (TestClose) (l).
        // you get the following somewhat confusing error:
        //   java.lang.ClassCastException: TestClose cannot be cast to TestClose
        // The workaround is to access the fields of TestClose with reflection.
        Field f = l.getClass().getField("runnables");
        @SuppressWarnings("unchecked")
        List<Runnable> runnables = (List<Runnable>) f.get(l);
        runnables.add(r);
    } catch (Throwable t) {
        t.printStackTrace();
    }
}
 
Example 4
Source File: TestSimpleAddRemove.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
@Override
        public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
//            System.err.println("TestProcessor.process " + roundEnv);
            JavacTask task = JavacTask.instance(processingEnv);
            if (++round == 1) {
                switch (ak) {
                    case ADD_IN_PROCESSOR:
                        task.addTaskListener(listener);
                        break;
                    case ADD_IN_LISTENER:
                        addInListener(task, TaskEvent.Kind.ANALYZE, listener);
                        break;
                }
            } else if (roundEnv.processingOver()) {
                switch (rk) {
                    case REMOVE_IN_PROCESSOR:
                        task.removeTaskListener(listener);
                        break;
                    case REMOVE_IN_LISTENER:
                        removeInListener(task, TaskEvent.Kind.GENERATE, listener);
                        break;
                }
            }
            return true;
        }
 
Example 5
Source File: TestClose.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
public static void add(ProcessingEnvironment env, Runnable r) {
    try {
        JavacTask task = JavacTask.instance(env);
        TaskListener l = ((BasicJavacTask) task).getTaskListeners().iterator().next();
        // The TaskListener is an instanceof TestClose, but when using the
        // default class loaders. the taskListener uses a different
        // instance of Class<TestClose> than the anno processor.
        // If you try to evaluate
        //      TestClose tc = (TestClose) (l).
        // you get the following somewhat confusing error:
        //   java.lang.ClassCastException: TestClose cannot be cast to TestClose
        // The workaround is to access the fields of TestClose with reflection.
        Field f = l.getClass().getField("runnables");
        @SuppressWarnings("unchecked")
        List<Runnable> runnables = (List<Runnable>) f.get(l);
        runnables.add(r);
    } catch (Throwable t) {
        t.printStackTrace();
    }
}
 
Example 6
Source File: TestSimpleAddRemove.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
@Override
        public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
//            System.err.println("TestProcessor.process " + roundEnv);
            JavacTask task = JavacTask.instance(processingEnv);
            if (++round == 1) {
                switch (ak) {
                    case ADD_IN_PROCESSOR:
                        task.addTaskListener(listener);
                        break;
                    case ADD_IN_LISTENER:
                        addInListener(task, TaskEvent.Kind.ANALYZE, listener);
                        break;
                }
            } else if (roundEnv.processingOver()) {
                switch (rk) {
                    case REMOVE_IN_PROCESSOR:
                        task.removeTaskListener(listener);
                        break;
                    case REMOVE_IN_LISTENER:
                        removeInListener(task, TaskEvent.Kind.GENERATE, listener);
                        break;
                }
            }
            return true;
        }
 
Example 7
Source File: TestClose.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
public static void add(ProcessingEnvironment env, Runnable r) {
    try {
        JavacTask task = JavacTask.instance(env);
        TaskListener l = ((BasicJavacTask) task).getTaskListeners().iterator().next();
        // The TaskListener is an instanceof TestClose, but when using the
        // default class loaders. the taskListener uses a different
        // instance of Class<TestClose> than the anno processor.
        // If you try to evaluate
        //      TestClose tc = (TestClose) (l).
        // you get the following somewhat confusing error:
        //   java.lang.ClassCastException: TestClose cannot be cast to TestClose
        // The workaround is to access the fields of TestClose with reflection.
        Field f = l.getClass().getField("runnables");
        @SuppressWarnings("unchecked")
        List<Runnable> runnables = (List<Runnable>) f.get(l);
        runnables.add(r);
    } catch (Throwable t) {
        t.printStackTrace();
    }
}
 
Example 8
Source File: TestSimpleAddRemove.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
@Override
        public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
//            System.err.println("TestProcessor.process " + roundEnv);
            JavacTask task = JavacTask.instance(processingEnv);
            if (++round == 1) {
                switch (ak) {
                    case ADD_IN_PROCESSOR:
                        task.addTaskListener(listener);
                        break;
                    case ADD_IN_LISTENER:
                        addInListener(task, TaskEvent.Kind.ANALYZE, listener);
                        break;
                }
            } else if (roundEnv.processingOver()) {
                switch (rk) {
                    case REMOVE_IN_PROCESSOR:
                        task.removeTaskListener(listener);
                        break;
                    case REMOVE_IN_LISTENER:
                        removeInListener(task, TaskEvent.Kind.GENERATE, listener);
                        break;
                }
            }
            return true;
        }
 
Example 9
Source File: TestClose.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
public static void add(ProcessingEnvironment env, Runnable r) {
    // ensure this class in this class loader can access javac internals
    try {
        JavacTask task = JavacTask.instance(env);
        TaskListener l = ((BasicJavacTask) task).getTaskListeners().iterator().next();
        // The TaskListener is an instanceof TestClose, but when using the
        // default class loaders. the taskListener uses a different
        // instance of Class<TestClose> than the anno processor.
        // If you try to evaluate
        //      TestClose tc = (TestClose) (l).
        // you get the following somewhat confusing error:
        //   java.lang.ClassCastException: TestClose cannot be cast to TestClose
        // The workaround is to access the fields of TestClose with reflection.
        Field f = l.getClass().getField("runnables");
        @SuppressWarnings("unchecked")
        List<Runnable> runnables = (List<Runnable>) f.get(l);
        runnables.add(r);
    } catch (Throwable t) {
        t.printStackTrace();
    }
}
 
Example 10
Source File: TestSimpleAddRemove.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
@Override
        public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
//            System.err.println("TestProcessor.process " + roundEnv);
            JavacTask task = JavacTask.instance(processingEnv);
            if (++round == 1) {
                switch (ak) {
                    case ADD_IN_PROCESSOR:
                        task.addTaskListener(listener);
                        break;
                    case ADD_IN_LISTENER:
                        addInListener(task, TaskEvent.Kind.ANALYZE, listener);
                        break;
                }
            } else if (roundEnv.processingOver()) {
                switch (rk) {
                    case REMOVE_IN_PROCESSOR:
                        task.removeTaskListener(listener);
                        break;
                    case REMOVE_IN_LISTENER:
                        removeInListener(task, TaskEvent.Kind.GENERATE, listener);
                        break;
                }
            }
            return true;
        }
 
Example 11
Source File: TestClose.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
public static void add(ProcessingEnvironment env, Runnable r) {
    try {
        JavacTask task = JavacTask.instance(env);
        TaskListener l = ((BasicJavacTask) task).getTaskListeners().iterator().next();
        // The TaskListener is an instanceof TestClose, but when using the
        // default class loaders. the taskListener uses a different
        // instance of Class<TestClose> than the anno processor.
        // If you try to evaluate
        //      TestClose tc = (TestClose) (l).
        // you get the following somewhat confusing error:
        //   java.lang.ClassCastException: TestClose cannot be cast to TestClose
        // The workaround is to access the fields of TestClose with reflection.
        Field f = l.getClass().getField("runnables");
        @SuppressWarnings("unchecked")
        List<Runnable> runnables = (List<Runnable>) f.get(l);
        runnables.add(r);
    } catch (Throwable t) {
        t.printStackTrace();
    }
}
 
Example 12
Source File: TestSimpleAddRemove.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
@Override
        public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
//            System.err.println("TestProcessor.process " + roundEnv);
            JavacTask task = JavacTask.instance(processingEnv);
            if (++round == 1) {
                switch (ak) {
                    case ADD_IN_PROCESSOR:
                        task.addTaskListener(listener);
                        break;
                    case ADD_IN_LISTENER:
                        addInListener(task, TaskEvent.Kind.ANALYZE, listener);
                        break;
                }
            } else if (roundEnv.processingOver()) {
                switch (rk) {
                    case REMOVE_IN_PROCESSOR:
                        task.removeTaskListener(listener);
                        break;
                    case REMOVE_IN_LISTENER:
                        removeInListener(task, TaskEvent.Kind.GENERATE, listener);
                        break;
                }
            }
            return true;
        }
 
Example 13
Source File: TestClose.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
public static void add(ProcessingEnvironment env, Runnable r) {
    try {
        JavacTask task = JavacTask.instance(env);
        TaskListener l = ((BasicJavacTask) task).getTaskListeners().iterator().next();
        // The TaskListener is an instanceof TestClose, but when using the
        // default class loaders. the taskListener uses a different
        // instance of Class<TestClose> than the anno processor.
        // If you try to evaluate
        //      TestClose tc = (TestClose) (l).
        // you get the following somewhat confusing error:
        //   java.lang.ClassCastException: TestClose cannot be cast to TestClose
        // The workaround is to access the fields of TestClose with reflection.
        Field f = l.getClass().getField("runnables");
        @SuppressWarnings("unchecked")
        List<Runnable> runnables = (List<Runnable>) f.get(l);
        runnables.add(r);
    } catch (Throwable t) {
        t.printStackTrace();
    }
}
 
Example 14
Source File: TestSimpleAddRemove.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
@Override
        public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
//            System.err.println("TestProcessor.process " + roundEnv);
            JavacTask task = JavacTask.instance(processingEnv);
            if (++round == 1) {
                switch (ak) {
                    case ADD_IN_PROCESSOR:
                        task.addTaskListener(listener);
                        break;
                    case ADD_IN_LISTENER:
                        addInListener(task, TaskEvent.Kind.ANALYZE, listener);
                        break;
                }
            } else if (roundEnv.processingOver()) {
                switch (rk) {
                    case REMOVE_IN_PROCESSOR:
                        task.removeTaskListener(listener);
                        break;
                    case REMOVE_IN_LISTENER:
                        removeInListener(task, TaskEvent.Kind.GENERATE, listener);
                        break;
                }
            }
            return true;
        }
 
Example 15
Source File: TestClose.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
public static void add(ProcessingEnvironment env, Runnable r) {
    try {
        JavacTask task = JavacTask.instance(env);
        TaskListener l = ((BasicJavacTask) task).getTaskListeners().iterator().next();
        // The TaskListener is an instanceof TestClose, but when using the
        // default class loaders. the taskListener uses a different
        // instance of Class<TestClose> than the anno processor.
        // If you try to evaluate
        //      TestClose tc = (TestClose) (l).
        // you get the following somewhat confusing error:
        //   java.lang.ClassCastException: TestClose cannot be cast to TestClose
        // The workaround is to access the fields of TestClose with reflection.
        Field f = l.getClass().getField("runnables");
        @SuppressWarnings("unchecked")
        List<Runnable> runnables = (List<Runnable>) f.get(l);
        runnables.add(r);
    } catch (Throwable t) {
        t.printStackTrace();
    }
}
 
Example 16
Source File: TestSimpleAddRemove.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
@Override
        public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
//            System.err.println("TestProcessor.process " + roundEnv);
            JavacTask task = JavacTask.instance(processingEnv);
            if (++round == 1) {
                switch (ak) {
                    case ADD_IN_PROCESSOR:
                        task.addTaskListener(listener);
                        break;
                    case ADD_IN_LISTENER:
                        addInListener(task, TaskEvent.Kind.ANALYZE, listener);
                        break;
                }
            } else if (roundEnv.processingOver()) {
                switch (rk) {
                    case REMOVE_IN_PROCESSOR:
                        task.removeTaskListener(listener);
                        break;
                    case REMOVE_IN_LISTENER:
                        removeInListener(task, TaskEvent.Kind.GENERATE, listener);
                        break;
                }
            }
            return true;
        }