I am kind a confuse on the implementation i am working on. This the method which i am suppose to call
<I, O, T> T post(String url, I data, ContentBuilder<I, HttpEntity> contentBuilder, ResponseHandler<? extends T> responseHandler);
Now i have to write couple of classes which will be used for ContentBuilder and responsehandler. the thing to be noticed is return type T.
I wrote an abstract class WSResponse and then extended it MyWSResponse so that my response handler looks like
ResponseHandler <WSResponse> rh = new MyWSResponse();
The method which is calling this post method has this signature
public interface GPCl{
public <T> T callPost (String data)
}
…
public <T> T callPost(String data){
HttpWebServiceClient client = getHttpWSClient();
ContentBuilder<String, HttpEntity> contentBuilder = new MyXMLBuilder();
ResponseHandler<WSResponse> rh= new MyWSResponse();
WSResponse wsResponse = client.post("", data, contentBuilder, rh);
return (T)wsResponse;
}
Now i am confused at return type of this method. Would it remain T or something else? If i change it from T to WSRespons, i get an error that i haven’t implemented method callPost.
The method callPost is being called as
GPClient cl = new GPClient(myhttpParams);
cl.callPost(d)
P.S:
this is first time i am dealing with generics
Methods of this type —
<T> T method(things that don't mention T)— are almost never what you want to do, and are highly dangerous, because they inevitably involve casts that probably don’t work — but you won’t be warned until it fails at runtime. I suspect that the return type of this method should just beWSResponse, but your question isn’t clear enough for me to be sure.