3 Replies Latest reply: Jul 29, 2011 3:46 AM by Andrew Dinn RSS

Catching ClassCastExceptions

Scott Stark Master

Is there a way to catch ClassCastExceptions and determine the object that was on the right side of the cast, as well as the Class object to which the object was being cast?

  • 1. Re: Catching ClassCastExceptions
    Andrew Dinn Master

    Hi Scott,

    Scott Stark wrote:

     

    Is there a way to catch ClassCastExceptions and determine the object that was on the right side of the cast, as well as the Class object to which the object was being cast?

    I'm not sure exactly what you are asking for here. Can you giev a concrete example  of the situation you are interested in and what you would like the injected code to  do?

  • 2. Re: Catching ClassCastExceptions
    Scott Stark Master

    Yes, what I would like is to something like the following, but I don't believe every cast goes through the Class.cast method:

     

    ########################################################################

    #

    # Rule to trace print details of ClassCastException

    #

     

     

    RULE ClassCastException trace

    METHOD java.lang.Class.cast(Object)

    AT THROW java.lang.ClassCastException

    IF TRUE

    DO traceStack(15)

              printProtectionDomain($0, $1.getClass())

    ENDRULE

  • 3. Re: Catching ClassCastExceptions
    Andrew Dinn Master

    Scott Stark wrote:

     

    Yes, what I would like is to something like the following, but I don't believe every cast goes through the Class.cast method:

     

    ########################################################################

    #

    # Rule to trace print details of ClassCastException

    #

     

     

    RULE ClassCastException trace

    METHOD java.lang.Class.cast(Object)

    AT THROW java.lang.ClassCastException

    IF TRUE

    DO traceStack(15)

              printProtectionDomain($0, $1.getClass())

    ENDRULE

     

    Well, that would be nice but I don't know if it's possible to do what you want, perhaps also not even a close approximation. Most of these exceptions are going to happen as a result of executing a checkcast bytecode -- virtually, that is, as clearly it actually happens in the interpreter or in JITted machine code. Byteman can only modify execution at the bytecode level and cannot go under the hood to catch things happening in the VM. So the short answer is no.

     

    The slightly longer answer is that you ought be able to do some of what you want. You should be able to intercept the construction of the ClassCastException by injecting a rule into it's constructor(s). That would allow you to see the checkcast failiure and even, if you acessed the exception stack trace (AT EXIT to be sure it is available) to locate the class, method and line number for the exception but not to idenitfy what was being assigned.

     

    As an aside, I'll explain the modality in those last few sentences. I think it is possible that a specific JVM might construct the ClassCastException internally in the VM without executing the Java constructor  but I think that is probably unlikely --  optimising exceptions is never a priority and, anyway, not running certain constructors is the sort of anomaly that comes back to bite you the more you do it. Also, I am not clear exactly what the stack trace is supposed to look like when the exception is being constructed. It should include the offending method frame below the exception consructor frame but I don't know whether there ought or ought not to be other frames in between representing either auxiliary Java code used to invoke the constructor or non-Java frames. I don't think the VMspec gets specific about either of these two behaviours but I am not certain of that.