Skip to content
Advertisement

Generic Method in Java with out parameters

I am trying to implement something similar to EventBus implemented in eshopOnContainers.

Is it Possible to define a method like this in java & read the meta data about T and TH at runtime ? There can be multiple classes extending IntegrationEvent(e.g. PriceChangedEvent) & we should be able to Identify the exact class name at runtime.

public <T extends IntegrationEvent, TH extends IntegrationEventHandler<T>> void subscribe() {
       
        
}

Advertisement

Answer

You can pass type info into method:

        public <T extends IntegrationEvent, TH extends IntegrationEventHandler<T>> void subscribe(java.lang.Class<T> tClass, java.lang.Class<TH> thClass) {

        }

Probably Guava Event Bus will be useful for you.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement