Q:

NoSuchMethodException when instantiating a generic class

I have a class as follows:
public class StreamEventSuccess<T> {

    private final T event;

    public StreamEventSuccess(final T event) {
        this.event = event;
    }

    public T getEvent() {
        return event;
    }
}

I try to instiate it:
 StreamEventSuccess<BusinessEvent> success = new StreamEventSuccess<BusinessEvent>(event);

and I get:
java.lang.NoSuchMethodError: com.ryan.protocol.StreamEventSuccess.<init>(Lcom/ryan/domain/event/BusinessEvent;)V

Any idea why?
the event parameter is of type BusinessEvent

A:

The problem you are describing would happen if:

your-library-version-1 had a constructor StreamEventSuccess(BusinessEvent event)
you have a project (or class) project-x that was compiled with your-library-version-1.
your-library-version-2 was made generic StreamEventSuccess(T event).
And you are running project-x with your-library-version-2.

If that's the case, just recompile the class that calls your new constructor.
That happens because the actual type of that constructor in the generic form is <init>(Ljava/lang/Object;)V so you must recompile the dependencies after generifying that class, because their bytecode is pointing to the old (non generic) version of the constructor <init>(Lcom/ryan/domain/event/BusinessEvent;)V

