1 Reply Latest reply: Jul 22, 2010 11:20 PM by EJP RSS

    How to test and cast an object to a generic

    843793
      Hi all,
      my problem is the following:
      I have a list of notifications from a transaction listener of the Eclipse Modeling Framework.
      Each element of that list is of type Notification, but I have to cast it to something else to work with it since it is pretty useless by itself.
      I need to test if it is a certain generic type (+NotifyingListImpl<?>+) but of course I can't use the instanceof test. Is there a way to solve the problem?
      Bear in mind that I googled a lot and tried a lot of solutions without success before asking this question (which I hope it is a piece of cake for an
      java expert :-) )


      As a side problem I don't know the actual type of the generic since I gathered all the information using the Eclipse debug, and the debug
      says my notification is of type NotifyingListImpl$1, I figured out the parameter of that type is ResourceImpl since the inspection of the
      notification's list of fields contains a this$0 of that type. Is my intuition about the Eclipse debug correct?
        • 1. Re: How to test and cast an object to a generic
          EJP
          I need to test if it is a certain generic type (+NotifyingListImpl<?>+) but of course I can't use the instanceof test.
          Yes you can:
          if (object instanceof NotifyingListImpl) { ... }
          As a side problem I don't know the actual type of the generic
          You can't. It is erased to the lower bound at compile time.
          since I gathered all the information using the Eclipse debug, and the debug
          Is my intuition about the Eclipse debug correct?
          It has nothing to do with Eclipse debug. The information isn't available. It was erased by the compiler.