use of com.sun.source.util.JavacTask in project ceylon-compiler by ceylon.
the class DisjunctiveTypeWellFormednessTest method run.
void run(JavaCompiler tool, StandardJavaFileManager fm) throws Exception {
JavacTask ct = (JavacTask) tool.getTask(null, fm, diagChecker, null, null, Arrays.asList(source));
ct.analyze();
check();
}
use of com.sun.source.util.JavacTask in project ceylon-compiler by ceylon.
the class Test method test.
void test(File test) throws Exception {
JavacTool tool1 = JavacTool.create();
StandardJavaFileManager fm = tool1.getStandardFileManager(null, null, null);
Iterable<? extends JavaFileObject> files = fm.getJavaFileObjects(test);
// parse test file into a tree, and write it out to a stringbuffer using Pretty
JavacTask t1 = tool1.getTask(null, fm, null, null, null, files);
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
Iterable<? extends CompilationUnitTree> trees = t1.parse();
for (CompilationUnitTree tree : trees) {
new Pretty(pw, true).printExpr((JCTree) tree);
}
pw.close();
final String out = sw.toString();
System.err.println("generated code:\n" + out + "\n");
// verify the generated code is valid Java by compiling it
JavacTool tool2 = JavacTool.create();
JavaFileObject fo = new SimpleJavaFileObject(URI.create("output"), JavaFileObject.Kind.SOURCE) {
@Override
public CharSequence getCharContent(boolean ignoreEncodingErrors) {
return out;
}
};
JavacTask t2 = tool2.getTask(null, fm, null, null, null, Collections.singleton(fo));
boolean ok = t2.call();
if (!ok)
throw new Exception("compilation of generated code failed");
File expectedClass = new File(test.getName().replace(".java", ".class"));
if (!expectedClass.exists())
throw new Exception(expectedClass + " not found");
}
use of com.sun.source.util.JavacTask in project ceylon-compiler by ceylon.
the class AbstractTreeScannerTest method read.
/**
* 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;
}
use of com.sun.source.util.JavacTask in project ceylon-compiler by ceylon.
the class T6963934 method main.
public static void main(String[] args) throws Exception {
File testSrc = new File(System.getProperty("test.src"));
File thisSrc = new File(testSrc, T6963934.class.getSimpleName() + ".java");
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
JavacTask task = (JavacTask) compiler.getTask(null, fileManager, null, null, null, fileManager.getJavaFileObjects(thisSrc));
CompilationUnitTree tree = task.parse().iterator().next();
int count = 0;
for (ImportTree importTree : tree.getImports()) {
System.out.println(importTree);
count++;
}
int expected = 7;
if (count != expected)
throw new Exception("unexpected number of imports found: " + count + ", expected: " + expected);
}
use of com.sun.source.util.JavacTask in project ceylon-compiler by ceylon.
the class TreePosTest method read.
/**
* 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;
}
Aggregations