如何在源代码中获取通用 ASTNode 的行号?
How to get line number for general ASTNode in the source code?
我如何在基本中实现这个方法
class ASTNode让我更容易获得不同的ASTNode行号?
例如,如果我想获取 MethodDeclaration 位置,我需要编写代码
@Override
public boolean visit(MethodDeclaration node) {
int lineNum = ((CompilationUnit) node.getRoot()).getLineNumber(node.getStartPosition());
return super.visit(node);
}
但是,我想获取这样的位置信息
@Override
public boolean visit(MethodDeclaration node) {
int lineNum = node.getLineNumber();
return super.visit(node);
}
CompilationUnit
提供了一个叫getLineNumber
的方法,用lineEndTable
实现,一般ASTNode
只有一个字段叫startPosition
,所以我有没有可能可以在摘要中获得一个lineEntTable
class ASTNode
?
"Is it possible...?" 不,像 MethodDeclaration
这样的个别节点没有此信息。为了避免冗余 lineEndTable
仅存储在一个特定节点中,即 CompilationUnit
。鉴于您已经找到了解决方案,寻找其他解决方案没有任何好处。 API 就足够了,您需要编写的额外代码最少。
我找到了一个折衷的方法来做到这一点。我为 abstract class ASTNode.
添加了两个方法
public int getStartingLineNumber() {
if (this.getRoot().getNodeType() == 15) {
return (((CompilationUnit)this.getRoot()).getLineNumber(this.getStartPosition()));
}
if (this instanceof Comment) {
ASTNode alter = ((Comment) this).getAlternateRoot();
if (alter.getNodeType() == 15) {
return (((CompilationUnit) alter).getLineNumber(this.getStartPosition()));
}
}
return -1;
}
public String fileName;
public String getFileName() {
if (this.getRoot().getNodeType() == 15) {
return ((CompilationUnit) this.getRoot()).getFileName();
}
if (this instanceof Comment) {
ASTNode alter = ((Comment) this).getAlternateRoot();
if (alter.getNodeType() == 15) {
return ((CompilationUnit) alter).getFileName();
}
}
return fileName;
}`
我还为 CompilationUnit 添加了一个 setFileName
方法,这样我就可以获得每个 ASTNode 的 fileName
和 LineNum
信息。
CompilationUnit result = (CompilationUnit)(astParser.createAST(null));
result.setFileName(javaFilePath);
谁能给我一个更好的解决方案?
我如何在基本中实现这个方法 class ASTNode让我更容易获得不同的ASTNode行号? 例如,如果我想获取 MethodDeclaration 位置,我需要编写代码
@Override
public boolean visit(MethodDeclaration node) {
int lineNum = ((CompilationUnit) node.getRoot()).getLineNumber(node.getStartPosition());
return super.visit(node);
}
但是,我想获取这样的位置信息
@Override
public boolean visit(MethodDeclaration node) {
int lineNum = node.getLineNumber();
return super.visit(node);
}
CompilationUnit
提供了一个叫getLineNumber
的方法,用lineEndTable
实现,一般ASTNode
只有一个字段叫startPosition
,所以我有没有可能可以在摘要中获得一个lineEntTable
class ASTNode
?
"Is it possible...?" 不,像 MethodDeclaration
这样的个别节点没有此信息。为了避免冗余 lineEndTable
仅存储在一个特定节点中,即 CompilationUnit
。鉴于您已经找到了解决方案,寻找其他解决方案没有任何好处。 API 就足够了,您需要编写的额外代码最少。
我找到了一个折衷的方法来做到这一点。我为 abstract class ASTNode.
添加了两个方法public int getStartingLineNumber() {
if (this.getRoot().getNodeType() == 15) {
return (((CompilationUnit)this.getRoot()).getLineNumber(this.getStartPosition()));
}
if (this instanceof Comment) {
ASTNode alter = ((Comment) this).getAlternateRoot();
if (alter.getNodeType() == 15) {
return (((CompilationUnit) alter).getLineNumber(this.getStartPosition()));
}
}
return -1;
}
public String fileName;
public String getFileName() {
if (this.getRoot().getNodeType() == 15) {
return ((CompilationUnit) this.getRoot()).getFileName();
}
if (this instanceof Comment) {
ASTNode alter = ((Comment) this).getAlternateRoot();
if (alter.getNodeType() == 15) {
return ((CompilationUnit) alter).getFileName();
}
}
return fileName;
}`
我还为 CompilationUnit 添加了一个 setFileName
方法,这样我就可以获得每个 ASTNode 的 fileName
和 LineNum
信息。
CompilationUnit result = (CompilationUnit)(astParser.createAST(null));
result.setFileName(javaFilePath);
谁能给我一个更好的解决方案?