You are viewing a plain text version of this content. The canonical link for it is here.
Posted to ojb-dev@db.apache.org by Justis Peters <ju...@oculan.com> on 2003/11/06 17:28:42 UTC

Better error for ClassCastException in ProxyHelper.getRealObject(Object)

Hi All,

I'm not sure if this is the right place to send this.  Yet again, an issue I submitted to scarab ended up getting assigned the same ID as an existing bug, and I can't get back to it.  Anyhow, here's the issue:

I recommend that we modify ProxyHelper.getRealObject(Object) to more gracefully handle the case where someone uses their own implementation of java.lang.reflect.InvocationHandler instead of the expected org.apache.ojb.broker.accesslayer.IndirectionHandler.  Here is the line that was causing my ClassCastExcpetion:
  handler = (IndirectionHandler) Proxy.getInvocationHandler(objectOrProxy);

Obviously, the expectation is that we should be using an IndirectionHandler.  If that is the case, we should either check ahead of time (with "instanceof") or we should catch the ClassCastException, log it, and throw a PersistenceBrokerException:

  try
  {
    handler = (IndirectionHandler) Proxy.getInvocationHandler(objectOrProxy);
    return handler.getRealSubject();
  }
  catch (ClassCastException e)
  {
    log.error("The InvocationHandler for the provided Proxy was not an instance of org.apache.ojb.broker.accesslayer.IndirectionHandler.");
    throw new PersistenceBrokerException("The InvocationHandler for the provided Proxy was not an instance of org.apache.ojb.broker.accesslayer.IndirectionHandler.", e);
  }
  catch (IllegalArgumentException e)
  {
    log.error("Could not retrieve real object for given Proxy: "+objectOrProxy);
    throw new PersistenceBrokerException(e);
  }
  catch (PersistenceBrokerException e)
  {
    log.error("Could not retrieve real object for given Proxy: "+objectOrProxy);
    throw e;
  }


This isn't a really big deal.  The only reason I ask for it is because it would have saved me a decent amount of time if I had received an error message of that sort the first time around.

Thanks in advance for your consideration!

Sincerely,
Justis Peters
Oculan Corp.
www.oculan.com


---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-dev-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-dev-help@db.apache.org


Re: Better error for ClassCastException in ProxyHelper.getRealObject(Object)

Posted by Jakob Braeuchi <jb...@gmx.ch>.
hi justis,

scarab does have some problems thats's why i stopped using it :(
anyway, i added the additional catch of ClassCastException.

hth
jakob

Justis Peters wrote:

> Hi All,
> 
> I'm not sure if this is the right place to send this.  Yet again, an issue I submitted to scarab ended up getting assigned the same ID as an existing bug, and I can't get back to it.  Anyhow, here's the issue:
> 
> I recommend that we modify ProxyHelper.getRealObject(Object) to more gracefully handle the case where someone uses their own implementation of java.lang.reflect.InvocationHandler instead of the expected org.apache.ojb.broker.accesslayer.IndirectionHandler.  Here is the line that was causing my ClassCastExcpetion:
>   handler = (IndirectionHandler) Proxy.getInvocationHandler(objectOrProxy);
> 
> Obviously, the expectation is that we should be using an IndirectionHandler.  If that is the case, we should either check ahead of time (with "instanceof") or we should catch the ClassCastException, log it, and throw a PersistenceBrokerException:
> 
>   try
>   {
>     handler = (IndirectionHandler) Proxy.getInvocationHandler(objectOrProxy);
>     return handler.getRealSubject();
>   }
>   catch (ClassCastException e)
>   {
>     log.error("The InvocationHandler for the provided Proxy was not an instance of org.apache.ojb.broker.accesslayer.IndirectionHandler.");
>     throw new PersistenceBrokerException("The InvocationHandler for the provided Proxy was not an instance of org.apache.ojb.broker.accesslayer.IndirectionHandler.", e);
>   }
>   catch (IllegalArgumentException e)
>   {
>     log.error("Could not retrieve real object for given Proxy: "+objectOrProxy);
>     throw new PersistenceBrokerException(e);
>   }
>   catch (PersistenceBrokerException e)
>   {
>     log.error("Could not retrieve real object for given Proxy: "+objectOrProxy);
>     throw e;
>   }
> 
> 
> This isn't a really big deal.  The only reason I ask for it is because it would have saved me a decent amount of time if I had received an error message of that sort the first time around.
> 
> Thanks in advance for your consideration!
> 
> Sincerely,
> Justis Peters
> Oculan Corp.
> www.oculan.com
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: ojb-dev-unsubscribe@db.apache.org
> For additional commands, e-mail: ojb-dev-help@db.apache.org
> 
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-dev-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-dev-help@db.apache.org