4

I have the following generic method in my project that will return a BaseModel object dependent on the class given:

public <T> T getObjectfromList(Class<T> clazz) {
   for (BaseModel model : items) {
      if (model.getClass().equals(clazz))
         return (T) model;
      }

      return null;
   }
}

Right now I am getting a lint warning on the line return (T) model because it is an unchecked cast from BaseModel to T.

To work around this, I added the bound here:

public <T extends BaseModel> T getObjectfromList(Class<T> clazz) {
   for (BaseModel model : items) {
      if (model.getClass().equals(clazz))
         return (T) model;
      }

      return null;
   }
}

However, the lint warning still appears. How can I work around this?

AdamMc331
  • 16,492
  • 10
  • 71
  • 133

0 Answers0