Is there no way to distinguish different generic instantiations runtime in Java?
generics, java
Solution
As far as I am aware, sorry, it is simply impossible.
Problem
I come from a C# world and I've just learned about erasure in Java, which put me a bit off. Is there really no way to distinguish `SomeGenericInstance<String>` from `SomeGenericInstance<Integer>` runtime in Java? I'm asking because I've implemented a super simple pub-sub framework and I wanted to have a generic class `GenericMessage<T>`. It's essential not to send `GenericMessage<String>` to listeners of `GenericMessage<Integer>`. I tried implementing it by having a List of key-value pairs where the key is the `Class` object representing the type of the message. But this code line yields `true` which is a problem...: `new GenericMessage<Integer>().getClass.equals(new GenericMessage<String>().getClass())`