Java Code Examples for com.sun.source.tree.CompilationUnitTree#getLineMap()

The following examples show how to use com.sun.source.tree.CompilationUnitTree#getLineMap() . 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: T4994049.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
public boolean run(DocletEnvironment root) {
    DocTrees trees = root.getDocTrees();

    SourcePositions sourcePositions = trees.getSourcePositions();
    for (TypeElement klass : ElementFilter.typesIn(root.getIncludedElements())) {
        for (ExecutableElement method : getMethods(klass)) {
            if (method.getSimpleName().toString().equals("tabbedMethod")) {
                TreePath path = trees.getPath(method);
                CompilationUnitTree cu = path.getCompilationUnit();
                long pos = sourcePositions.getStartPosition(cu, path.getLeaf());
                LineMap lineMap = cu.getLineMap();
                long columnNumber = lineMap.getColumnNumber(pos);
                if (columnNumber == 9) {
                    System.out.println(columnNumber + ": OK!");
                    return true;
                } else {
                    System.err.println(columnNumber + ": wrong tab expansion");
                    return false;
                }
            }
        }
    }
    return false;
}
 
Example 2
Source File: TreeAnalyzer.java    From meghanada-server with GNU General Public License v3.0 6 votes vote down vote up
private static Source analyzeUnit(CompilationUnitTree cut, Set<File> errorFiles)
    throws IOException {

  LineMap lineMap = cut.getLineMap();
  URI uri = cut.getSourceFile().toUri();
  File file = new File(uri.normalize());
  String path = file.getCanonicalPath();
  Source source = new Source(path, lineMap);
  if (errorFiles.contains(file)) {
    source.hasCompileError = true;
  }
  SourceContext context = new SourceContext(source);
  analyzeCompilationUnitTree(context, cut);
  source.resetLineRange();
  source.buildMethodCallsBF();
  return source;
}
 
Example 3
Source File: MethodArgumentsScanner.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/** Creates a new instance of MethodArgumentsScanner */
public MethodArgumentsScanner(int offset, CompilationUnitTree tree,
                              SourcePositions positions, boolean methodInvocation,
                              ASTOperationCreationDelegate positionDelegate) {
    this.offset = offset;
    this.tree = tree;
    this.positions = positions;
    this.lineMap = tree.getLineMap();
    this.methodInvocation = methodInvocation;
    this.positionDelegate = positionDelegate;
}
 
Example 4
Source File: ExpressionScanner.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public ExpressionScanner(int lineNumber, int statementStart, int statementEnd,
                         CompilationUnitTree tree, SourcePositions positions) {
    this.tree = tree;
    this.lineNumber = lineNumber;
    this.statementStart = statementStart;
    this.statementEnd = statementEnd;
    this.positions = positions;
    this.lineMap = tree.getLineMap();
}
 
Example 5
Source File: Utils.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public long getLineNumber(Element e) {
    TreePath path = getTreePath(e);
    if (path == null) { // maybe null if synthesized
        TypeElement encl = getEnclosingTypeElement(e);
        path = getTreePath(encl);
    }
    CompilationUnitTree cu = path.getCompilationUnit();
    LineMap lineMap = cu.getLineMap();
    DocSourcePositions spos = docTrees.getSourcePositions();
    long pos = spos.getStartPosition(cu, path.getLeaf());
    return lineMap.getLineNumber(pos);
}
 
Example 6
Source File: CompletenessStressTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private boolean testStatement(StringWriter writer, SourcePositions sp, String text, CompilationUnitTree cut, Tree statement) {
    if (statement == null) {
        return true;
    }
    int start = (int) sp.getStartPosition(cut, statement);
    int end = (int) sp.getEndPosition(cut, statement);
    char ch = text.charAt(end - 1);
    SourceCodeAnalysis.Completeness expected = COMPLETE;
    LineMap lineMap = cut.getLineMap();
    int row = (int) lineMap.getLineNumber(start);
    int column = (int) lineMap.getColumnNumber(start);
    switch (ch) {
        case ',':
        case ';':
            expected = (statement instanceof ExpressionStatementTree)
                    ? COMPLETE
                    : COMPLETE_WITH_SEMI;
            --end;
            break;
        case '}':
            break;
        default:
            writer.write(String.format("Unexpected end: row %d, column %d: '%c' -- %s\n",
                    row, column, ch, text.substring(start, end)));
            return true;
    }
    String unit = text.substring(start, end);
    SourceCodeAnalysis.CompletionInfo ci = getAnalysis().analyzeCompletion(unit);
    if (ci.completeness() != expected) {
        if (expected == COMPLETE_WITH_SEMI && (ci.completeness() == CONSIDERED_INCOMPLETE || ci.completeness() == EMPTY)) {
            writer.write(String.format("Empty statement: row %d, column %d: -- %s\n",
                    start, end, unit));
        } else {
            writer.write(String.format("Expected %s got %s: '%s'  row %d, column %d: -- %s\n",
                    expected, ci.completeness(), unit, row, column, unit));
            return false;
        }
    }
    return true;
}
 
Example 7
Source File: JavaDocGenerator.java    From vertx-docgen with Apache License 2.0 5 votes vote down vote up
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();
}
 
Example 8
Source File: TreeContext.java    From compile-testing with Apache License 2.0 4 votes vote down vote up
TreeContext(CompilationUnitTree compilationUnit, Trees trees) {
  this.compilationUnit = compilationUnit;
  this.trees = trees;
  this.sourcePositions = trees.getSourcePositions();
  this.lineMap = compilationUnit.getLineMap();
}