本文整理匯總了Java中com.sun.source.util.JavacTask.setTaskListener方法的典型用法代碼示例。如果您正苦於以下問題:Java JavacTask.setTaskListener方法的具體用法?Java JavacTask.setTaskListener怎麽用?Java JavacTask.setTaskListener使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類com.sun.source.util.JavacTask
的用法示例。
在下文中一共展示了JavacTask.setTaskListener方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: getTask
import com.sun.source.util.JavacTask; //導入方法依賴的package包/類
/**
* 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;
}
示例2: run
import com.sun.source.util.JavacTask; //導入方法依賴的package包/類
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");
}
示例3: test
import com.sun.source.util.JavacTask; //導入方法依賴的package包/類
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();
}