java: How to fix the Unchecked cast warning
casting, generics, java
Solution
`Class.cast` is what you want. Well, you might consider not using reflection.
Change the line:
T result = (T)store.get(e);
to:
T result = exampleClass.cast(store.get(e));
Problem
I've got the following code: ``` private HashMap<Class<?>, HashMap<Entity, ? extends Component>> m_componentStores; public <T extends Component> T getComponent(Entity e, Class<T> exampleClass) { HashMap<Entity, ? extends Component> store = m_componentStores.get(exampleClass); T result = (T)store.get(e); if (result == null) { throw new IllegalArgumentException( "GET FAIL: "+e+" does not possess Component of class\nmissing: "+exampleClass ); } return result; } ``` When I compile, it shows that `T result = (T)store.get(e)` has an unchecked cast. ``` Type safety: Unchecked cast from capture#2-of ? extends Component to T ``` What am I missing to prevent this warning from appearing?