克隆检测算法访问 AST 节点中的“src”字段时出现 NoSuchField 错误

问题描述 投票:0回答:1

我正在研究克隆检测算法,需要输出 AST(抽象语法树)中节点的位置。但是,当我尝试访问节点 (e.src) 的 src 字段时,遇到 NoSuchField("src") 错误。这是令人费解的,因为在 AST 遍历和记录期间,src 字段似乎带有位置注释。以下是日志片段和相关 Java 代码:

method
method(int(),"sum",[parameter(int(),"a",0,src=|java+compilationUnit:///src/duplication.java|(225,5,<12,19>,<12,24>),decl=|unresolved:///|,typ=int()),parameter(int(),"b",0,src=|java+compilationUnit:///src/duplication.java|(232,5,<12,26>,<12,31>),decl=|unresolved:///|,typ=int())],[],block([return(infix(simpleName("a",src=|java+compilationUnit:///src/duplication.java|(256,1,<13,15>,<13,16>),decl=|unresolved:///|,typ=int()),"+",simpleName("b",src=|java+compilationUnit:///src/duplication.java|(260,1,<13,19>,<13,20>),decl=|unresolved:///|,typ=int()),src=|java+compilationUnit:///src/duplication.java|(256,5,<13,15>,<13,20>),typ=int()),src=|java+compilationUnit:///src/duplication.java|(249,13,<13,8>,<13,21>)),return(infix(simpleName("a",src=|java+compilationUnit:///src/duplication.java|(279,1,<14,15>,<14,16>),decl=|unresolved:///|,typ=int()),"+",simpleName("b",src=|java+compilationUnit:///src/duplication.java|(283,1,<14,19>,<14,20>),decl=|unresolved:///|,typ=int()),src=|java+compilationUnit:///src/duplication.java|(279,5,<14,15>,<14,20>),typ=int()),src=|java+compilationUnit:///src/duplication.java|(272,13,<14,8>,<14,21>)),return(infix(simpleName("a",src=|java+compilationUnit:///src/duplication.java|(301,1,<15,15>,<15,16>),decl=|unresolved:///|,typ=int()),"+",simpleName("b",src=|java+compilationUnit:///src/duplication.java|(305,1,<15,19>,<15,20>),decl=|unresolved:///|,typ=int()),src=|java+compilationUnit:///src/duplication.java|(301,5,<15,15>,<15,20>),typ=int()),src=|java+compilationUnit:///src/duplication.java|(294,13,<15,8>,<15,21>)),return(infix(simpleName("a",src=|java+compilationUnit:///src/duplication.java|(323,1,<16,15>,<16,16>),decl=|unresolved:///|,typ=int()),"+",simpleName("b",src=|java+compilationUnit:///src/duplication.java|(327,1,<16,19>,<16,20>),decl=|unresolved:///|,typ=int()),src=|java+compilationUnit:///src/duplication.java|(323,5,<16,15>,<16,20>),typ=int()),src=|java+compilationUnit:///src/duplication.java|(316,13,<16,8>,<16,21>)),return(infix(simpleName("a",src=|java+compilationUnit:///src/duplication.java|(345,1,<17,15>,<17,16>),decl=|unresolved:///|,typ=int()),"+",simpleName("b",src=|java+compilationUnit:///src/duplication.java|(349,1,<17,19>,<17,20>),decl=|unresolved:///|,typ=int()),src=|java+compilationUnit:///src/duplication.java|(345,5,<17,15>,<17,20>),typ=int()),src=|java+compilationUnit:///src/duplication.java|(338,13,<17,8>,<17,21>))],src=|java+compilationUnit:///src/duplication.java|(239,118,<12,33>,<18,5>)),modifiers=[public()],src=|java+compilationUnit:///src/duplication.java|(210,147,<12,4>,<18,5>),decl=|unresolved:///|,typ=unresolved())
     void main() {
        M3 model = createM3FromMavenProject(|project://java-benchmark|);
        list[Declaration] asts = getASTs(model);
    
        detectClones(asts);
    }
    
    void detectClones(list[Declaration] asts)
    {
        visit(asts)
        {
            case node e : {
                println(getName(e));
                println(e.src);
                // println(e);
            }
        }
    }

我正在寻求帮助来理解为什么尽管 src 字段在日志中可见,但还是会发生 NoSuchField("src") 错误。任何正确访问 src 字段的见解或解决方案将不胜感激。

rascal
1个回答
0
投票

这是因为类型节点没有这个属性。如果您要匹配正确的类型(例如声明),它就会起作用。

如果你真的想要像这样动态的东西,你需要使用 Node 模块中的函数

getKeywordParameters

© www.soinside.com 2019 - 2024. All rights reserved.