eclipse - Remove All Methodinvocation from JAVA Source AST -
hi find methodinvocation astvisitor , remove astrewrite when use "for loop" remove methods, remove first method invocation on ever call event. guess problem reason don't refresh ast , compiliationunit , icompilationunit or astrewrite don't know when , how can refresh remove in first event.
static void createast(icompilationunit unit) throws javamodelexception { // create ast icompilationunits compilationunit parse = parse(unit); methodvisitor visitor = new methodvisitor(); parse.accept(visitor); (methodinvocation metinv : visitor.getmethods1()) { try { //if declarated user. if (metinv.resolvemethodbinding().getdeclaringclass().isfromsource()) { methodinvocremove(unit, metinv); } } catch (exception e) { system.out.println("excepotion:" + e.getmessage()); } } }
this method remove methodinvocations.
private static void methodinvocremove(icompilationunit unit, methodinvocation met) { try { iproject project = unit.getjavaproject().getproject(); compilationunit astroot = parse(unit); // create astrewrite ast ast = astroot.getast(); astrewrite rewriter = astrewrite.create(met.getparent().getparent().getast()); rewriter.remove(met.getname().getparent(), null); textedit edits; edits = rewriter.rewriteast(); document document = new document(unit.getsource()); edits.apply(document); // code adding statements unit.getbuffer().setcontents(document.get()); unit.getbuffer().close(); } catch (malformedtreeexception e) { system.out.println("exp!!!" + e.getmessage()); } catch (badlocationexception e) { system.out.println("exp!!!" + e.getmessage()); } catch (javamodelexception e) { system.out.println("exp!!!" + e.getmessage()); } catch (illegalargumentexception e) { system.out.println("exp!!!" + e.getmessage()); } }
this method find method invocation astvisitor. after every event on eclipse jdt call method.
public static void findmethod(iproject project) { try { if (project.isnatureenabled("org.eclipse.jdt.core.javanature")) { ipackagefragment[] packages = javacore.create(project).getpackagefragments(); (ipackagefragment mypack : packages) { if (mypack.getkind() == ipackagefragmentroot.k_source) { (icompilationunit unit : mypack.getcompilationunits()) { compilationunit parse = parse(unit); } } } } } catch (coreexception e) { system.out.println("exp!!!"+e.getmessage()); } }
sample code of java file parse , modification.
public class methodinvoc { private void invoc1() { methoddeclar object1 = new methoddeclar(); system.out.println(object1.getstr()); } private void invoc2() { methoddeclar object2 = new methoddeclar(); system.out.println(object2.getstr()); } private void invoc4() { methoddeclar object3 = new methoddeclar(); system.out.println(object3.getstr()); } }
after call event , run methodinvocremove above code change to:
public class methodinvoc { private void invoc1() { methoddeclar object1 = new methoddeclar(); system.out.println(); } private void invoc2() { methoddeclar object2 = new methoddeclar(); system.out.println(object2.getstr());//not change :( } private void invoc4() { methoddeclar object3 = new methoddeclar(); system.out.println(object3.getstr());//not change :( } }
you need cache astrewrite
per compilationunit
, remove methodinvocation
nodes astrewrite.remove(...)
, apply edits once.
so in pseudo-code:
astrewrite rewriter = astrewrite.create(astroot.getast()); (methodinvocation metinv : getallmethodinvocationstoremove()) { rewriter.remove(metinv, null); } unit.applytextedit(rewriter.rewriteast(), new nullprogressmonitor());
to apply edits in rewriter
, have had experiences code (where unit
instance of icompilationunit
) instead of using document
:
unit.applytextedit(rewriter.rewriteast(), new nullprogressmonitor());
also might want @ post, , use icompilationunit.becomeworkingcopy
icompilationunit.commitworkingcopy
wrap changes: eclipse ast not changing files not opened in eclipse
Comments
Post a Comment