Java Code Examples for com.sun.source.util.JavacTask#parse()
The following examples show how to use
com.sun.source.util.JavacTask#parse() .
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: FieldOverloadKindNotAssignedTest.java From openjdk-jdk9 with GNU General Public License v2.0 | 6 votes |
void run() throws Exception { Context context = new Context(); JavacFileManager.preRegister(context); final JavaCompiler tool = ToolProvider.getSystemJavaCompiler(); JavacTask ct = (JavacTask)tool.getTask(null, null, null, null, null, Arrays.asList(new JavaSource())); Iterable<? extends CompilationUnitTree> elements = ct.parse(); ct.analyze(); Assert.check(elements.iterator().hasNext()); JCTree topLevel = (JCTree)elements.iterator().next(); new TreeScanner() { @Override public void visitReference(JCMemberReference tree) { Assert.check(tree.getOverloadKind() != null); } }.scan(topLevel); }
Example 2
Source File: T6557752.java From openjdk-8-source with GNU General Public License v2.0 | 6 votes |
public static void main(String[] args) throws IOException { JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); task = (JavacTask) compiler.getTask(null, null, null, null, null, List.of(new MyFileObject())); Iterable<? extends CompilationUnitTree> asts = task.parse(); task.analyze(); trees = Trees.instance(task); MyVisitor myVisitor = new MyVisitor(); for (CompilationUnitTree ast : asts) { myVisitor.compilationUnit = ast; myVisitor.scan(ast, null); } if (!myVisitor.foundError) { throw new AssertionError("Expected error not found!"); } }
Example 3
Source File: T6852595.java From openjdk-8-source with GNU General Public License v2.0 | 6 votes |
public static void main(String[] args) throws IOException { JavaFileObject sfo = new SimpleJavaFileObject(URI.create("myfo:/Test.java"),Kind.SOURCE) { public CharSequence getCharContent(boolean ignoreEncodingErrors) { return "class BadName { Object o = j; }"; } }; List<? extends JavaFileObject> files = Arrays.asList(sfo); JavaCompiler tool = ToolProvider.getSystemJavaCompiler(); JavacTask ct = (JavacTask)tool.getTask(null, null, null, null, null, files); Iterable<? extends CompilationUnitTree> compUnits = ct.parse(); CompilationUnitTree cu = compUnits.iterator().next(); ClassTree cdef = (ClassTree)cu.getTypeDecls().get(0); JCVariableDecl vdef = (JCVariableDecl)cdef.getMembers().get(0); TreePath path = TreePath.getPath(cu, vdef.init); Trees.instance(ct).getScope(path); }
Example 4
Source File: StringFoldingTest.java From TencentKona-8 with GNU General Public License v2.0 | 6 votes |
void run(boolean disableStringFolding) throws IOException { List<String> argsList = new ArrayList<String>(); if (disableStringFolding) { argsList.add("-XDallowStringFolding=false"); } JavacTask ct = (JavacTask)tool.getTask(null, null, null, argsList, null, Arrays.asList(source)); Iterable<? extends CompilationUnitTree> trees = ct.parse(); String text = trees.toString(); System.out.println(text); if (disableStringFolding) { if (text.contains("FOLDED")) { throw new AssertionError("Expected no string folding"); } if (!text.contains("\"F\"")) { throw new AssertionError("Expected content not found"); } } else { if (!text.contains("FOLDED")) { throw new AssertionError("Expected string folding"); } } }
Example 5
Source File: TreePosTest.java From openjdk-jdk9 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, 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; }
Example 6
Source File: DocTreePathScannerTest.java From openjdk-jdk8u-backup with GNU General Public License v2.0 | 6 votes |
void run() throws Exception { List<File> files = new ArrayList<File>(); File testSrc = new File(System.getProperty("test.src")); for (File f: testSrc.listFiles()) { if (f.isFile() && f.getName().endsWith(".java")) files.add(f); } JavacTool javac = JavacTool.create(); StandardJavaFileManager fm = javac.getStandardFileManager(null, null, null); Iterable<? extends JavaFileObject> fos = fm.getJavaFileObjectsFromFiles(files); JavacTask t = javac.getTask(null, fm, null, null, null, fos); DocTrees trees = DocTrees.instance(t); Iterable<? extends CompilationUnitTree> units = t.parse(); DeclScanner ds = new DeclScanner(trees); for (CompilationUnitTree unit: units) { ds.scan(unit, null); } if (errors > 0) throw new Exception(errors + " errors occurred"); }
Example 7
Source File: T6852595.java From openjdk-jdk8u-backup with GNU General Public License v2.0 | 6 votes |
public static void main(String[] args) throws IOException { JavaFileObject sfo = new SimpleJavaFileObject(URI.create("myfo:/Test.java"),Kind.SOURCE) { public CharSequence getCharContent(boolean ignoreEncodingErrors) { return "class BadName { Object o = j; }"; } }; List<? extends JavaFileObject> files = Arrays.asList(sfo); JavaCompiler tool = ToolProvider.getSystemJavaCompiler(); JavacTask ct = (JavacTask)tool.getTask(null, null, null, null, null, files); Iterable<? extends CompilationUnitTree> compUnits = ct.parse(); CompilationUnitTree cu = compUnits.iterator().next(); ClassTree cdef = (ClassTree)cu.getTypeDecls().get(0); JCVariableDecl vdef = (JCVariableDecl)cdef.getMembers().get(0); TreePath path = TreePath.getPath(cu, vdef.init); Trees.instance(ct).getScope(path); }
Example 8
Source File: InferenceRegressionTest02.java From openjdk-jdk9 with GNU General Public License v2.0 | 6 votes |
void run() throws Exception { Context context = new Context(); JavacFileManager.preRegister(context); Trees trees = JavacTrees.instance(context); StringWriter strOut = new StringWriter(); PrintWriter pw = new PrintWriter(strOut); DPrinter dprinter = new DPrinter(pw, trees); final JavaCompiler tool = ToolProvider.getSystemJavaCompiler(); JavacTask ct = (JavacTask)tool.getTask(null, null, null, null, null, Arrays.asList(new JavaSource())); Iterable<? extends CompilationUnitTree> elements = ct.parse(); ct.analyze(); Assert.check(elements.iterator().hasNext()); dprinter.treeTypes(true).printTree("", (JCTree)elements.iterator().next()); String output = strOut.toString(); Assert.check(!output.contains("java.lang.Object"), "there shouldn't be any type instantiated to Object"); }
Example 9
Source File: T6993305.java From openjdk-jdk8u-backup 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 10
Source File: DocTreePathScannerTest.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
void run() throws Exception { List<File> files = new ArrayList<File>(); File testSrc = new File(System.getProperty("test.src")); for (File f: testSrc.listFiles()) { if (f.isFile() && f.getName().endsWith(".java")) files.add(f); } JavacTool javac = JavacTool.create(); try (StandardJavaFileManager fm = javac.getStandardFileManager(null, null, null)) { Iterable<? extends JavaFileObject> fos = fm.getJavaFileObjectsFromFiles(files); JavacTask t = javac.getTask(null, fm, null, null, null, fos); DocTrees trees = DocTrees.instance(t); Iterable<? extends CompilationUnitTree> units = t.parse(); DeclScanner ds = new DeclScanner(trees); for (CompilationUnitTree unit: units) { ds.scan(unit, null); } if (errors > 0) throw new Exception(errors + " errors occurred"); } }
Example 11
Source File: BadLambdaExpr.java From openjdk-jdk8u-backup with GNU General Public License v2.0 | 5 votes |
void run(JavaCompiler tool, StandardJavaFileManager fm) throws Exception { JavacTask ct = (JavacTask)tool.getTask(null, fm, diagChecker, null, null, Arrays.asList(source)); try { ct.parse(); } catch (Throwable ex) { throw new AssertionError("Error thron when parsing the following source:\n" + source.getCharContent(true)); } check(); }
Example 12
Source File: BadLambdaExpr.java From openjdk-8-source with GNU General Public License v2.0 | 5 votes |
void run(JavaCompiler tool, StandardJavaFileManager fm) throws Exception { JavacTask ct = (JavacTask)tool.getTask(null, fm, diagChecker, null, null, Arrays.asList(source)); try { ct.parse(); } catch (Throwable ex) { throw new AssertionError("Error thron when parsing the following source:\n" + source.getCharContent(true)); } check(); }
Example 13
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 14
Source File: Main.java From openjdk-jdk8u-backup with GNU General Public License v2.0 | 5 votes |
public static void main(String[] args) throws IOException { JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); JavacTask task = (JavacTask) compiler.getTask(null, null, null, null, null, List.of(new MyFileObject())); trees = Trees.instance(task); Iterable<? extends CompilationUnitTree> asts = task.parse(); task.analyze(); for (CompilationUnitTree ast : asts) { new MyVisitor().scan(ast, null); } }
Example 15
Source File: SimpleDocTreeVisitorTest.java From hottub with GNU General Public License v2.0 | 5 votes |
void run() throws Exception { List<File> files = new ArrayList<File>(); File testSrc = new File(System.getProperty("test.src")); for (File f: testSrc.listFiles()) { if (f.isFile() && f.getName().endsWith(".java")) files.add(f); } JavacTool javac = JavacTool.create(); StandardJavaFileManager fm = javac.getStandardFileManager(null, null, null); Iterable<? extends JavaFileObject> fos = fm.getJavaFileObjectsFromFiles(files); JavacTask t = javac.getTask(null, fm, null, null, null, fos); DocTrees trees = DocTrees.instance(t); Iterable<? extends CompilationUnitTree> units = t.parse(); Set<DocTree.Kind> found = EnumSet.noneOf(DocTree.Kind.class); DeclScanner ds = new DeclScanner(trees, found); for (CompilationUnitTree unit: units) { ds.scan(unit, null); } for (DocTree.Kind k: DocTree.Kind.values()) { if (!found.contains(k) && k != DocTree.Kind.OTHER) error("not found: " + k); } if (errors > 0) throw new Exception(errors + " errors occurred"); }
Example 16
Source File: LambdaParserTest.java From TencentKona-8 with GNU General Public License v2.0 | 5 votes |
public void run() { JavacTask ct = (JavacTask)comp.getTask(null, fm.get(), diagChecker, null, null, Arrays.asList(source)); try { ct.parse(); } catch (Throwable ex) { processException(ex); return; } check(); }
Example 17
Source File: Main.java From hottub with GNU General Public License v2.0 | 5 votes |
public static void main(String[] args) throws IOException { JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); JavacTask task = (JavacTask) compiler.getTask(null, null, null, null, null, List.of(new MyFileObject())); trees = Trees.instance(task); Iterable<? extends CompilationUnitTree> asts = task.parse(); task.analyze(); for (CompilationUnitTree ast : asts) { new MyVisitor().scan(ast, null); } }
Example 18
Source File: ParserTest.java From openjdk-jdk8u-backup with GNU General Public License v2.0 | 4 votes |
void run(JavaCompiler tool, StandardJavaFileManager fm) throws Exception { JavacTask ct = (JavacTask)tool.getTask(null, fm, diagChecker, null, null, Arrays.asList(source)); ct.parse(); check(); }
Example 19
Source File: CheckErrorsForSource7.java From jdk8u60 with GNU General Public License v2.0 | 4 votes |
private void run(String... args) throws IOException, URISyntaxException { //the first and only parameter must be the name of the file to be analyzed: if (args.length != 1) throw new IllegalStateException("Must provide source file!"); File testSrc = new File(System.getProperty("test.src")); File testFile = new File(testSrc, args[0]); if (!testFile.canRead()) throw new IllegalStateException("Cannot read the test source"); JavacFileManager fm = JavacTool.create().getStandardFileManager(null, null, null); //gather spans of the @TA annotations into typeAnnotationSpans: JavacTask task = JavacTool.create().getTask(null, fm, null, Collections.<String>emptyList(), null, fm.getJavaFileObjects(testFile)); final Trees trees = Trees.instance(task); final CompilationUnitTree cut = task.parse().iterator().next(); final List<int[]> typeAnnotationSpans = new ArrayList<>(); new TreePathScanner<Void, Void>() { @Override public Void visitAnnotation(AnnotationTree node, Void p) { if (node.getAnnotationType().getKind() == Kind.IDENTIFIER && ((IdentifierTree) node.getAnnotationType()).getName().contentEquals("TA")) { int start = (int) trees.getSourcePositions().getStartPosition(cut, node); int end = (int) trees.getSourcePositions().getEndPosition(cut, node); typeAnnotationSpans.add(new int[] {start, end}); } return null; } }.scan(cut, null); //sort the spans in the reverse order, to simplify removing them from the source: Collections.sort(typeAnnotationSpans, new Comparator<int[]>() { @Override public int compare(int[] o1, int[] o2) { return o2[0] - o1[0]; } }); //verify the errors are produce correctly: String originalSource = cut.getSourceFile().getCharContent(false).toString(); for (int[] toKeep : typeAnnotationSpans) { //prepare updated source code by removing all the annotations except the toKeep one: String updated = originalSource; for (int[] span : typeAnnotationSpans) { if (span == toKeep) continue; updated = updated.substring(0, span[0]) + updated.substring(span[1]); } //parse and verify: JavaFileObject updatedFile = new TestFO(cut.getSourceFile().toUri(), updated); DiagnosticCollector<JavaFileObject> errors = new DiagnosticCollector<>(); JavacTask task2 = JavacTool.create().getTask(null, fm, errors, Arrays.asList("-source", "7"), null, Arrays.asList(updatedFile)); task2.parse(); boolean found = false; for (Diagnostic<? extends JavaFileObject> d : errors.getDiagnostics()) { if (d.getKind() == Diagnostic.Kind.ERROR && EXPECTED_ERRORS.contains(d.getCode())) { if (found) { throw new IllegalStateException("More than one expected error found."); } found = true; } } if (!found) throw new IllegalStateException("Did not produce proper errors for: " + updated); } }
Example 20
Source File: DocLint.java From openjdk-jdk8u with GNU General Public License v2.0 | 4 votes |
public void run(PrintWriter out, String... args) throws BadArgs, IOException { env = new Env(); processArgs(args); if (needHelp) showHelp(out); if (javacFiles.isEmpty()) { if (!needHelp) out.println(localize("dc.main.no.files.given")); } JavacTool tool = JavacTool.create(); JavacFileManager fm = new JavacFileManager(new Context(), false, null); fm.setSymbolFileEnabled(false); fm.setLocation(StandardLocation.PLATFORM_CLASS_PATH, javacBootClassPath); fm.setLocation(StandardLocation.CLASS_PATH, javacClassPath); 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() { @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()); }