0 Replies Latest reply on Jul 29, 2004 8:47 AM by brett_s_r

    javassist.reflect contruction interception draft

    brett_s_r Newbie

      I have posted a proposed enhancement to the javassist.reflect package, to support constructor interception.

      The enhancement provides the following additional features:

      An event is generated prior to the calling of a reflected class's super constructor.
      An event is generated prior to the calling of a reflected class's constructor body. This event has access to the object under-construction, and can optionally bypass the constructor body if required (the default behaviour is to execute the constructor body.
      An event is generated at the completion of the reflected class's constructor body. The entire body is wrapped in a try/catch block that will catch unchecked Throwables (RuntimeException and Error), as well as any checked Exceptions declared in the constructor. The caught Throwables are available to the event along with the object under-construction, and by default they are re-thrown, or they can optionally remain caught.

      The three events are available as three new callback methods in ClassMetaobject:

      public void trapBeforeSuperConstructor() throws Throwable {}
      public boolean trapBeforeClassConstructor(Object underConstruction) throws Throwable {} // return false to bypass constructor body
      public boolean trapAfterClassConstructor(Object underConstruction, Throwable threw) throws Throwable {} // return false to prevent re-throw


      Initially I had though to attempt to wrap the constructors using CtNewWrappedConstructor or similar, or replicating the constructors as a static factory method. This was less attractive when I considered the additional difficulty of actually replacing the object constructed in memory.

      The main TODO is to make the constructor arguments available to the events as Object[]. I guess this would be fairly easy with a WrappedConstructor, but another construction is not what I was after.

      This proposed enhancement is posted on Sourceforge for review.