mercredi 7 janvier 2015

Using Generics and Reflections in Java for interface method implementation

I am trying to solve an exercise using Guice. I have done pretty much everything else, but this method implementation is confusing the soul out of me. The purpose of this method is to act as a registrar of loggers and there is another method which will act as a dispatch messages to the correct logger (@named loggers). I am not worried about the dispatcher too much as I know how to do that. But the registrar method MUST use generics and reflections. I am badly stuck here and my Java experience is almost non-existent when it comes to using reflections with "java.lang.Class".


The code is given below:



class MyManagerImpl implements MyMgr {

/* MyListener below is an interface */
public synchronized void regService(Class<? extends MyListener> loggerObj) {
...
...
...
}

public synchronized void dispatch(String msg, String logger) {
/ * dispatches the messages to the correct logger which
* I know how to do
*
*/
}


}


I understand that the above wildcard-enabled type expression says "Class contains anytype that implements of, or extends, MyListener interface". But I am quite new to using Class for anonymity. Is it possible to see an example where this loggerObj is actually being modified/used to invoke a member function.






Aucun commentaire:

Enregistrer un commentaire