Reputation: 566
How please works following generic casting?
private <T, N> N send(final T request) {
final String serviceUrl = "someUri"
try {
return (N) webServiceTemplate.marshalSendAndReceive(serviceUrl, request);
} catch (final SoapFaultClientException e) {
}
}
When I call this private method like this:
MyReq request = new MyReq();
MyResp response = send(req);
How this method casting this object into MyResp object? What mean this in return type:
< T, N>
Upvotes: 2
Views: 403
Reputation: 117
< T, N>
Is not a return type. N is.
private <T, N> N send(final T request)
↑
You have "choosen" type T by passing MyReq argument. Before returning value of marshalSendAndReceive it is being casted to N type. In your case MyResp.
<T,N>
just declares that this class/method is generic and you may specify generic input type as well ans output.
Upvotes: 1
Reputation: 1530
Here T and N
are Generic parameters.
Here the return type is N
not <T,N>.
Upvotes: 0
Reputation: 11776
It's about using templates in java similar to templates in C++;
Example:
void add(T x, T y){
System.out.println(x+y);
}
Above function can be called with either of the following...
add(3.0f, 4.5f);
add(2,3);
Means, the function is for any kind of parameters
Upvotes: 0
Reputation: 75565
The method is parameterized with the generic parameters T
and N
. It takes an argument of type T
and returns a reference of type N
.
The return type is not <T,N>
, it is just N
.
Upvotes: 0