2 Replies Latest reply on May 23, 2005 9:05 PM by Daisuke Kameda

    NullPointerException occurs when Handler#getType() method is

    Daisuke Kameda Newbie

      I faced the problem to which NullPointerException occurs when
      Handler#getType() method is executed in ExprEditor#edit(Handler).
      Originally, edit(Handler) should hook only catch block.
      But, the method hooks the finally block and the synchronized block.

      As the result of my own investigation, it turns out that the problem
      is derived from the following specification of the Java class file.

      1. The catch block, the finally block, and the synchronized block are stored
      in the same exception table.

      2. There is an area where the type of the exception (name of Exception class)
      is stored in the entry of an exception table.

      But, in the finally block and the synchronized block, 0 which means
      arbitrary exceptions is stored.


      The code of Handler#getType() is as follows.

      public CtClass getType() throws NotFoundException {
       ConstPool cp = getConstPool();
       String name = cp.getClassInfo(etable.catchType(index));
       return Descriptor.toCtClass(name, thisClass.getClassPool());
      }


      In getType() method, ConstPool#getClassInfo() returns null, in case of
      the block which is hooked is finally or synchronized. So, NullPointerException
      has been generated because the value is referred by Descriptor#toCtClass().


      To avoid this problem, I propose to check null of the return value of
      ConstPool#getClassInfo() (type of the exception) in the getType() method.

        • 1. Re: NullPointerException occurs when Handler#getType() metho
          Shigeru Chiba Expert

          Thank you for your bug report and pretty good analysis.
          It is really helpful.

          To avoid this problem, I propose to check null of the return value of
          ConstPool#getClassInfo() (type of the exception) in the getType() method


          Yes, this is a correct and quick fix. However, if your analysis is correct,
          I think ExprEditor.edit(Handler) should not be called with a finally block.
          (Note that Javac automatically adds a finally block for each synchronized
          block. So the exception table includes a catch block or a finally
          block.)

          Don't you think I must modify the implementation of ExprEditor
          rather than getType()? Or, do you like to hook both catch and finally
          blocks?

          Let me know your thought. Thx.



          • 2. Re: NullPointerException occurs when Handler#getType() metho
            Daisuke Kameda Newbie

            I agree with you. Concretely, I propose the following modification.

            - Change the ExprEditor#edit(Handler) method to hook only the catch block.

            Moreover, I want to other methods to treat respectively the synchronized
            block and finally block. So, I want you to adopt the new methods for hooking
            the synchronized block and the finally block respectively.

            I would prefer it if you could consider this idea.