0

In my GWT client I am trying to ensure that the Throwable passed to my Callback#onFailure() implementations is always handled, because many implementations in this vast code base doesn't do anything about it.

To do this I have defined my own RemoteServiceProxy#doCreateRequestCallback() which wraps every Callback in my own implementation. In that #onFailure() method I want to use my own version of a Throwable so that I can track whether it gets handled, eg. when Throwable#getMessage() is called, so I pass it to the original Callback#onFailure().

public class ThrowableProxy extends Throwable {
    private Throwable delegate;
    private boolean handled;

    public ThrowableProxy(Throwable delegate) {
        this.delegate = delegate;
        this.handled = false;
    }

    // Overriding Throwable's methods to defer to the delegate

    // package protected
    boolean isHandled() {
        return this.handled;
    }
}

This all looks fine, until you hit instanceof. Now, if the client code wants to check the type of the exception, eg. throwable instanceof StatusCodeException, the Throwable is an instance of ThrowableProxy, but what I really want is to check the the type of the delegate.

How does instanceof work? Can I somehow make it check the delegate without doing something like throwable.getDelegate() instanceof StatusCodeException?

Ankur Lathi
  • 7,636
  • 5
  • 37
  • 49
Niel de Wet
  • 7,806
  • 9
  • 63
  • 100
  • possible duplicate of [What is the 'instanceof' operator used for?](http://stackoverflow.com/questions/7313559/what-is-the-instanceof-operator-used-for) – Buhake Sindi Jul 23 '13 at 15:11
  • I think you are missing the point of the question, I am trying to find out if there is a way to make `instanceof` consider the delegate, or another way to solve this problem. I thought about overriding `#getClass()` but I couldn't find any documentation to support that. – Niel de Wet Jul 23 '13 at 15:16
  • I am quite non-understanding now. A proxy class would implement an interface too; you could make a common marker interface. Instead of delegating one could also use `setCause/getCause`. – Joop Eggen Jul 23 '13 at 15:34
  • You can't do what you want to do. Simple as that. – Bohemian Jul 23 '13 at 16:15
  • So, do you want something of this effect? `StatusCodeException.class.isInstance(throwable)` or am I misunderstanding this completely? – Buhake Sindi Jul 23 '13 at 19:52

2 Answers2

0

instanceof works just like in regular Java -- you can't "override" it for ThrowableProxy.

I'm not entirely sure what you want to accomplish, but you could have all of your callbacks extend your own class, and require them to call a handledFailure(ex) method and you could do something if onFailure returns without having called that. Of course, if you are modifying the code to make that call, you could just as easily modify it to actually handle the failure.

BTW, I think using instanceof to check a laundry-list of types is a code smell, and indicates you probably need a common superclass to encode whatever it is you are checking.

jat
  • 61
  • 3
  • About your last sentence, he means that the client code may use `instanceof` - so the implication is he doesn't have control over that. Anyway, `instanceof` is generally more acceptable in `catch` blocks. – Paul Bellora Jul 23 '13 at 15:21
0

I'm not sure what you are trying to accomplish here, it would be really helpful if you detailed more what you are trying to accomplish here. But this is what I see based on the information you provided...

If you are trying to check instanceof against a wide variety Exceptions in the client you will eventually hit a brick wall as not all exceptions are available in the client, remember that the client will run Javascript when compiled and there are limitations to be considered and that includes a limited set of Exceptions. A good way to implement a centralized error handling in the client is to use:

GWT.setUncaughtExceptionHandler

This allows you to implement a UncaughtExceptionHandler that will ultimately catch all the exceptions not explicitly handled in the app. A good way to do that is to implement you own app specific runtime exception that would allow you the granular handling you apparently require.

This is a wild guess since I honestly don't get what your goal is.

Chepech
  • 5,258
  • 4
  • 47
  • 70