本文整理汇总了Java中com.sun.tools.javac.api.JavacTool类的典型用法代码示例。如果您正苦于以下问题:Java JavacTool类的具体用法?Java JavacTool怎么用?Java JavacTool使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
JavacTool类属于com.sun.tools.javac.api包,在下文中一共展示了JavacTool类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: PubApiExtractor
import com.sun.tools.javac.api.JavacTool; //导入依赖的package包/类
/**
* Setup a compilation context, used for reading public apis of classes on the classpath
* as well as annotation processors.
*/
public PubApiExtractor(Options options) {
JavacTool compiler = com.sun.tools.javac.api.JavacTool.create();
fileManager = new SmartFileManager(compiler.getStandardFileManager(null, null, null));
context = new com.sun.tools.javac.util.Context();
String[] args = options.prepJavacArgs();
task = compiler.getTask(new PrintWriter(System.err),
fileManager,
null,
Arrays.asList(args),
null,
null,
context);
// Trigger a creation of the JavaCompiler, necessary to get a sourceCompleter for ClassFinder.
// The sourceCompleter is used for build situations where a classpath class references other classes
// that happens to be on the sourcepath.
JavaCompiler.instance(context);
// context.put(JavaFileManager.class, fileManager);
}
示例2: run
import com.sun.tools.javac.api.JavacTool; //导入依赖的package包/类
void run() throws IOException {
JavacTool tool = JavacTool.create();
JavaSource source = new JavaSource("class Test {" +
" I i = STOP -> {};" +
" interface I {" +
" public void test(int i) {}" +
" }" +
"}");
Context context = new Context();
CrashingAttr.preRegister(context);
List<JavaSource> inputs = Arrays.asList(source);
JavacTaskImpl task =
(JavacTaskImpl) tool.getTask(null, null, null, null, null, inputs, context);
try {
task.analyze(null);
throw new AssertionError("Expected exception not seen.");
} catch (StopException ex) {
//ok
}
}
示例3: test
import com.sun.tools.javac.api.JavacTool; //导入依赖的package包/类
static void test(JavacFileManager fm, JavaFileObject f, List<String> addExports, String... args) throws Throwable {
List<String> allArgs = new ArrayList<>();
allArgs.addAll(addExports);
allArgs.addAll(Arrays.asList(args));
Context context = new Context();
JavacTool tool = JavacTool.create();
JavacTaskImpl task = (JavacTaskImpl) tool.getTask(null, fm, null, allArgs, null, List.of(f), context);
task.call();
JavaCompiler c = JavaCompiler.instance(context);
if (c.errorCount() != 0)
throw new AssertionError("compilation failed");
long msec = c.elapsed_msec;
if (msec < 0 || msec > 5 * 60 * 1000) // allow test 5 mins to execute, should be more than enough!
throw new AssertionError("elapsed time is suspect: " + msec);
}
示例4: parse
import com.sun.tools.javac.api.JavacTool; //导入依赖的package包/类
static List<? extends Tree> parse(String srcfile) throws Exception {
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
Iterable<? extends JavaFileObject> fileObjects = fileManager.getJavaFileObjects(srcfile);
String classPath = System.getProperty("java.class.path");
List<String> options = Arrays.asList("-classpath", classPath);
DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<>();
Context context = new Context();
JavacTaskImpl task = (JavacTaskImpl) ((JavacTool) compiler).getTask(null, null,
diagnostics, options, null, fileObjects, context);
TrialParserFactory.instance(context);
Iterable<? extends CompilationUnitTree> asts = task.parse();
Iterator<? extends CompilationUnitTree> it = asts.iterator();
if (it.hasNext()) {
CompilationUnitTree cut = it.next();
return cut.getTypeDecls();
} else {
throw new AssertionError("Expected compilation unit");
}
}
示例5: run
import com.sun.tools.javac.api.JavacTool; //导入依赖的package包/类
void run() throws Exception {
File testSrc = new File(System.getProperty("test.src"));
JavacTool tool = JavacTool.create();
try (StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null)) {
File f = new File(testSrc, T6993305.class.getSimpleName() + ".java");
Iterable<? extends JavaFileObject> fos = fm.getJavaFileObjects(f);
JavacTask task = tool.getTask(null, fm, null, null, null, fos);
Iterable<? extends CompilationUnitTree> cus = task.parse();
TestScanner s = new TestScanner();
s.scan(cus, task);
if (errors > 0)
throw new Exception(errors + " errors occurred");
}
}
示例6: read
import com.sun.tools.javac.api.JavacTool; //导入依赖的package包/类
/**
* Read a file.
* @param file the file to be read
* @return the tree for the content of the file
* @throws IOException if any IO errors occur
* @throws TreePosTest.ParseException if any errors occur while parsing the file
*/
JCCompilationUnit read(File file) throws IOException, ParseException {
JavacTool tool = JavacTool.create();
r.errors = 0;
Iterable<? extends JavaFileObject> files = fm.getJavaFileObjects(file);
JavacTask task = tool.getTask(pw, fm, r, Collections.<String>emptyList(), null, files);
Iterable<? extends CompilationUnitTree> trees = task.parse();
pw.flush();
if (r.errors > 0)
throw new ParseException(sw.toString());
Iterator<? extends CompilationUnitTree> iter = trees.iterator();
if (!iter.hasNext())
throw new Error("no trees found");
JCCompilationUnit t = (JCCompilationUnit) iter.next();
if (iter.hasNext())
throw new Error("too many trees found");
return t;
}
示例7: read
import com.sun.tools.javac.api.JavacTool; //导入依赖的package包/类
/**
* Read a file.
* @param file the file to be read
* @return the tree for the content of the file
* @throws IOException if any IO errors occur
* @throws TreePosTest.ParseException if any errors occur while parsing the file
*/
JCCompilationUnit read(File file) throws IOException, ParseException {
JavacTool tool = JavacTool.create();
r.errors = 0;
Iterable<? extends JavaFileObject> files = fm.getJavaFileObjects(file);
JavacTask task = tool.getTask(pw, fm, r, List.of("-proc:none"), null, files);
Iterable<? extends CompilationUnitTree> trees = task.parse();
pw.flush();
if (r.errors > 0)
throw new ParseException(sw.toString());
Iterator<? extends CompilationUnitTree> iter = trees.iterator();
if (!iter.hasNext())
throw new Error("no trees found");
JCCompilationUnit t = (JCCompilationUnit) iter.next();
if (iter.hasNext())
throw new Error("too many trees found");
return t;
}
示例8: main
import com.sun.tools.javac.api.JavacTool; //导入依赖的package包/类
public static void main(String[] args) throws IOException {
JavacTool tool = JavacTool.create();
try (StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null)) {
Iterable<? extends JavaFileObject> files =
fm.getJavaFileObjectsFromFiles(Arrays.asList(new File(testSrcDir, self + ".java")));
Iterable<String> options = Arrays.asList(
"--add-exports", "jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED",
"--processor-path", testClassDir,
"-processor", self,
"-s", ".",
"-d", ".");
JavacTask task = tool.getTask(out, fm, null, options, null, files);
VerifyingTaskListener vtl = new VerifyingTaskListener(new File(testSrcDir, self + ".out"));
task.setTaskListener(vtl);
if (!task.call())
throw new AssertionError("compilation failed");
if (vtl.iter.hasNext() || vtl.errors)
throw new AssertionError("comparison against golden file failed.");
}
}
示例9: main
import com.sun.tools.javac.api.JavacTool; //导入依赖的package包/类
public static void main(String[] args) throws Exception {
PrintWriter out = new PrintWriter(System.out, true);
JavacTool tool = JavacTool.create();
try (StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null)) {
File testSrc = new File(System.getProperty("test.src"));
Iterable<? extends JavaFileObject> f =
fm.getJavaFileObjectsFromFiles(Arrays.asList(new File(testSrc, "T6345974.java")));
JavacTask task = tool.getTask(out, fm, null, null, null, f);
Iterable<? extends CompilationUnitTree> trees = task.parse();
out.flush();
Scanner s = new Scanner();
for (CompilationUnitTree t: trees)
s.scan(t, null);
}
}
示例10: run
import com.sun.tools.javac.api.JavacTool; //导入依赖的package包/类
/**
* method-run.
*/
void run() throws Exception {
File testSrc = new File(System.getProperty("test.src"));
File file = new File(testSrc, "TestDocComments.java");
JavacTool tool = JavacTool.create();
try (StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null)) {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
Iterable<? extends JavaFileObject> fileObjects = fm.getJavaFileObjects(file);
JavacTask task = tool.getTask(pw, fm, null, null, null, fileObjects);
Iterable<? extends CompilationUnitTree> units = task.parse();
Trees trees = Trees.instance(task);
CommentScanner s = new CommentScanner();
int n = s.scan(units, trees);
if (n != 12)
error("Unexpected number of doc comments found: " + n);
if (errors > 0)
throw new Exception(errors + " errors occurred");
}
}
示例11: run
import com.sun.tools.javac.api.JavacTool; //导入依赖的package包/类
void run(String resourceSpecification, boolean expected) throws IOException {
String template = "public class Test implements AutoCloseable {\n" +
" void t() {\n" +
" try (Test resource = RESOURCE) { }\n" +
" }\n" +
" public void close() { }\n" +
"}\n";
String code = template.replace("RESOURCE", resourceSpecification);
Context ctx = new Context();
DumpLower.preRegister(ctx);
Iterable<ToolBox.JavaSource> files = Arrays.asList(new ToolBox.JavaSource(code));
JavacTask task = JavacTool.create().getTask(null, null, null, null, null, files, ctx);
task.call();
boolean hasNullCheck = ((DumpLower) DumpLower.instance(ctx)).hasNullCheck;
if (hasNullCheck != expected) {
throw new IllegalStateException("expected: " + expected +
"; actual: " + hasNullCheck +
"; code: " + code);
}
}
示例12: run
import com.sun.tools.javac.api.JavacTool; //导入依赖的package包/类
void run(String trySpec, boolean expected) throws IOException {
String template = "public class Test implements AutoCloseable {\n" +
" void t(int i) {\n" +
" TRY\n" +
" }\n" +
" public void close() { }\n" +
"}\n";
String code = template.replace("TRY", trySpec);
Context ctx = new Context();
DumpLower.preRegister(ctx);
Iterable<ToolBox.JavaSource> files = Arrays.asList(new ToolBox.JavaSource(code));
JavacTask task = JavacTool.create().getTask(null, null, null, null, null, files, ctx);
task.call();
boolean actual = ((DumpLower) DumpLower.instance(ctx)).closeSeen;
if (expected != actual) {
throw new IllegalStateException("expected: " + expected + "; actual: " + actual + "; code:\n" + code);
}
}
示例13: testNoAnnotationProcessing
import com.sun.tools.javac.api.JavacTool; //导入依赖的package包/类
static void testNoAnnotationProcessing(JavacFileManager fm, List<JavaFileObject> files) throws Throwable {
Context context = new Context();
String[] args = { "-d", "." };
JavacTool tool = JavacTool.create();
JavacTask task = tool.getTask(null, fm, null, List.from(args), null, files, context);
// no need in this simple case to call task.prepareCompiler(false)
JavaCompiler compiler = JavaCompiler.instance(context);
compiler.compile(files);
try {
compiler.compile(files);
throw new Error("Error: AssertionError not thrown after second call of compile");
} catch (AssertionError e) {
System.err.println("Exception from compiler (expected): " + e);
}
}
示例14: testAnnotationProcessing
import com.sun.tools.javac.api.JavacTool; //导入依赖的package包/类
static void testAnnotationProcessing(JavacFileManager fm, List<JavaFileObject> files) throws Throwable {
Context context = new Context();
String[] args = {
"-XprintRounds",
"-processorpath", testClasses,
"-processor", self,
"-d", "."
};
JavacTool tool = JavacTool.create();
JavacTask task = tool.getTask(null, fm, null, List.from(args), null, files, context);
// no need in this simple case to call task.prepareCompiler(false)
JavaCompiler compiler = JavaCompiler.instance(context);
compiler.compile(files);
try {
compiler.compile(files);
throw new Error("Error: AssertionError not thrown after second call of compile");
} catch (AssertionError e) {
System.err.println("Exception from compiler (expected): " + e);
}
}
示例15: main
import com.sun.tools.javac.api.JavacTool; //导入依赖的package包/类
public static void main(String... args) throws Exception {
// Create a single file manager and reuse it for each compile to save time.
try (StandardJavaFileManager fm = JavacTool.create().getStandardFileManager(null, null, null)) {
for (ClassKind ck1 : ClassKind.values()) {
String cname1 = "C1";
for (ClassKind ck2 : ClassKind.values()) {
if (!ck1.isAllowed(ck2)) continue;
String cname2 = "C2";
for (ClassKind ck3 : ClassKind.values()) {
if (!ck2.isAllowed(ck3)) continue;
String cname3 = "C3";
new T7003595(fm, new ClassKind[] {ck1, ck2, ck3}, new String[] { cname1, cname2, cname3 }).compileAndCheck();
}
}
}
}
System.out.println("Total checks made: " + checkCount);
}