Determine generics type alternative

View: New views
1 Messages — Rating Filter:   Alert me  

Determine generics type alternative

by Prodoc81 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi,

I though I had a clever way of using java generics but this turned out not to be the case.

In the following class I'm using the class parameter type to create a List with items of the same type. This all works nice and well, except for the actual query bit. The table it should get its data from is now hard-coded ("Threat"). This should, however, be based on the parameter type. My plan was to determine the type and use that to create the query. Determining the type turns out not to be possible at all, with something like instanceof.

Now I'm looking for a clean alternative but I can't think of one. Do you have any idea's or suggestions?


------------------------------------------------------
public class AddDeviceTool<T extends GeneralDevice> implements DeviceTool, EventListener {
  private DeviceManager deviceManager;
  private EntityManager entityManager;
  private Query deviceQuery;
  private List<T> deviceList;

  public AddDeviceTool(DeviceManager deviceManager) {
      this.svgCanvas = deviceManager.getCanvas();
      this.deviceManager = deviceManager;

      entityManager = java.beans.Beans.isDesignTime() ? null : javax.persistence.Persistence.createEntityManagerFactory("MPSFPU").createEntityManager();
      deviceQuery = java.beans.Beans.isDesignTime() ? null : entityManager.createQuery("SELECT d FROM " + "Threat" + " D");

      @SuppressWarnings("unchecked")
      List<T> list = (List<T>) (java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : deviceQuery.getResultList());             deviceList = list;
  }


  public void handleEvent(final Event evt) {
      ...
      deviceManager.addDevice(device, mousePosition);
      ...
  }

  ....
}
------------------------------------------------------

Yours,

Age