Is it possible to create an anonymous class while using reflection?

前端 未结 3 573
野性不改
野性不改 2020-12-20 17:25

I would like to be able to implement a method at runtime that is called before an object runs the initializers. This will allow me to set fields that are used during initia

3条回答
  •  遥遥无期
    2020-12-20 18:06

    What you are trying to do would entail creating a new anonymous inner class that is a subclass of some class that you only know at runtime. Reflection cannot solve this problem.

    Dynamic proxies might ... if you were trying to implement a dynamically loaded interface.

    The only other approaches I can think of are to use BCEL or dynamic source code generation / compilation. And both are going to be very complicated. And neither count as pure Java.

    My advice would be to take a fresh look at what it is you are trying to achieve. (Your question gives no hints as to what that might be, so it is hard to offer any practical suggestions.)

提交回复
热议问题