Skip to content
Advertisement

Java instanceOf analogue in Javassist

I’m working on a plugin for Gradle in android project. I use Transformation Api for manipulate with byte code across compilation and for this purpose I choosen Javassist. My question related to this library. Before manipulate byte code I need determine which type has this class. For example, if some ctClass has method setOnClickListener(), I want to know its type (type of class which represented by CtClass). May be ctClass.instanceOf(android.view.View) or ctClass.instanceOf(android.view.ViewGroup). I found method .toClass() but it throws InvocationTargetException and I think it does unnecessary work for me

Advertisement

Answer

The CtClass object is usually obtained using ClassPool.get(“classname”) and represented the class file of the class you selected to get through the “classname”. It basically represents the object itself, not an instance of that object.

Therefore it is not an instantiated object and finding out which “instance” it is does not make sense.

However if you need information about the class hierarchy you can use a lot of methods exposed by the CtClass. Here just a couple of examples:

  • getSuperclass() – obtains the class object representing the superclass of the class
  • getInterfaces() – obtains the class objects representing the interfaces implemented by the class
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement