Java Code Examples for com.sun.tools.javac.api.JavacTool#getTask()
The following examples show how to use
com.sun.tools.javac.api.JavacTool#getTask() .
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: T6430241.java From openjdk-8-source with GNU General Public License v2.0 | 6 votes |
void testTaskAPI(boolean expectWarnings, Iterable<? extends File> pcp) throws Exception { System.err.println("test task API: " + pcp); JavacTool tool = JavacTool.create(); StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null); if (pcp != null) fm.setLocation(StandardLocation.PLATFORM_CLASS_PATH, pcp); Iterable<? extends JavaFileObject> files = fm.getJavaFileObjects(testFile); StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); JavacTask task = tool.getTask(pw, fm, null, null, null, files); boolean ok = task.call(); String out = showOutput(sw.toString()); checkCompilationOK(ok); checkOutput(out, expectWarnings); }
Example 2
Source File: T6430241.java From openjdk-jdk8u-backup with GNU General Public License v2.0 | 6 votes |
void testTaskAPI(boolean expectWarnings, Iterable<? extends File> pcp) throws Exception { System.err.println("test task API: " + pcp); JavacTool tool = JavacTool.create(); StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null); if (pcp != null) fm.setLocation(StandardLocation.PLATFORM_CLASS_PATH, pcp); Iterable<? extends JavaFileObject> files = fm.getJavaFileObjects(testFile); StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); JavacTask task = tool.getTask(pw, fm, null, null, null, files); boolean ok = task.call(); String out = showOutput(sw.toString()); checkCompilationOK(ok); checkOutput(out, expectWarnings); }
Example 3
Source File: T6993305.java From jdk8u60 with GNU General Public License v2.0 | 6 votes |
void run() throws Exception { File testSrc = new File(System.getProperty("test.src")); JavacTool tool = JavacTool.create(); 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"); }
Example 4
Source File: AbstractTreeScannerTest.java From openjdk-jdk8u-backup with GNU General Public License v2.0 | 6 votes |
/** * 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; }
Example 5
Source File: TreePosTest.java From openjdk-8 with GNU General Public License v2.0 | 6 votes |
/** * 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; }
Example 6
Source File: T6410706.java From openjdk-jdk9 with GNU General Public License v2.0 | 6 votes |
public static void main(String... args) throws IOException { String testSrc = System.getProperty("test.src", "."); String testClasses = System.getProperty("test.classes", "."); JavacTool tool = JavacTool.create(); MyDiagListener dl = new MyDiagListener(); try (StandardJavaFileManager fm = tool.getStandardFileManager(dl, null, null)) { fm.setLocation(StandardLocation.CLASS_OUTPUT, Arrays.asList(new File(testClasses))); Iterable<? extends JavaFileObject> files = fm.getJavaFileObjectsFromFiles(Arrays.asList(new File(testSrc, T6410706.class.getName()+".java"))); JavacTask task = tool.getTask(null, fm, dl, null, null, files); task.parse(); task.analyze(); task.generate(); // expect 2 notes: // Note: T6410706.java uses or overrides a deprecated API. // Note: Recompile with -Xlint:deprecation for details. if (dl.notes != 2) throw new AssertionError(dl.notes + " notes given"); } }
Example 7
Source File: ScopeTest.java From jdk8u60 with GNU General Public License v2.0 | 5 votes |
private static void verifyLambdaScopeCorrect(final String packageClause) throws Exception { JavacTool tool = JavacTool.create(); JavaFileObject source = new SimpleJavaFileObject(URI.create("mem://Test.java"), Kind.SOURCE) { @Override public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException { return packageClause + SOURCE_CODE; } @Override public boolean isNameCompatible(String simpleName, Kind kind) { return true; } }; Iterable<? extends JavaFileObject> fos = Collections.singletonList(source); JavacTask task = tool.getTask(null, null, null, new ArrayList<String>(), null, fos); final Types types = JavacTypes.instance(((JavacTaskImpl) task).getContext()); final Trees trees = Trees.instance(task); CompilationUnitTree cu = task.parse().iterator().next(); task.analyze(); new TreePathScanner<Void, Void>() { @Override public Void visitMemberSelect(MemberSelectTree node, Void p) { if (node.getIdentifier().contentEquals("correct")) { TypeMirror xType = trees.getTypeMirror(new TreePath(getCurrentPath(), node.getExpression())); Scope scope = trees.getScope(getCurrentPath()); for (Element l : scope.getLocalElements()) { if (!l.getSimpleName().contentEquals("x")) continue; if (!types.isSameType(xType, l.asType())) { throw new IllegalStateException("Incorrect variable type in scope: " + l.asType() + "; should be: " + xType); } } } return super.visitMemberSelect(node, p); } }.scan(cu, null); }
Example 8
Source File: JavacTurbineTest.java From bazel with Apache License 2.0 | 5 votes |
private void compileLib( Path jar, Collection<Path> classpath, Iterable<? extends JavaFileObject> units) throws IOException { final Path outdir = temp.newFolder().toPath(); JavacFileManager fm = new JavacFileManager(new Context(), false, UTF_8); fm.setLocationFromPaths(StandardLocation.CLASS_OUTPUT, Collections.singleton(outdir)); fm.setLocationFromPaths(StandardLocation.CLASS_PATH, classpath); List<String> options = ImmutableList.of("-d", outdir.toString()); JavacTool tool = JavacTool.create(); JavacTask task = tool.getTask( new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.err, UTF_8)), true), fm, null, options, null, units); assertThat(task.call()).isTrue(); try (JarOutputStream jos = new JarOutputStream(Files.newOutputStream(jar))) { Files.walkFileTree( outdir, new SimpleFileVisitor<Path>() { @Override public FileVisitResult visitFile(Path path, BasicFileAttributes attrs) throws IOException { JarEntry je = new JarEntry(outdir.relativize(path).toString()); jos.putNextEntry(je); Files.copy(path, jos); return FileVisitResult.CONTINUE; } }); } }
Example 9
Source File: RunTest.java From openjdk-8 with GNU General Public License v2.0 | 5 votes |
void testInit(boolean expectOK, String[] args, List<? extends JavaFileObject> files) { JavacTool javac = JavacTool.create(); JavacTask task = javac.getTask(null, null, null, null, null, files); try { DocLint dl = new DocLint(); dl.init(task, args, true); if (!expectOK) error("expected IllegalArgumentException not thrown"); task.call(); } catch (IllegalArgumentException e) { System.err.println(e); if (expectOK) error("unexpected IllegalArgumentException caught"); } }
Example 10
Source File: MakeQualIdent.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
public static void main(String... args) throws Exception { JavacTool tool = JavacTool.create(); JavacTask task = tool.getTask(null, null, null, new ArrayList<String>(), null, null); Context ctx = ((JavacTaskImpl)task).getContext(); TreeMaker treeMaker = TreeMaker.instance(ctx); Symtab syms = Symtab.instance(ctx); String stringTree = printTree(treeMaker.QualIdent(syms.stringType.tsym)); if (!"java.lang.String".equals(stringTree)) { throw new IllegalStateException(stringTree); } }
Example 11
Source File: T6345974.java From TencentKona-8 with GNU General Public License v2.0 | 5 votes |
public static void main(String[] args) throws Exception { PrintWriter out = new PrintWriter(System.out, true); JavacTool tool = JavacTool.create(); 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); }
Example 12
Source File: PackageGenerator.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
public PackageGenerator() { JavacTool jt = JavacTool.create(); JavacTask task = jt.getTask(null, null, null, null, null, null); Context ctx = ((JavacTaskImpl)task).getContext(); make = TreeMaker.instance(ctx); names = Names.instance(ctx); syms = Symtab.instance(ctx); factory = DocumentBuilderFactory.newInstance(); documentifier = Documentifier.instance(ctx); }
Example 13
Source File: T6345974.java From hottub with GNU General Public License v2.0 | 5 votes |
public static void main(String[] args) throws Exception { PrintWriter out = new PrintWriter(System.out, true); JavacTool tool = JavacTool.create(); 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); }
Example 14
Source File: GenStubs.java From openjdk-jdk8u with GNU General Public License v2.0 | 5 votes |
public boolean run(String sourcepath, File outdir, List<String> classes) { //System.err.println("run: sourcepath:" + sourcepath + " outdir:" + outdir + " classes:" + classes); if (sourcepath == null) throw new IllegalArgumentException("sourcepath not set"); if (outdir == null) throw new IllegalArgumentException("source output dir not set"); JavacTool tool = JavacTool.create(); StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null); try { fm.setLocation(StandardLocation.SOURCE_OUTPUT, Collections.singleton(outdir)); fm.setLocation(StandardLocation.SOURCE_PATH, splitPath(sourcepath)); List<JavaFileObject> files = new ArrayList<JavaFileObject>(); for (String c: classes) { JavaFileObject fo = fm.getJavaFileForInput( StandardLocation.SOURCE_PATH, c, JavaFileObject.Kind.SOURCE); if (fo == null) error("class not found: " + c); else files.add(fo); } JavacTask t = tool.getTask(null, fm, null, null, null, files); Iterable<? extends CompilationUnitTree> trees = t.parse(); for (CompilationUnitTree tree: trees) { makeStub(fm, tree); } } catch (IOException e) { error("IO error " + e, e); } return (errors == 0); }
Example 15
Source File: RunTest.java From openjdk-8-source with GNU General Public License v2.0 | 5 votes |
void testInit(boolean expectOK, String[] args, List<? extends JavaFileObject> files) { JavacTool javac = JavacTool.create(); JavacTask task = javac.getTask(null, null, null, null, null, files); try { DocLint dl = new DocLint(); dl.init(task, args, true); if (!expectOK) error("expected IllegalArgumentException not thrown"); task.call(); } catch (IllegalArgumentException e) { System.err.println(e); if (expectOK) error("unexpected IllegalArgumentException caught"); } }
Example 16
Source File: ScopeTest.java From openjdk-8 with GNU General Public License v2.0 | 5 votes |
private static void verifyLambdaScopeCorrect(final String packageClause) throws Exception { JavacTool tool = JavacTool.create(); JavaFileObject source = new SimpleJavaFileObject(URI.create("mem://Test.java"), Kind.SOURCE) { @Override public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException { return packageClause + SOURCE_CODE; } @Override public boolean isNameCompatible(String simpleName, Kind kind) { return true; } }; Iterable<? extends JavaFileObject> fos = Collections.singletonList(source); JavacTask task = tool.getTask(null, null, null, new ArrayList<String>(), null, fos); final Types types = JavacTypes.instance(((JavacTaskImpl) task).getContext()); final Trees trees = Trees.instance(task); CompilationUnitTree cu = task.parse().iterator().next(); task.analyze(); new TreePathScanner<Void, Void>() { @Override public Void visitMemberSelect(MemberSelectTree node, Void p) { if (node.getIdentifier().contentEquals("correct")) { TypeMirror xType = trees.getTypeMirror(new TreePath(getCurrentPath(), node.getExpression())); Scope scope = trees.getScope(getCurrentPath()); for (Element l : scope.getLocalElements()) { if (!l.getSimpleName().contentEquals("x")) continue; if (!types.isSameType(xType, l.asType())) { throw new IllegalStateException("Incorrect variable type in scope: " + l.asType() + "; should be: " + xType); } } } return super.visitMemberSelect(node, p); } }.scan(cu, null); }
Example 17
Source File: TestSuppression.java From jdk8u60 with GNU General Public License v2.0 | 4 votes |
void test(String src, WarningKind wk, int gen) throws Exception { count++; System.err.println("Test " + count + ": wk:" + wk + " gen:" + gen + " src:" +src); File testDir = new File("test" + count); File srcDir = createDir(testDir, "src"); File gensrcDir = createDir(testDir, "gensrc"); File classesDir = createDir(testDir, "classes"); File x = writeFile(new File(srcDir, "X.java"), src); DiagListener dl = new DiagListener(); JavacTool tool = JavacTool.create(); StandardJavaFileManager fm = tool.getStandardFileManager(dl, null, null); fm.setLocation(StandardLocation.CLASS_PATH, Arrays.asList(classesDir, new File(System.getProperty("test.classes")))); fm.setLocation(StandardLocation.CLASS_OUTPUT, Collections.singleton(classesDir)); fm.setLocation(StandardLocation.SOURCE_OUTPUT, Collections.singleton(gensrcDir)); List<String> args = new ArrayList<String>(); // args.add("-XprintProcessorInfo"); args.add("-XprintRounds"); args.add("-Agen=" + gen); if (wk == WarningKind.YES) args.add("-Xlint:serial"); Iterable<? extends JavaFileObject> files = fm.getJavaFileObjects(x); StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); JavacTask task = tool.getTask(pw, fm, dl, args, null, files); task.setProcessors(Arrays.asList(new AnnoProc())); boolean ok = task.call(); pw.close(); System.err.println("ok:" + ok + " diags:" + dl.counts); if (sw.toString().length() > 0) { System.err.println("output:\n" + sw.toString()); } for (Diagnostic.Kind dk: Diagnostic.Kind.values()) { Integer v = dl.counts.get(dk); int found = (v == null) ? 0 : v; int expect = (dk == Diagnostic.Kind.WARNING && wk == WarningKind.YES) ? gen : 0; if (found != expect) { error("Unexpected value for " + dk + ": expected: " + expect + " found: " + found); } } System.err.println(); }
Example 18
Source File: TestClose2.java From jdk8u60 with GNU General Public License v2.0 | 4 votes |
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( "-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"); }
Example 19
Source File: TestSuppression.java From openjdk-8 with GNU General Public License v2.0 | 4 votes |
void test(String src, WarningKind wk, int gen) throws Exception { count++; System.err.println("Test " + count + ": wk:" + wk + " gen:" + gen + " src:" +src); File testDir = new File("test" + count); File srcDir = createDir(testDir, "src"); File gensrcDir = createDir(testDir, "gensrc"); File classesDir = createDir(testDir, "classes"); File x = writeFile(new File(srcDir, "X.java"), src); DiagListener dl = new DiagListener(); JavacTool tool = JavacTool.create(); StandardJavaFileManager fm = tool.getStandardFileManager(dl, null, null); fm.setLocation(StandardLocation.CLASS_PATH, Arrays.asList(classesDir, new File(System.getProperty("test.classes")))); fm.setLocation(StandardLocation.CLASS_OUTPUT, Collections.singleton(classesDir)); fm.setLocation(StandardLocation.SOURCE_OUTPUT, Collections.singleton(gensrcDir)); List<String> args = new ArrayList<String>(); // args.add("-XprintProcessorInfo"); args.add("-XprintRounds"); args.add("-Agen=" + gen); if (wk == WarningKind.YES) args.add("-Xlint:serial"); Iterable<? extends JavaFileObject> files = fm.getJavaFileObjects(x); StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); JavacTask task = tool.getTask(pw, fm, dl, args, null, files); task.setProcessors(Arrays.asList(new AnnoProc())); boolean ok = task.call(); pw.close(); System.err.println("ok:" + ok + " diags:" + dl.counts); if (sw.toString().length() > 0) { System.err.println("output:\n" + sw.toString()); } for (Diagnostic.Kind dk: Diagnostic.Kind.values()) { Integer v = dl.counts.get(dk); int found = (v == null) ? 0 : v; int expect = (dk == Diagnostic.Kind.WARNING && wk == WarningKind.YES) ? gen : 0; if (found != expect) { error("Unexpected value for " + dk + ": expected: " + expect + " found: " + found); } } System.err.println(); }
Example 20
Source File: DocLint.java From openjdk-jdk9 with GNU General Public License v2.0 | 4 votes |
public void run(PrintWriter out, String... args) throws BadArgs, IOException { env = new Env(); processArgs(args); boolean noFiles = javacFiles.isEmpty(); if (needHelp) { showHelp(out); if (noFiles) return; } else if (noFiles) { out.println(localize("dc.main.no.files.given")); return; } JavacTool tool = JavacTool.create(); JavacFileManager fm = new JavacFileManager(new Context(), false, null); fm.setSymbolFileEnabled(false); if (javacBootClassPath != null) { fm.setLocation(StandardLocation.PLATFORM_CLASS_PATH, javacBootClassPath); } if (javacClassPath != null) { fm.setLocation(StandardLocation.CLASS_PATH, javacClassPath); } if (javacSourcePath != null) { fm.setLocation(StandardLocation.SOURCE_PATH, javacSourcePath); } JavacTask task = tool.getTask(out, fm, null, javacOpts, null, fm.getJavaFileObjectsFromFiles(javacFiles)); Iterable<? extends CompilationUnitTree> units = task.parse(); ((JavacTaskImpl) task).enter(); env.init(task); checker = new Checker(env); DeclScanner ds = new DeclScanner(env) { @Override void visitDecl(Tree tree, Name name) { TreePath p = getCurrentPath(); DocCommentTree dc = env.trees.getDocCommentTree(p); checker.scan(dc, p); } }; ds.scan(units, null); reportStats(out); Context ctx = ((JavacTaskImpl) task).getContext(); JavaCompiler c = JavaCompiler.instance(ctx); c.printCount("error", c.errorCount()); c.printCount("warn", c.warningCount()); }