javagenericsretrofit2generic-programmingrobospice

Get type of Generics


I need to get type of generic type. I've already tried following

public abstract class RetrofitRequest<RESULT> extends SpiceRequest<Response<RESULT>> {

    public RetrofitRequest(){
        super(Response<RESULT>.class); //compile time error
    }

    @Override
    public final Response<RESULT> loadDataFromNetwork() throws Exception {
        WebService webService = RestFulWebService.getRestFulWebService();
        return doInBackground(webService);
    }

    protected abstract Response<RESULT> doInBackground(WebService webService) throws Exception;

}

any idea how to get this done? thanks in advance!!!

and SpiceRequest is something like this

public abstract class SpiceRequest<RESULT> implements Comparable<SpiceRequest<RESULT>> {

    public SpiceRequest(Class<RESULT> clazz) {
        ....
    }
}

Thanks!

-supunz


Solution

  • solved myself with the help of RC comment

    public abstract class RetrofitRequest<RESULT> extends SpiceRequest<Response<RESULT>> {
    
        protected RetrofitRequest(){
            super((Class<Response<RESULT>>) (Class<?>) Response.class);
        }
    
        @Override
        public final Response<RESULT> loadDataFromNetwork() throws Exception {
            WebService webService = RestFulWebService.getRestFulWebService();
            return doInBackground(webService);
        }
    
        @WorkerThread
        protected abstract Response<RESULT> doInBackground(WebService webService) throws Exception;
    
    }
    

    and https://coderanch.com/t/681438/java/Class-Type-List-java#3196253 just saved my day