Java - Reflection to get Method's Generic Type?

The second addService is not possible unless you map on the class name (instead of Type ) - e.g.

The second addService is not possible unless you map on the class name (instead of Type) - e.g. Public class ServiceContainer { HashMap services; public ServiceContainer() { services = new HashMap(); } public void addService(Class, T object) { services. Put(t, object); } public void addService(T object) { Class type = object.getClass(); services. Put(type, object); } } the reason typeOfT() doesn't work is because java generics are "erased" after compilation.

It is really only 'syntactic sugar' that the ocmpiler uses to check for obvious errors in type assignments. Edit: since the question changed: to implement the containsService method: public boolean containsService(String classname) { return services. Get(Class.

ForName(classname))! = null; } I'd say, given what you're doing is quite common, have a look into how you might use a dependency injection library to perform your service registrations and retrieval. I hear good things about Spring , and google Guice.

And what is the difference between Class and Type. The java docs are kind of vague – cable729 May 8 '11 at 3:01 I would pass in either a Class literal (e.g. , MyService. Class), or even the name of the class, into the containsService method.

– Chii May 8 '11 at 5:03 I don't know what you mean by Type - I assumed it was a class you wrote yourself that designates the type of the service (unless you meant download.oracle.com/javase/1,5.0/docs/api/java/lang/reflect/… - which won't be useful in your case. – Chii May 8 '11 at 5:04 I did go ahead and include a class literal in it. It's a one time thing most of the time, so it's not a big deal.

– cable729 May 8 '11 at 5:15.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions