I'm working on a clone detection algorithm and need to output the locations of nodes in an AST (Abstract Syntax Tree). However, when I attempt to access the src field of a node (e.src), I encounter a NoSuchField("src") error. This is puzzling because, during AST traversal and logging, the src field appears to be present with location annotations. Below is a snippet from the log and the relevant Java code:
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);
}
}
}
I'm seeking assistance in understanding why the NoSuchField("src") error occurs despite the src field being visible in the log. Any insights or solutions to access the src field correctly would be greatly appreciated.
It's because the type node doesn't have that property. If you were to match on a proper type such as Declaration, it would work.
It you truly want something dynamic like this, you need to use the function getKeywordParameters
from the Node module.