GWT work-around for missing Class.isInstance()

前端 未结 2 2017
花落未央
花落未央 2021-01-02 10:07

I\'m trying to write a job scheduling system in GWT that maintains an array of exceptions (Class[] exceptions), that might be resolve

相关标签:
2条回答
  • 2021-01-02 10:33

    Maybe something like this would help:

    boolean offerRetry(Exception exception) {
       try{
          throw exception;
       } catch (SpecException se) {
          return true;
       } catch (SpecException1 se1) {
          return true;
       ...
       } catch (Exception e) {
          return false;
       }   
    }
    

    It depends on how you construct the array of exceptions. If the java 7 stuff works properly then you could put all exceptions in one catch:

    boolean offerRetry(Exception exception) {
       try{
          throw exception;
       } catch (SpecException | SpecException1 | ... se) {
          return true;
       } catch (Exception e) {
          return false;
       }   
    }
    
    0 讨论(0)
  • 2021-01-02 10:39

    I use following code:

     public static <T> boolean isInstanceOf(Class<T> type, Object object) {
        try {
             T objectAsType = (T) object;
         } catch (ClassCastException exception) {
             return false;
         }
         return true;
     }
    
    0 讨论(0)
提交回复
热议问题