use of com.sun.source.tree.CompilationUnitTree 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.tree.CompilationUnitTree in project vertx-docgen by vert-x3.
the class JavaDocGenerator method renderSource.
public String renderSource(TreePath path, List<? extends Tree> trees, String source) {
CompilationUnitTree unit = path.getCompilationUnit();
int from = (int) docTrees.getSourcePositions().getStartPosition(unit, trees.get(0));
int to = (int) docTrees.getSourcePositions().getEndPosition(unit, trees.get(trees.size() - 1));
// Correct boundaries
while (from > 1 && source.charAt(from - 1) != '\n') {
from--;
}
while (to < source.length() && source.charAt(to) != '\n') {
to++;
}
String block = source.substring(from, to);
// Determine margin
int blockMargin = Integer.MAX_VALUE;
LineMap lineMap = unit.getLineMap();
for (Tree statement : trees) {
int statementStart = (int) docTrees.getSourcePositions().getStartPosition(unit, statement);
int lineStart = statementStart;
while (lineMap.getLineNumber(statementStart) == lineMap.getLineNumber(lineStart - 1)) {
lineStart--;
}
blockMargin = Math.min(blockMargin, statementStart - lineStart);
}
// Crop the fragment
StringBuilder fragment = new StringBuilder();
for (Iterator<String> sc = new Scanner(block).useDelimiter("\n"); sc.hasNext(); ) {
String line = sc.next();
int margin = Math.min(blockMargin, line.length());
line = line.substring(margin);
fragment.append(line);
if (sc.hasNext()) {
fragment.append('\n');
}
}
return fragment.toString();
}
Aggregations