use of com.sun.source.tree.CompilationUnitTree in project ceylon by eclipse.
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.tree.CompilationUnitTree in project ceylon by eclipse.
the class T6993301 method testExceptionParameterCorrectKind.
public void testExceptionParameterCorrectKind() throws IOException {
final String bootPath = System.getProperty("sun.boot.class.path");
final JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
assert tool != null;
String code = "package test; public class Test { { try { } catch (NullPointerException ex) {} } }";
final JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, Arrays.asList("-bootclasspath", bootPath), null, Arrays.asList(new MyFileObject(code)));
CompilationUnitTree cut = ct.parse().iterator().next();
ct.analyze();
new TreePathScanner<Void, Void>() {
@Override
public Void visitVariable(VariableTree node, Void p) {
Element el = Trees.instance(ct).getElement(getCurrentPath());
assertNotNull(el);
assertEquals(ElementKind.EXCEPTION_PARAMETER, el.getKind());
return super.visitVariable(node, p);
}
}.scan(cut, null);
}
use of com.sun.source.tree.CompilationUnitTree in project ceylon by eclipse.
the class T6557752 method main.
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!");
}
}
use of com.sun.source.tree.CompilationUnitTree in project ceylon by eclipse.
the class T6345974 method main.
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);
}
use of com.sun.source.tree.CompilationUnitTree in project ceylon by eclipse.
the class T6472751 method main.
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);
positions = trees.getSourcePositions();
Iterable<? extends CompilationUnitTree> asts = task.parse();
for (CompilationUnitTree ast : asts) {
new MyVisitor().scan(ast, null);
}
}
Aggregations