java - Call super.super.method, skipping super.method -


i have following (third-party) class structure. we'll call third-party project projectseriously, , note i'm using system.out.println in place of other complicated functionality (100s of lines of code).

class {     public void hi() {         // important thing         system.out.println("important thing a");     } }  class b extends {      public void hi() {         // terrible, terrible things         system.out.println("terrible thing b");          // important thing         super.hi();     } } 

now want write (this isn't valid java):

class c extends b {      public void hi() {         // not-so-terrible things         system.out.println("ok thing c");          // important thing         super.super.hi();     } } 

i have pass instanceof b other piece of wonderful project, projectseriously. seeing these public methods, feel should possible.

you use javassist modify class before use of it.

but really ugly hack, please try refactor code in and/or b expose important parts.

package test;  import javassist.classpool; import javassist.ctclass; import javassist.ctmethod; import javassist.ctnewmethod;  class {     public void hi() {         // important thing         system.out.println("important thing a");     } }  class b extends {      public void hi() {         // terrible, terrible things         system.out.println("terrible thing b");          // important thing         super.hi();     } }  class c extends b {      public void hi() {         // not-so-terrible things         system.out.println("ok thing c");          // important thing         super.hi();     } }  public class main {      public static void main(string[] args) throws exception {         ctclass cc = classpool.getdefault().get("test.b"); // don't use test.b.class.getname() force class loader load class         ctmethod m1 = cc.getdeclaredmethod("hi");         cc.removemethod(m1);         ctmethod m2 = ctnewmethod.copy(m1, cc, null);         m2.setbody("{ /* override method b.hi() body */ return super.hi();}", "this", m1.getname());         cc.addmethod(m2);         cc.toclass();         c obj = new c();         obj.hi();     } } 

result:

ok thing c important thing 

Comments