user3466413
user3466413

Reputation: 819

Getting around type erasure in Java/GWT

In a GWT project, I have a method with this signature:

public <H> HandlerRegistration addHandler(Event.Type<H> type, H handler)

In that method, I want to include some logging messages; specifically, I want to know <H> so I can log what was passed. Java generic type erasure, being the way it works, compiles it out and it effectively becomes Object. And, being GWT, I can't use some of the fancier reflection techniques as they're not compatible with GWT. So, if I read type.getClass().getName(), there's no mention of what <H> is.

Is there some way around this?

Upvotes: 2

Views: 104

Answers (1)

Richard Lewan
Richard Lewan

Reputation: 232

I would call getClass().getName() on handler rather than on type. This should work no matter the handler implementation or Event Type. You will get back the Class object that represents the runtime class <H>.

http://docs.oracle.com/javase/7/docs/api/java/lang/Object.html#getClass%28%29

Upvotes: 1

Related Questions