com.sun.source.util.TaskListener Java Examples

The following examples show how to use com.sun.source.util.TaskListener. 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 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 #2
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 #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: 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 #5
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 #6
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 #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: BasicJavacTask.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void setTaskListener(TaskListener tl) {
    MultiTaskListener mtl = MultiTaskListener.instance(context);
    if (taskListener != null)
        mtl.remove(taskListener);
    if (tl != null)
        mtl.add(tl);
    taskListener = tl;
}
 
Example #9
Source File: MultiTaskListener.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void finished(TaskEvent e) {
    // guard against listeners being updated by a listener
    TaskListener[] ll = this.listeners;
    for (TaskListener l: ll)
        l.finished(e);
}
 
Example #10
Source File: MultiTaskListener.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void started(TaskEvent e) {
    // guard against listeners being updated by a listener
    TaskListener[] ll = this.listeners;
    for (TaskListener l: ll)
        l.started(e);
}
 
Example #11
Source File: MultiTaskListener.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
public void remove(TaskListener listener) {
    for (int i = 0; i < listeners.length; i++) {
        if (ccw.unwrap(listeners[i]) == listener) {
            TaskListener[] newListeners = new TaskListener[listeners.length - 1];
            System.arraycopy(listeners, 0, newListeners, 0, i);
            System.arraycopy(listeners, i + 1, newListeners, i, newListeners.length - i);
            listeners = newListeners;
            break;
        }
    }
}
 
Example #12
Source File: MultiTaskListener.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
public void remove(TaskListener listener) {
    for (int i = 0; i < listeners.length; i++) {
        if (ccw.unwrap(listeners[i]) == listener) {
            TaskListener[] newListeners = new TaskListener[listeners.length - 1];
            System.arraycopy(listeners, 0, newListeners, 0, i);
            System.arraycopy(listeners, i + 1, newListeners, i, newListeners.length - i);
            listeners = newListeners;
            break;
        }
    }
}
 
Example #13
Source File: BasicJavacTask.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void setTaskListener(TaskListener tl) {
    MultiTaskListener mtl = MultiTaskListener.instance(context);
    if (taskListener != null)
        mtl.remove(taskListener);
    if (tl != null)
        mtl.add(tl);
    taskListener = tl;
}
 
Example #14
Source File: MultiTaskListener.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
public void add(TaskListener listener) {
    for (TaskListener l: listeners) {
        if (ccw.unwrap(l) == listener)
            throw new IllegalStateException();
    }
    listeners = Arrays.copyOf(listeners, listeners.length + 1);
    listeners[listeners.length - 1] = ccw.wrap(listener);
}
 
Example #15
Source File: MultiTaskListener.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
public void remove(TaskListener listener) {
    for (int i = 0; i < listeners.length; i++) {
        if (ccw.unwrap(listeners[i]) == listener) {
            TaskListener[] newListeners = new TaskListener[listeners.length - 1];
            System.arraycopy(listeners, 0, newListeners, 0, i);
            System.arraycopy(listeners, i + 1, newListeners, i, newListeners.length - i);
            listeners = newListeners;
            break;
        }
    }
}
 
Example #16
Source File: MultiTaskListener.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void started(TaskEvent e) {
    // guard against listeners being updated by a listener
    TaskListener[] ll = this.listeners;
    for (TaskListener l: ll)
        l.started(e);
}
 
Example #17
Source File: BasicJavacTask.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void setTaskListener(TaskListener tl) {
    MultiTaskListener mtl = MultiTaskListener.instance(context);
    if (taskListener != null)
        mtl.remove(taskListener);
    if (tl != null)
        mtl.add(tl);
    taskListener = tl;
}
 
Example #18
Source File: MultiTaskListener.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public void add(TaskListener listener) {
    for (TaskListener l: listeners) {
        if (ccw.unwrap(l) == listener)
            throw new IllegalStateException();
    }
    listeners = Arrays.copyOf(listeners, listeners.length + 1);
    listeners[listeners.length - 1] = ccw.wrap(listener);
}
 
Example #19
Source File: BuckJavacTask.java    From buck with Apache License 2.0 5 votes vote down vote up
/**
 * Sets a {@link TaskListener}. Like {@link JavacTask}'s implementation of this method, the
 * listener does not replace listeners added with {@link #addTaskListener(TaskListener)}. Instead,
 * it replaces only the listener provided in the previous call to this method, if any.
 *
 * <p>Presumably this behavior was to enable {@link com.sun.source.util.Plugin}s to work properly
 * with build systems that were written when only a single {@link TaskListener} was supported at a
 * time.
 */
@Override
public void setTaskListener(@Nullable TaskListener taskListener) {
  if (singleTaskListener != null) {
    removeTaskListener(singleTaskListener);
  }
  singleTaskListener = taskListener;
  if (singleTaskListener != null) {
    addTaskListener(singleTaskListener);
  }
}
 
Example #20
Source File: MultiTaskListener.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
public void remove(TaskListener listener) {
    for (int i = 0; i < listeners.length; i++) {
        if (ccw.unwrap(listeners[i]) == listener) {
            TaskListener[] newListeners = new TaskListener[listeners.length - 1];
            System.arraycopy(listeners, 0, newListeners, 0, i);
            System.arraycopy(listeners, i + 1, newListeners, i, newListeners.length - i);
            listeners = newListeners;
            break;
        }
    }
}
 
Example #21
Source File: MultiTaskListener.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
public void add(TaskListener listener) {
    for (TaskListener l: listeners) {
        if (ccw.unwrap(l) == listener)
            throw new IllegalStateException();
    }
    listeners = Arrays.copyOf(listeners, listeners.length + 1);
    listeners[listeners.length - 1] = ccw.wrap(listener);
}
 
Example #22
Source File: InterfaceScannerTest.java    From buck with Apache License 2.0 5 votes vote down vote up
private void findTypeReferences(String[] sourceLines, boolean errorsOK) throws IOException {
  constantReferences = new ArrayList<>();
  typeReferences = new ArrayList<>();
  importedTypes = new ArrayList<>();
  starImportedElements = new ArrayList<>();
  declaredTypes = new ArrayList<>();
  staticImportOwners = new HashMap<>();
  staticStarImports = new ArrayList<>();

  testCompiler.setAllowCompilationErrors(errorsOK);
  compile(
      ImmutableMap.of("Foo.java", Joiner.on('\n').join(sourceLines)),
      // ErrorType's get nulled out when the task returns, so we have to get a call back during
      // the run so that we can still look at them.
      new TaskListenerFactory() {
        @Override
        public TaskListener newTaskListener(BuckJavacTask task) {
          return new PostEnterCallback() {
            @Override
            protected void enterComplete(List<CompilationUnitTree> compilationUnits) {
              FinderListener listener = new FinderListener();
              InterfaceScanner finder = new InterfaceScanner(trees);
              for (CompilationUnitTree compilationUnit : compilationUnits) {
                finder.findReferences(compilationUnit, listener);
              }
            }
          };
        }
      });

  if (!errorsOK) {
    assertNoErrors();
  }
}
 
Example #23
Source File: MultiTaskListener.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override @DefinedBy(Api.COMPILER_TREE)
public void started(TaskEvent e) {
    // guard against listeners being updated by a listener
    TaskListener[] ll = this.listeners;
    for (TaskListener l: ll)
        l.started(e);
}
 
Example #24
Source File: JavacProcessingEnvironment.java    From java-n-IDE-for-Android with Apache License 2.0 5 votes vote down vote up
/** Run a processing round. */
void run(boolean lastRound, boolean errorStatus) {
    printRoundInfo(lastRound);

    TaskListener taskListener = context.get(TaskListener.class);
    if (taskListener != null)
        taskListener.started(new TaskEvent(TaskEvent.Kind.ANNOTATION_PROCESSING_ROUND));

    try {
        if (lastRound) {
            filer.setLastRound(true);
            Set<Element> emptyRootElements = Collections.emptySet(); // immutable
            RoundEnvironment renv = new JavacRoundEnvironment(true,
                    errorStatus,
                    emptyRootElements,
                    JavacProcessingEnvironment.this);
            discoveredProcs.iterator().runContributingProcs(renv);
        } else {
            discoverAndRunProcs(context, annotationsPresent, topLevelClasses, packageInfoFiles);
        }
    } finally {
        if (taskListener != null)
            taskListener.finished(new TaskEvent(TaskEvent.Kind.ANNOTATION_PROCESSING_ROUND));
    }

    nMessagerErrors = messager.errorCount();
}
 
Example #25
Source File: MultiTaskListener.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void finished(TaskEvent e) {
    // guard against listeners being updated by a listener
    TaskListener[] ll = this.listeners;
    for (TaskListener l: ll)
        l.finished(e);
}
 
Example #26
Source File: MultiTaskListener.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public void remove(TaskListener listener) {
    for (int i = 0; i < listeners.length; i++) {
        if (ccw.unwrap(listeners[i]) == listener) {
            if (listeners.length == 1) {
                listeners = EMPTY_LISTENERS;
            } else {
                TaskListener[] newListeners = new TaskListener[listeners.length - 1];
                System.arraycopy(listeners, 0, newListeners, 0, i);
                System.arraycopy(listeners, i + 1, newListeners, i, newListeners.length - i);
                listeners = newListeners;
            }
            break;
        }
    }
}
 
Example #27
Source File: BasicJavacTask.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Override @DefinedBy(Api.COMPILER_TREE)
public void setTaskListener(TaskListener tl) {
    MultiTaskListener mtl = MultiTaskListener.instance(context);
    if (taskListener != null)
        mtl.remove(taskListener);
    if (tl != null)
        mtl.add(tl);
    taskListener = tl;
}
 
Example #28
Source File: MultiTaskListener.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
public void remove(TaskListener listener) {
    for (int i = 0; i < listeners.length; i++) {
        if (ccw.unwrap(listeners[i]) == listener) {
            TaskListener[] newListeners = new TaskListener[listeners.length - 1];
            System.arraycopy(listeners, 0, newListeners, 0, i);
            System.arraycopy(listeners, i + 1, newListeners, i, newListeners.length - i);
            listeners = newListeners;
            break;
        }
    }
}
 
Example #29
Source File: MultiTaskListener.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public void add(TaskListener listener) {
    for (TaskListener l: listeners) {
        if (ccw.unwrap(l) == listener)
            throw new IllegalStateException();
    }
    listeners = Arrays.copyOf(listeners, listeners.length + 1);
    listeners[listeners.length - 1] = ccw.wrap(listener);
}
 
Example #30
Source File: MultiTaskListener.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void finished(TaskEvent e) {
    // guard against listeners being updated by a listener
    TaskListener[] ll = this.listeners;
    for (TaskListener l: ll)
        l.finished(e);
}