微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

给定 ASTNode 如何使用 Eclipse JDT 查找方法主体?

如何解决给定 ASTNode 如何使用 Eclipse JDT 查找方法主体?

我有一个 ASTNode,我需要找出定义这个 ASTNode 的方法名称。如果它在类本身中定义,那么我需要找出类名。

举个例子,ASTNode 指向“\R”,我需要找出它定义的方法,即emitAndindent(String s)。下面的例子:

/** 
 * Emits  {@code s} with indentation as required. It's important that all code that writes to{@link #out} does it through here,since we emit indentation lazily in order to avoidunnecessary trailing whitespace.
 */
CodeWriter emitAndindent(String s) throws IOException {
  boolean first=true;
  for (  String line : s.split("\\R",-1)) {
    if (!first) {
      if ((javadoc || comment) && trailingNewline) {
        emitIndentation();
        out.append(javadoc ? " *" : "//");
      }
      out.append("\n");
      trailingNewline=true;
      if (statementLine != -1) {
        if (statementLine == 0) {
          indent(2);
        }
        statementLine++;
      }
    }
    first=false;
    if (line.isEmpty())     continue;
    if (trailingNewline) {
      emitIndentation();
      if (javadoc) {
        out.append(" * ");
      }
 else       if (comment) {
        out.append("// ");
      }
    }
    out.append(line);
    trailingNewline=false;
  }
  return this;
}

目前,我正在递归检查 newASTNode.parent 和 newASTNode.parent.parent,依此类推。

方法正确吗?

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。