Hi all.
I have this observer pattern based logger in my program. In it I have a method that allows you get get a listener depending on its class. Thought it would be a simple solution, since it is all I need at the moment and it didnt take much effort to implement. I use generics to make so the caller wont have to care that much about casting. However, I get a compiler warning about an unsafe cast.
The code itself runs just fine. No exceptions are thrown during runtime (in fact I am just using the method once, to avoid sending the object along side with the logger). Is this warning something I should worry about?public synchronized <T extends LogListener> T findListener(Class<T> logClass) { T found = null; if (logClass != null) { for (LogListener l : mListeners) // mListeners is just a list of listeners { if ( logClass.isInstance( l ) ) { found = (T) l; // This is where the warning comes in break; } } } return found; }
Take care,
Kerr.