tags:

views:

113

answers:

2

I have been working on Google AdWords and came across this code

adwords-api-6.4.0, com.google.api.adwords.lib.AdWordsUser

public <T extends java.rmi.Remote> T getService(AdWordsService service) throws ServiceException {
    try {
      return (T) AdWordsServiceFactory.generateSerivceStub(service, this,
          service.getEndpointServer(this.isUsingSandbox()), false);
    } catch (ClassCastException e) {
      throw new ServiceException("Cannot cast serivce. Check the type of return-capture variable.", e);
    }
}

which is invoked like this:

AdWordsUser user = new AdWordsUser();
AdGroupServiceInterface adGroupService = user.getService(AdWordsService.V200909.ADGROUP_SERVICE);

Could you please explain how generics work in getService method? How is the return type determined?
What is the purpose of such usage? It doesn't seem like it provides type safety.
Does this usage have a specific name (so I could find more info on it and change the question title)?

+2  A: 

I know more about C# than Java, but I'd guess that the getService method takes a parameter of type AdWordsService, and returns a type T, which must derive from java.rmi.Remote.

Presumably the AdWordsServiceFactory.generateSerivceStub method return type is of type java.rmi.Remote or similar, so it would be legal to cast this into the T type.

In C#, generics are used in this way to avoid casting, but the casting's still taking place here. I guess with this particular method, it's saving you from having to cast to your desired type, by making you tell the generic method exactly what type you want back.

I would also imagine that you need to specify the type of T in your call to getService. Maybe like this?

AdGroupServiceInterface adGroupService = user.getService<AdGroupServiceInterface>(AdWordsService.V200909.ADGROUP_SERVICE);
Graham Clark
in java rather *user.<AdGroupServiceInterface>getService (...)*
Roman
+2  A: 
erickson