java - How to create FunctionCall statement with arguments and add in function -


i need create functioncall statement utiliy.initialize("value") , add in first line of every function of js file.

below code using trying create functioncall

private functioncall getfunctioncall() {         functioncall functioncall = new functioncall();         name name =  new name();         name.setidentifier("initialize");         functioncall.settarget(name); } 

below code using add in every functionnode

class functionvisitor implements nodevisitor {         @override         public boolean visit(astnode node) {              if (node.getclass() == functionnode.class) {                 functionnode fun = (functionnode) node;                 fun.addchildrentofront(getfunctioncall());             }             return true;         }      } 

please suggest how can create functioncall arguments , how can print created functioncall statement test. there tool available view javascript nodes java astview viewer?

you have create stringliteral argument , add functioncall, in othere case numberliteral, arrrayliteral etc. (see: http://javadox.com/org.mozilla/rhino/1.7r4/org/mozilla/javascript/ast/astnode.html)

private functioncall getfunctioncall() {         functioncall functioncall = new functioncall();         name name =  new name();         name.setidentifier("initialize");         functioncall.settarget(name);         stringliteral arg = new stringliteral();         arg.setvalue("value");         arg.setquotecharacter('"');         functioncall.addargument(arg);         return functioncall; }  class functionvisitor implements nodevisitor {         @override         public boolean visit(astnode node) {              if (node.getclass() == functionnode.class) {                 functionnode fun = (functionnode) node;                 if(fun.getname().equals("initialize")) //prevents infinit loop                 {                      return true;                 }                 fun.getbody().addchildrentofront(new emptystatement()); // adds ';', don't know if required                 fun.getbody().addchildrentofront(getfunctioncall());//no fun.addchildrentofront             }             return true;         }      } 

you can print every corect astnode tosource() method. hope help.


Comments

Popular posts from this blog

matlab - "Contour not rendered for non-finite ZData" -

delphi - Indy UDP Read Contents of Adata -

javascript - Any ideas when Firefox is likely to implement lengthAdjust and textLength? -