use of com.sun.source.tree.CompilationUnitTree in project ceylon-compiler by ceylon.
the class GenStubs method makeStub.
void makeStub(StandardJavaFileManager fm, CompilationUnitTree tree) throws IOException {
CompilationUnitTree tree2 = new StubMaker().translate(tree);
CompilationUnitTree tree3 = new ImportCleaner(fm).removeRedundantImports(tree2);
String className = fm.inferBinaryName(StandardLocation.SOURCE_PATH, tree.getSourceFile());
JavaFileObject fo = fm.getJavaFileForOutput(StandardLocation.SOURCE_OUTPUT, className, JavaFileObject.Kind.SOURCE, null);
// System.err.println("Writing " + className + " to " + fo.getName());
Writer out = fo.openWriter();
try {
new Pretty(out, true).printExpr((JCTree) tree3);
} finally {
out.close();
}
}
use of com.sun.source.tree.CompilationUnitTree in project ceylon-compiler by ceylon.
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);
}
}
use of com.sun.source.tree.CompilationUnitTree in project ceylon-compiler by ceylon.
the class CheckAttributedTree 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
*/
List<Pair<JCCompilationUnit, JCTree>> read(File file) throws IOException, AttributionException {
JavacTool tool = JavacTool.create();
r.errors = 0;
Iterable<? extends JavaFileObject> files = fm.getJavaFileObjects(file);
String[] opts = { "-XDshouldStopPolicy=ATTR", "-XDverboseCompilePolicy" };
JavacTask task = tool.getTask(pw, fm, r, Arrays.asList(opts), null, files);
final List<Element> analyzedElems = new ArrayList<>();
task.setTaskListener(new TaskListener() {
public void started(TaskEvent e) {
if (e.getKind() == TaskEvent.Kind.ANALYZE)
analyzedElems.add(e.getTypeElement());
}
public void finished(TaskEvent e) {
}
});
try {
Iterable<? extends CompilationUnitTree> trees = task.parse();
task.analyze();
List<Pair<JCCompilationUnit, JCTree>> res = new ArrayList<>();
//System.out.println("Try to add pairs. Elems are " + analyzedElems);
for (CompilationUnitTree t : trees) {
JCCompilationUnit cu = (JCCompilationUnit) t;
for (JCTree def : cu.defs) {
if (def.getTag() == JCTree.CLASSDEF && analyzedElems.contains(((JCTree.JCClassDecl) def).sym)) {
//System.out.println("Adding pair...");
res.add(new Pair<>(cu, def));
}
}
}
return res;
} catch (Throwable t) {
throw new AttributionException("Exception while attributing file: " + file);
}
}
use of com.sun.source.tree.CompilationUnitTree in project ceylon-compiler by ceylon.
the class T6404194 method main.
public static void main(String... args) throws IOException {
class MyFileObject extends SimpleJavaFileObject {
MyFileObject() {
super(URI.create("myfo:///Test.java"), SOURCE);
}
@Override
public String getCharContent(boolean ignoreEncodingErrors) {
// 01234567890123456 7890 123456789012345
return "@SuppressWarning(\"foo\") @Deprecated class Test { Test() { } }";
}
}
JavaCompiler javac = ToolProvider.getSystemJavaCompiler();
List<JavaFileObject> compilationUnits = Collections.<JavaFileObject>singletonList(new MyFileObject());
JavacTask task = (JavacTask) javac.getTask(null, null, null, null, null, compilationUnits);
Trees trees = Trees.instance(task);
CompilationUnitTree toplevel = task.parse().iterator().next();
ClassTree classTree = (ClassTree) toplevel.getTypeDecls().get(0);
List<? extends Tree> annotations = classTree.getModifiers().getAnnotations();
Tree tree1 = annotations.get(0);
Tree tree2 = annotations.get(1);
long pos = trees.getSourcePositions().getStartPosition(toplevel, tree1);
if (pos != 0)
throw new AssertionError(String.format("Start pos for %s is incorrect (%s)!", tree1, pos));
pos = trees.getSourcePositions().getEndPosition(toplevel, tree1);
if (pos != 23)
throw new AssertionError(String.format("End pos for %s is incorrect (%s)!", tree1, pos));
pos = trees.getSourcePositions().getStartPosition(toplevel, tree2);
if (pos != 24)
throw new AssertionError(String.format("Start pos for %s is incorrect (%s)!", tree2, pos));
pos = trees.getSourcePositions().getEndPosition(toplevel, tree2);
if (pos != 35)
throw new AssertionError(String.format("End pos for %s is incorrect (%s)!", tree2, pos));
}
use of com.sun.source.tree.CompilationUnitTree in project ceylon-compiler by ceylon.
the class StatusPrinterTaskListener method started.
@Override
public void started(TaskEvent e) {
CompilationUnitTree compilationUnit = e.getCompilationUnit();
String path = e.getSourceFile().toUri().toString();
sp.clearLine();
switch(e.getKind()) {
case ANALYZE:
sp.log("Javac [typecheck]: " + path);
break;
case ENTER:
sp.log("Javac [enter]: " + path);
break;
case GENERATE:
sp.log("Javac [generate]: " + path);
break;
case PARSE:
// do not log parsing for Ceylon files
if (compilationUnit instanceof CeylonCompilationUnit == false)
sp.log("Javac [parse]: " + path);
break;
default:
break;
}
}
Aggregations