java—给定一个astnode如何使用EclipseJDT查找方法体?

wkyowqbh  于 2021-07-09  发布在  Java
关注(0)|答案(0)|浏览(230)

我有一个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,依此类推。
正确的方法是什么?

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题