Java 类com.sun.source.tree.LineMap 实例源码

项目:incubator-netbeans    文件:PreferredCCParser.java   
/** return the offset of the first non-whitespace character on the line,
           or -1 when the line does not exist
 */
private static int findLineOffset(LineMap lineMap, CharSequence text, int lineNumber) {
    int offset;
    try {
        offset = (int) lineMap.getStartPosition(lineNumber);
        int offset2 = (int) lineMap.getStartPosition(lineNumber + 1);
        CharSequence lineStr = text.subSequence(offset, offset2);
        for (int i = 0; i < lineStr.length(); i++) {
            if (!Character.isWhitespace(lineStr.charAt(i))) {
                offset += i;
                break;
            }
        }
    } catch (IndexOutOfBoundsException ioobex) {
        return -1;
    }
    return offset;
}
项目:openjdk-jdk10    文件:T4994049.java   
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;
}
项目:openjdk9    文件:T4994049.java   
public boolean run(DocletEnvironment root) {
    DocTrees trees = root.getDocTrees();

    SourcePositions sourcePositions = trees.getSourcePositions();
    for (TypeElement klass : root.getIncludedClasses()) {
        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;
}
项目:meghanada-server    文件:TreeAnalyzer.java   
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();
  return source;
}
项目:error-prone-aspirator    文件:AbortInOvercatch.java   
@Override
public Description matchTry (TryTree tree, VisitorState state) {
  List<? extends CatchTree> catchList = tree.getCatches();
  if (catchList == null || catchList.size() == 0) {
    // TODO: this try block does not have a catch, we should further check the 
    // finally block!
    return Description.NO_MATCH;
  }

  CatchTree lastCatch = catchList.get(tree.getCatches().size() - 1);
  if (overcatch(lastCatch, state)) {
    if (abortInCatch(lastCatch, state)) {
      LineMap lineMap = state.getPath().getCompilationUnit().getLineMap();

      /* System.out.println("****** warning starts **************");
        System.out.println("WARNING: abort in overcatch: " 
           + state.getPath().getCompilationUnit().getSourceFile().getName()
           + ":" + lineMap.getLineNumber(TreeInfo.getStartPos((JCTree) lastCatch)));
        System.out.println(state.getPath().getLeaf());
          System.out.println("****** warning ends **************");
        System.out.println();  */
      return describeMatch(lastCatch, NO_FIX);
    }
  }
  return Description.NO_MATCH;
}
项目:error-prone-aspirator    文件:EmptyCatch.java   
@Override
public Description matchTry (TryTree tree, VisitorState state) {
  if (badEmptyCatchBlock(tree, state)) {
    // If it has finally block, assume it's OK
    BlockTree bt = tree.getFinallyBlock();
    if (bt == null || bt.getStatements().size() == 0) {
      CatchTree lastCatch = tree.getCatches().get(tree.getCatches().size() - 1);
      LineMap lineMap = state.getPath().getCompilationUnit().getLineMap();

       /* System.out.println("****** warning starts **************");
        System.out.println("WARNING: empty catch: " 
           + state.getPath().getCompilationUnit().getSourceFile().getName()
           // + ":" + state.getEndPosition((JCTree) tree)
           + ":" + lineMap.getLineNumber(TreeInfo.getStartPos((JCTree) lastCatch)));
        System.out.println(state.getPath().getLeaf());
        System.out.println(); 
        System.out.println("****** warning ends **************"); */

      return describeMatch(lastCatch, NO_FIX);
    }
  }
  return Description.NO_MATCH;
}
项目:openjdk-jdk10    文件:Utils.java   
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);
}
项目:openjdk-jdk10    文件:CompletenessStressTest.java   
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;
}
项目:openjdk9    文件:Utils.java   
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);
}
项目:vertx-docgen    文件:JavaDocGenerator.java   
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();
}
项目:incubator-netbeans    文件:OrganizeMembers.java   
@TriggerTreeKind(Kind.CLASS)
public static ErrorDescription checkMembers(final HintContext context) {
    for (Diagnostic<?> d : context.getInfo().getDiagnostics()) {
        if (Hacks.isSyntaxError(d)) {
            return null;
        }
    }
    Source source = context.getInfo().getSnapshot().getSource();
    try {
        ModificationResult result = ModificationResult.runModificationTask(Collections.singleton(source), new UserTask() {
            @Override
            public void run(ResultIterator resultIterator) throws Exception {
                WorkingCopy copy = WorkingCopy.get(resultIterator.getParserResult());
                copy.toPhase(Phase.RESOLVED);
                doOrganizeMembers(copy, context.getPath());
            }
        });
        List<? extends Difference> diffs = result.getDifferences(source.getFileObject());
        if (diffs != null && !diffs.isEmpty() && !checkGuarded(context.getInfo().getDocument(), diffs)) {
            Fix fix = new OrganizeMembersFix(context.getInfo(), context.getPath()).toEditorFix();
            SourcePositions sp = context.getInfo().getTrees().getSourcePositions();
            int offset = diffs.get(0).getStartPosition().getOffset();
            LineMap lm = context.getInfo().getCompilationUnit().getLineMap();
            long lno = lm.getLineNumber(offset);
            if (lno >= 1) {
                offset = (int)lm.getStartPosition(lno);
            }
            CompilationUnitTree cut = context.getPath().getCompilationUnit();
            ClassTree clazz = (ClassTree) context.getPath().getLeaf();
            for (Tree member : clazz.getMembers()) {
                if (context.getInfo().getTreeUtilities().isSynthetic(new TreePath(context.getPath(), member))) continue;
                if (sp.getStartPosition(cut, member) >= offset) {
                    return ErrorDescriptionFactory.forTree(context, member, NbBundle.getMessage(OrganizeMembers.class, "MSG_OragnizeMembers"), fix); //NOI18N
                }
            }
            return ErrorDescriptionFactory.forTree(context, clazz, NbBundle.getMessage(OrganizeMembers.class, "MSG_OragnizeMembers"), fix); //NOI18N
        }
    } catch (Exception ex) {
        Exceptions.printStackTrace(ex);
    }
    return null;
}
项目:openjdk9    文件:CompletenessStressTest.java   
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 {
            String oops = unit.substring(max(0, ci.unitEndPos - 10), ci.unitEndPos) + "|||" +
                    unit.substring(ci.unitEndPos, min(unit.length(), ci.unitEndPos + 10));
            writer.write(String.format("Expected %s got %s: '%s'  row %d, column %d: -- %s\n",
                    expected, ci.completeness, oops, row, column, unit));
            return false;
        }
    }
    return true;
}
项目:meghanada-server    文件:Source.java   
public Source(String filePath, LineMap lineMap) {
  this(filePath);
  this.lineMap = lineMap;
}
项目:oblivion-netbeans-plugin    文件:TriggerCompilationUnitTreeImpl.java   
@Override
public LineMap getLineMap() {
    return null;
}
项目:oblivion-netbeans-plugin    文件:CompilationUnitTreeImpl.java   
@Override
public LineMap getLineMap() {
    return null;
}
项目:form-follows-function    文件:Compiler.java   
public static long line(long position) {
    LineMap lm = getState().compilationUnit.getLineMap();
    return (lm != null) ? lm.getLineNumber(position) : 0;
}
项目:form-follows-function    文件:Compiler.java   
public static long column(long position) {
    LineMap lm = getState().compilationUnit.getLineMap();
    return (lm != null) ? lm.getColumnNumber(position) : 0;
}
项目:form-follows-function    文件:Compiler.java   
public static long position(long line, long column) {
    LineMap lm = getState().compilationUnit.getLineMap();
    return (lm != null) ? lm.getPosition(line, column) : Position.NOPOS;
}
项目:form-follows-function    文件:Compiler.java   
public static long startPosition(long line) {
    LineMap lm = getState().compilationUnit.getLineMap();
    return (lm != null) ? lm.getStartPosition(line) : Position.NOPOS;
}
项目:javaide    文件:CompilationUnitTree.java   
/**
 * Gets the line map for this compilation unit, if available.
 * Returns null if the line map is not available.
 * @return the line map for this compilation unit
 */
LineMap getLineMap();
项目:form-follows-function    文件:CompilationUnitTree.java   
/**
 * Gets the line map for this compilation unit, if available.
 * Returns null if the line map is not available.
 * @return the line map for this compilation unit
 */
LineMap getLineMap();
项目:form-follows-function    文件:UnitTree.java   
/**
 * Gets the line map for this compilation unit, if available.
 * Returns null if the line map is not available.
 * @return the line map for this compilation unit
 */
LineMap getLineMap();
项目:openjdk-source-code-learn    文件:CompilationUnitTree.java   
/**
 * Gets the line map for this compilation unit, if available.
 * Returns null if the line map is not available.
 * @return the line map for this compilation unit
 */
LineMap getLineMap();
项目:s4j    文件:CompilationUnitTree.java   
/**
 * Gets the line map for this compilation unit, if available.
 * Returns null if the line map is not available.
 * @return the line map for this compilation unit
 */
LineMap getLineMap();
项目:jdk7-langtools    文件:CompilationUnitTree.java   
/**
 * Gets the line map for this compilation unit, if available.
 * Returns null if the line map is not available.
 * @return the line map for this compilation unit
 */
LineMap getLineMap();
项目:javap    文件:CompilationUnitTree.java   
/**
 * Gets the line map for this compilation unit, if available.
 * Returns null if the line map is not available.
 * @return the line map for this compilation unit
 */
LineMap getLineMap();
项目:openjdk-icedtea7    文件:CompilationUnitTree.java   
/**
 * Gets the line map for this compilation unit, if available.
 * Returns null if the line map is not available.
 * @return the line map for this compilation unit
 */
LineMap getLineMap();
项目:metricgenerator-jdk-compiler    文件:CompilationUnitTree.java   
/**
 * Gets the line map for this compilation unit, if available.
 * Returns null if the line map is not available.
 * @return the line map for this compilation unit
 */
LineMap getLineMap();
项目:INF5000-StaticProxy    文件:CompilationUnitTree.java   
/**
 * Gets the line map for this compilation unit, if available.
 * Returns null if the line map is not available.
 * @return the line map for this compilation unit
 */
LineMap getLineMap();
项目:checker-framework    文件:CompilationUnitTree.java   
@PolyRead LineMap getLineMap(@PolyRead CompilationUnitTree this);