Using Javassist library to detect runtime type of a method caller -


i know javassist.expr.methodcall.getclassname() returns compile time type of method caller because depends on bytecode analysis. wondering if there efficient way actual runtime type of method caller javassist using trick or through code inspection.

here simple example make things clearer.

public interface animal {      public void eat(); }   public class dog implements animal {      @override     public void eat() {         system.out.println("dog eating");        }  }   public class mainclass {      public static void main(string[] args) {             animal = new dog();         a.eat();     } } 

in example, find way "dog" object method caller method "a.eat()"

from javassist.expr.methodcall can runtime class has called method:

ctclass ctc = javassist.expr.methodcall.getmethod().getdeclaringclass(); 

once have javassist representation of class called method contains need class.

ps if need dog instance can use reflection taking name ctclass, e.g.:

class clazz = class.forname(ctc.getname()); dog dog = ((dog)clazz).newinstance(); 

Popular posts from this blog

php - How should I create my API for mobile applications (Needs Authentication) -

5 Reasons to Blog Anonymously (and 5 Reasons Not To)

Google AdWords and AdSense - A Dynamic Small Business Marketing Duo