dimanche 23 juillet 2017

Finding the most specific class in Java

I tried to write some kind of exception handler for my specific purposes. I have a list of classes. Let's say:

List<Class<? extends Throwable>> list = new LinkedList<>();
list.add(RuntimeException.class);
list.add(IllegalArgumentException.class);

Now, I want to do:

public Class<? extends Throwable> findMostSpecific(Class<? extends Throwable> type) {
     for (....) {
         if (... WHAT TO PUT HERE ? .... ) {
             return ....
         }
     }
}

this method has to find the most specific class of given type. So if I pass:

  • IllegalArgumentException, it has to find IllegalArgumentException (not RTE).
  • RuntimeException, it has to find RuntimeException
  • IllegalStateException, it has to find also RuntimeException (becuse IllegalStateException is not on the list)

So how to find the most specific type of a given instance? Is it possible?





Aucun commentaire:

Enregistrer un commentaire