本文整理汇总了Java中com.sun.tools.javac.api.JavacTaskImpl.generate方法的典型用法代码示例。如果您正苦于以下问题:Java JavacTaskImpl.generate方法的具体用法?Java JavacTaskImpl.generate怎么用?Java JavacTaskImpl.generate使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.sun.tools.javac.api.JavacTaskImpl
的用法示例。
在下文中一共展示了JavacTaskImpl.generate方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: run
import com.sun.tools.javac.api.JavacTaskImpl; //导入方法依赖的package包/类
public void run(JavaCompiler comp) {
JavaSource source = new JavaSource();
JavacTaskImpl ct = (JavacTaskImpl)comp.getTask(null, null, dc,
Arrays.asList("-g"), null, Arrays.asList(source));
Context context = ct.getContext();
Symtab syms = Symtab.instance(context);
Names names = Names.instance(context);
Types types = Types.instance(context);
ct.addTaskListener(new Indifier(syms, names, types));
try {
ct.generate();
} catch (Throwable t) {
t.printStackTrace();
throw new AssertionError(
String.format("Error thrown when compiling following code\n%s",
source.source));
}
if (dc.diagFound) {
throw new AssertionError(
String.format("Diags found when compiling following code\n%s\n\n%s",
source.source, dc.printDiags()));
}
verifyBytecode();
}
示例2: testParseEnterAnalyze
import com.sun.tools.javac.api.JavacTaskImpl; //导入方法依赖的package包/类
@Test
public void testParseEnterAnalyze(Path base) throws Exception {
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
try (StandardJavaFileManager fm = compiler.getStandardFileManager(null, null, null)) {
Path moduleSrc = base.resolve("module-src");
Path m1 = moduleSrc.resolve("m1x");
tb.writeJavaFiles(m1, "module m1x { }",
"package p;",
"package p; class T { }");
Path classes = base.resolve("classes");
Iterable<? extends JavaFileObject> files = fm.getJavaFileObjects(findJavaFiles(moduleSrc));
List<String> options = Arrays.asList("-d", classes.toString(), "-Xpkginfo:always");
JavacTaskImpl task = (JavacTaskImpl) compiler.getTask(null, fm, null, options, null, files);
Iterable<? extends CompilationUnitTree> parsed = task.parse();
Iterable<? extends Element> entered = task.enter(parsed);
Iterable<? extends Element> analyzed = task.analyze(entered);
Iterable<? extends JavaFileObject> generatedFiles = task.generate(analyzed);
Set<String> generated = new HashSet<>();
for (JavaFileObject jfo : generatedFiles) {
generated.add(jfo.getName());
}
Set<String> expected = new HashSet<>(
Arrays.asList(Paths.get("testParseEnterAnalyze", "classes", "p", "package-info.class").toString(),
Paths.get("testParseEnterAnalyze", "classes", "module-info.class").toString(),
Paths.get("testParseEnterAnalyze", "classes", "p", "T.class").toString())
);
if (!Objects.equals(expected, generated))
throw new AssertionError("Incorrect generated files: " + generated);
}
}