Calling ".class" on Typed class (e.g. MyClass<MyType>.class)

63 views Asked by At

I am trying to use a function in GreenRobot's EventBus class:

public <T> T removeStickyEvent(@NotNull Class<T> tClass);

This works with normal classes such as:

MyEvent myEvent = eventbus.removeStickyEvent(MyEvent.class);

Now, I would like to get away from defining classes that just hold a single object (e.g. MyStringEvent, MyIntegerEvent) and use generics (e.g. MyEvent<String>, MyEvent<Integer>). However, the following code no longer works:

// Error: 'Cannot select from parameterized type'
MyEvent<String> myStringEvent = eventBus.removeStickyEvent(MyEvent<String>.class);

Is there a proper way to handle this use case?

0

There are 0 answers