You are here: Home JastAdd Forums The JastAdd Extensible Java Compiler (JastAddJ) how to check if a given method was called
Document Actions

how to check if a given method was called

Up to The JastAdd Extensible Java Compiler (JastAddJ)

how to check if a given method was called

Posted by Roberta Lopes Arcoverde at May 10. 2008

Hi there!


I'm sorry if this is not the right place to post this kind of question, but I'm kinda clueless :(


I need to implement an extension to JastAdd Java Compiler that verifies if a given method is called in a given scope (another method's body, for instance). I was trying to navigate through the AST and check every single Stmt, but I guess there are easier (and more elegant) ways to do such verification using equations; I just dont know how to start implementing it :(


Just to make it clear, this is sort of what I need:


MethodDecl m = ...;
boolean b = m.getBlock().callsMethod("foo") //checks if "foo" is called from m


Thanks in advance for ANY help and ANY hints,


Roberta


Re: how to check if a given method was called

Posted by Torbjörn Ekman at June 24. 2008

Hi Roberta,


I would use a collection-attribute to find all calls in a method with the given property:


// first get a binding from a MethodAccess to its enclosing MethodDecl or null if it is enclosed in an initializer or a constructor.
syn MethodDecl MethodAccess.enclosingMethod() = enclosingBodyDecl() instanceof MethodDecl ? (MethodDecl)enclosingBodyDecl() : null;
// define a collection for each Method that holds a set of MethodAccess nodes, use add to combine contributions, and include contributions from the current BodyDecl only.
coll HashSet MethodDecl.interestingCalls() [new HashSet()] with add root BodyDecl;
// A MethodAccess contributes itself to the interestingCalls() collection which is found in the node referenced by enclosingMethod() when the bound method has the name "foo".
MethodAccess contributes
  this when enclosingMethod() != null && decl().hasName("foo")
to MethodDecl.interestingCalls() for enclosingMethod();


There is a paper describing collection attributes in more detail here

 


Best regards
/Torbjorn


Powered by Ploneboard